I have the following code, the file appears on the share and it grows in size, but the code never completes. I.e: System.out.println("Writing file done.");
never executes.
The transferTo(...)
is a new method from Java 9+, which pretty much does the standard loop read buffer and write buffer with a buffer size of 8192.
try (
DiskShare share = (DiskShare) session.connectShare(sharee);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(sourcePath))
) {
Set<FileAttributes> fileAttributes = new HashSet<>();
fileAttributes.add(FileAttributes.FILE_ATTRIBUTE_NORMAL);
Set<SMB2CreateOptions> createOptions = new HashSet<>();
createOptions.add(SMB2CreateOptions.FILE_SEQUENTIAL_ONLY);
String fileName = "test.txt";
try(
File f = share.openFile(directory + "\\" + fileName, new HashSet(Arrays.asList(new AccessMask[]{AccessMask.GENERIC_ALL})), fileAttributes, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN_IF, createOptions);
BufferedOutputStream bo = new BufferedOutputStream(f.getOutputStream())
) {
System.out.println("Writing file to share...");
bi.transferTo(bo);
System.out.println("Writing file done.");
}
}