Skip to content

Commit

Permalink
Add some methods to SmbOutputStream (Fixes #27)
Browse files Browse the repository at this point in the history
  • Loading branch information
swaechter committed Aug 28, 2022
1 parent 627dc23 commit fd655fb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public class SmbOutputStream extends OutputStream {
private final File file;

/**
* output stream of the file that will be decorated.
* Output stream of the file that will be decorated.
*/
private final OutputStream outputStream;

/**
* Flag whether the content should be appended or the file should be overwritten/written at the beginning.
*/
private final Boolean appendContent;

/**
* Create a new decorated output stream that respects the reference counting close mechanism of the file. It's possible to append or
* overwrite existing content.
Expand All @@ -32,14 +37,45 @@ public class SmbOutputStream extends OutputStream {
public SmbOutputStream(File file, boolean appendContent) {
this.file = file;
this.outputStream = file.getOutputStream(appendContent);
this.appendContent = appendContent;
}

/**
* {@inheritDoc}
* Write a single value to the SMB file. The value is appended if appendContent is set to true.
*
* @param value Value to write or append
* @throws IOException Exception in case of an IO/network problem
*/
@Override
public void write(int value) throws IOException {
outputStream.write(value);
}

/**
* Write a byte buffer to the SMB file. The values are appended if appendContent is set to true. Otherwise, they will overwrite the file.
*
* @param values Values to write or append
* @throws IOException Exception in case of an IO/network problem
*/
@Override
public void write(byte[] values) throws IOException {
outputStream.write(values, 0, values.length); // smbj ignores the offset when appending is enabled
}

/**
* Write a byte buffer to the SMB file at a given offset with a given size. This method can only be used when appendContent is set to false.
*
* @param values Values to write
* @param offset Offset in the file
* @param length Length of the values to write
* @throws IOException Exception in case of an IO/network problem
*/
@Override
public void write(int i) throws IOException {
outputStream.write(i);
public void write(byte[] values, int offset, int length) throws IOException {
if (appendContent) {
throw new IOException("The method SmbOutputStream.write(values, offset, length) can not be used when appendingContent is set to true.");
}
outputStream.write(values, offset, length);
}

/**
Expand Down
43 changes: 38 additions & 5 deletions src/test/java/ch/swaechter/smbjwrapper/SmbFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -34,24 +35,54 @@ public void testUploadAndDownload(TestConnection testConnection) throws Exceptio
SmbDirectory subDirectory1 = transferDirectory.createDirectoryInCurrentDirectory("Subdirectory1");

// Upload a file
InputStream inputStream = new FileInputStream(new File("src/test/resources/Screenshot.png"));
File file = new File("src/test/resources/Screenshot.png");
assertTrue(file.exists());
byte[] expectedData = Files.readAllBytes(file.toPath());
long expectedSize = file.length();

InputStream inputStream = new FileInputStream(file);
assertNotNull(inputStream);

SmbFile subFile2_1 = subDirectory1.createFileInCurrentDirectory("Subfile1.txt");
SmbFile subFile2_1 = subDirectory1.createFileInCurrentDirectory("Subfile1.png");
OutputStream outputStream = subFile2_1.getOutputStream();
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();

// Transfer/download a file
SmbFile subFile2_2 = subDirectory1.createFileInCurrentDirectory("Subfile2.txt");
assertEquals(expectedSize, subFile2_1.getFileSize());

// Transfer/download a file via SmbOutputStream.write(buffer) method
SmbFile subFile2_2 = subDirectory1.createFileInCurrentDirectory("Subfile2.png");
inputStream = subFile2_1.getInputStream();
outputStream = subFile2_2.getOutputStream();

IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();

// Check the file size
assertEquals(37888, subFile2_2.getFileSize());

// Check the file content
inputStream = subFile2_2.getInputStream();
byte[] readData = IOUtils.toByteArray(inputStream);
assertArrayEquals(expectedData, readData);
inputStream.close();

// Write a file via SmbOutputStream.write(buffer, offset length) method
outputStream = subFile2_2.getOutputStream();
outputStream.write(expectedData, 0, expectedData.length);
outputStream.close();

// Check the file size
assertEquals(37888, subFile2_2.getFileSize());

// Check the file content
inputStream = subFile2_2.getInputStream();
readData = IOUtils.toByteArray(inputStream);
assertArrayEquals(expectedData, readData);
inputStream.close();

// Clean up
transferDirectory.deleteDirectoryRecursively();
assertFalse(transferDirectory.isExisting());
Expand Down Expand Up @@ -83,7 +114,9 @@ public void testAppendedUploadAndDownload(TestConnection testConnection) throws

// Do a first non-appended upload
OutputStream outputStream1 = testFile.getOutputStream();
outputStream1.write(testData1.getBytes(StandardCharsets.UTF_8));
for (byte character : testData1.getBytes(StandardCharsets.UTF_8)) {
outputStream1.write(character);
}
outputStream1.close();

InputStream inputStream1 = testFile.getInputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hierynomus.smbj.share.File;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand All @@ -11,7 +12,7 @@
import static org.mockito.Mockito.*;

/**
* This class provides a test for the shared input stream.
* This class provides a test for the SMB input stream.
*
* @author Simon Wächter
*/
Expand All @@ -22,8 +23,7 @@ public class SmbInputStreamTest {
*
* @throws IOException Exception in case of a problem
*/
// TODO: Re-enable test when Mockito-inline on Java 17 works
//@Test
@Test
public void testSmbInputStream() throws IOException {
// Create a spy input stream object
String testData = "Hello";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.hierynomus.smbj.share.File;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -11,7 +12,7 @@
import static org.mockito.Mockito.*;

/**
* This class provides a test for the shared output stream.
* This class provides a test for the SMB output stream.
*
* @author Simon Wächter
*/
Expand All @@ -22,8 +23,7 @@ public class SmbOutputStreamTest {
*
* @throws IOException Exception in case of a problem
*/
// TODO: Re-enable test when Mockito-inline on Java 17 works
//@Test
@Test
public void testSmbOutputStream() throws IOException {
// Create a spy output stream object
String testData = "Hello";
Expand All @@ -41,7 +41,7 @@ public void testSmbOutputStream() throws IOException {
// Test the data and method invocations
assertEquals(testData, new String(outputStream.toByteArray()));
verify(file, times(1)).close();
verify(outputStream, times(5)).write(anyInt());
verify(outputStream, times(1)).write(any(byte[].class), anyInt(), anyInt());
verify(outputStream, times(1)).flush();
verify(outputStream, times(1)).close();
}
Expand Down

0 comments on commit fd655fb

Please sign in to comment.