-1

One of my Java Utility class (PGP Encryption utility) reads java.io.File (which is in encrypted format) and converts it to java.io.InputStream object (basically decrypts the file and converts as java.io.InputStream object at the end of the method) I can't modify the utility class as it is used across the application. I need to write this object to an smb file to a shared path which needs password authentication. How can i convert java.io.InputStream to smb file and write it a shared path

1) Is it possible using jcifs? 2) If possible do we have any sample program?

User_1940878
  • 321
  • 1
  • 6
  • 25
  • You need to write the *contents of the stream*, not the stream object itself. Copying a stream is trivial. Please clarify your question. – user207421 Jul 11 '19 at 23:12

2 Answers2

1

There is no difference in write process for case of two usual File objects.

final SmbFile destFile = ... //Depends on how you init SmbFile object

InputStream inputStream = ... // your InputStream with data
OutputStream outputStream = null;
try {
    outputStream = destFile.getOutputStream();

    byte[] buf = new byte[STREAM_BUFFER_SIZE];
    int read;
    while ((read = inputStream.read(buf)) > -1) {
        outputStream.write(buf, 0, read);
    }
    outputStream.flush();
} finally {
    if (inputStream != null)
        inputStream.close();
    if (outputStream != null)
        outputStream.close();
}
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • Or, just use the [transferTo](https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/io/InputStream.html#transferTo%28java.io.OutputStream%29) method. – VGR Jul 11 '19 at 21:32
0
InputStream fileInputStream // already filled
File newFile = diskShare.openFile(fullFileName, EnumSet.of(AccessMask.FILE_WRITE_DATA), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OVERWRITE_IF, null);
            OutputStream oStream = newFile.getOutputStream();
            oStream.write(fileInputStream.readAllBytes());
            oStream.close();
user666
  • 1,750
  • 3
  • 18
  • 34