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);
One answer
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.File testFile= new File( "C:/testFile.txt");
byte[] byteArray = new byte[(int) testFile.length()];
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.