I need to append data to an existing file using the SMBJ client. The below code appends the data, but the final file is corrupted, not able to read/open the file.
Set<AccessMask> accessMask = new HashSet<>();
accessMask.add(AccessMask.GENERIC_READ);
accessMask.add(AccessMask.GENERIC_WRITE);
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_RANDOM_ACCESS);
File file = share.openFile("PATH", accessMask, fileAttributes, SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_OPEN_IF, createOptions);
// Approach - 1
long fileOffset = 0;
byte[] buffer = new byte[1024*4];
int length = inputStream.read(buffer);
while(length != -1){
fileOffset = share.getFileInformation("PATH").getStandardInformation().getEndOfFile();
file.write(buffer, fileOffset, 0, lenght);
}
//Approach - 2
OutputStream oStream = f.getOutputStream();
oStream.write(fileContents.getBytes());
oStream.flush();
oStream.close();
But from both approaches, not able to append the data properly.