It's all about the answers!

Ask a question

Contents are not readable after programmatically delivering files to stream.


Dashrath Kale (1542423) | asked Jul 24 '13, 5:30 a.m.
retagged Aug 14 '13, 12:54 p.m. by Te-Hsin Shih (2854)

I can deliver file to the stream under component but the contents are not getting in readable format and some files getting blank. We checked this by using UTF-8 encoding but facing the same issue. I we covert a String into byte array like. (

new VersionedContentManagerByteArrayInputStreamPovider("my file contentst".getBytes()),

null, monitor);

). then we can read the contents from the repository files.

With the following code I trying to deliver the files but contents are not readable.

File testFile= new File(

"C:/testFile.txt");

byte[] byteArray = new byte[(int) testFile.length()];

IFileItem file = (IFileItem) IFileItem.ITEM_TYPE.createItem();

file.setName(testFile.getName());

file.setParent(applicationFolder);

IFileContentManager contentManager = FileSystemCore

.getContentManager(repo);

IFileContent storedContent = contentManager.storeContent(

IFileContent.ENCODING_US_ASCII,

FileLineDelimiter.LINE_DELIMITER_PLATFORM,

new VersionedContentManagerByteArrayInputStreamPovider(byteArray),

null, monitor);

file.setContentType(IFileItem.CONTENT_TYPE_TEXT);

file.setContent(storedContent);

file.setFileTimestamp(new Date());

workspace.commit(cs1, Collections.singletonList(workspace

.configurationOpFactory().save(file)), monitor);

One answer



permanent link
Tim Mok (6.6k38) | answered Jul 25 '13, 11:02 a.m.
JAZZ DEVELOPER

File testFile= new File( "C:/testFile.txt");

byte[] byteArray = new byte[(int) testFile.length()];

I'm guessing that byteArray is empty. I don't see anywhere a line that sets the content of the array. You allocate the memory but never read the file into it. I'm guessing that results in your empty file.

Comments
Dashrath Kale commented Jul 26 '13, 12:56 a.m. | edited Jul 26 '13, 9:06 a.m.

Hi Tim

Thanks for your suggetion , however it resolved using the FileUtils.readFileToByteArray(File file) method as shown below.

IFileContent storedContent = contentManager

.storeContent(

IFileContent.ENCODING_US_ASCII,

FileLineDelimiter.LINE_DELIMITER_PLATFORM,

new VersionedContentManagerByteArrayInputStreamPovider(

FileUtils

.readFileToByteArray(testFile)),null, monitor);


Tim Mok commented Jul 26 '13, 9:09 a.m.
JAZZ DEVELOPER

That would be a reasonable fix as your original post did not read the file bytes to give to the input stream. Using FileUtils#readFileToByteArray() gives the file contents to the input stream so that it can be saved. Otherwise, you were giving empty content to the file that you were saving.

Your answer


Register or to post your answer.