Skip to content

Commit

Permalink
Write input stream to remote file (#789)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Heid <daniel.heid@stromnetz-hamburg.de>
  • Loading branch information
dheid and Daniel Heid committed Aug 25, 2023
1 parent 7b3a423 commit c9ab3d8
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/main/java/com/hierynomus/smbj/utils/SmbFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.hierynomus.smbj.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.EnumSet;
Expand All @@ -33,36 +35,50 @@
public class SmbFiles {

/**
* Copies local file to a destination path on the share
* Copies a local file to a destination path on the share
*
* @param share the share
* @param destPath the path to write to
* @param source the local File
* @param overwrite true/false to overwrite existing file
* @return the actual number of bytes that was written to the file
* @throws java.io.FileNotFoundException
* @throws java.io.IOException
* @throws FileNotFoundException file with the specified pathname does not exist
* @throws IOException file could not be read
*/
public static long copy(File source, DiskShare share, String destPath, boolean overwrite) throws IOException {
long r = 0;
long bytesWritten = 0L;
if (source != null && source.exists() && source.canRead() && source.isFile()) {
try (InputStream is = new FileInputStream(source)) {
bytesWritten = write(is, share, destPath, overwrite);
}
}
return bytesWritten;
}

try (InputStream is = new java.io.FileInputStream(source)) {
if (destPath != null && is != null) {
try (com.hierynomus.smbj.share.File f = share.openFile(
destPath,
EnumSet.of(AccessMask.GENERIC_WRITE),
EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL),
EnumSet.of(SMB2ShareAccess.FILE_SHARE_WRITE),
(overwrite ? SMB2CreateDisposition.FILE_OVERWRITE_IF : SMB2CreateDisposition.FILE_CREATE),
EnumSet.noneOf(SMB2CreateOptions.class)
)) {
r = f.write(new InputStreamByteChunkProvider(is));
}
}
/**
* Writes an input stream to a destination path on the share
*
* @param source the local File
* @param share the share
* @param destPath the path to write to
* @param overwrite true/false to overwrite existing file
* @return the actual number of bytes that was written to the file
*/
public static long write(InputStream source, DiskShare share, String destPath, boolean overwrite) {
long bytesWritten = 0L;
if (destPath != null && source != null) {
try (com.hierynomus.smbj.share.File file = share.openFile(
destPath,
EnumSet.of(AccessMask.GENERIC_WRITE),
EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL),
EnumSet.of(SMB2ShareAccess.FILE_SHARE_WRITE),
overwrite ? SMB2CreateDisposition.FILE_OVERWRITE_IF : SMB2CreateDisposition.FILE_CREATE,
EnumSet.noneOf(SMB2CreateOptions.class)
)) {
bytesWritten = file.write(new InputStreamByteChunkProvider(source));
}
}
return r;
return bytesWritten;
}

/**
Expand Down

0 comments on commit c9ab3d8

Please sign in to comment.