Search code examples
javainputstreamjava-iosmbjcifs

Write java.io.InputStream object to smb file


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?


Solution

  • 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();
    }