Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

Contents are not readable after programmatically delivering files to stream.

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);

0 votes



One answer

Permanent link

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.

0 votes

Comments

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);

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 log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,940
× 1,203

Question asked: Jul 24 '13, 5:30 a.m.

Question was seen: 7,332 times

Last updated: Aug 14 '13, 12:54 p.m.

Confirmation Cancel Confirm