Updates to resolve issues raised by Kevin Gilmore
This commit is contained in:
parent
87cc504e9e
commit
0d66be29a7
@ -11,17 +11,14 @@ import org.junit.Test;
|
|||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
|
|
||||||
public class InputStreamToOutputStreamUnitTest {
|
public class InputStreamToOutputStreamUnitTest {
|
||||||
|
|
||||||
// buffer size used for reading and writing
|
|
||||||
private static final int BUFFER_SIZE = 8192;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads all bytes from an input stream and writes them to an output stream.
|
* Reads all bytes from an input stream and writes them to an output stream.
|
||||||
* @param source - input stream to copy data from
|
* @param source - input stream to copy data from
|
||||||
* @param target - output stream to copy data too
|
* @param target - output stream to copy data too
|
||||||
*/
|
*/
|
||||||
private static void copy(InputStream source, OutputStream target) throws IOException {
|
void copy(InputStream source, OutputStream target) throws IOException {
|
||||||
byte[] buf = new byte[BUFFER_SIZE];
|
byte[] buf = new byte[8192];
|
||||||
int length;
|
int length;
|
||||||
while ((length = source.read(buf)) > 0) {
|
while ((length = source.read(buf)) > 0) {
|
||||||
target.write(buf, 0, length);
|
target.write(buf, 0, length);
|
||||||
@ -29,56 +26,61 @@ public class InputStreamToOutputStreamUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
public void givenUsingJavaEight_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "Hello World!";
|
String initialString = "Hello World!";
|
||||||
|
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
||||||
copy(inputStream, targetStream);
|
copy(inputStream, targetStream);
|
||||||
|
|
||||||
assertEquals(initialString, new String(targetStream.toByteArray()));
|
assertEquals(initialString, new String(targetStream.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException {
|
public void givenUsingJavaEight_whenCopyingLongInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = randomAlphabetic(20480);
|
String initialString = randomAlphabetic(20480);
|
||||||
|
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
||||||
copy(inputStream, targetStream);
|
copy(inputStream, targetStream);
|
||||||
|
|
||||||
assertEquals(initialString, new String(targetStream.toByteArray()));
|
assertEquals(initialString, new String(targetStream.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
public void givenUsingJavaNine_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "Hello World!";
|
String initialString = "Hello World!";
|
||||||
|
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
||||||
inputStream.transferTo(targetStream);
|
inputStream.transferTo(targetStream);
|
||||||
|
|
||||||
assertEquals(initialString, new String(targetStream.toByteArray()));
|
assertEquals(initialString, new String(targetStream.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
public void givenUsingGuava_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "Hello World!";
|
String initialString = "Hello World!";
|
||||||
|
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
||||||
ByteStreams.copy(inputStream, targetStream);
|
ByteStreams.copy(inputStream, targetStream);
|
||||||
|
|
||||||
assertEquals(initialString, new String(targetStream.toByteArray()));
|
assertEquals(initialString, new String(targetStream.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
public void givenUsingCommonsIO_whenCopyingInputStreamToOutputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "Hello World!";
|
String initialString = "Hello World!";
|
||||||
|
|
||||||
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
try (InputStream inputStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
ByteArrayOutputStream targetStream = new ByteArrayOutputStream()) {
|
||||||
IOUtils.copy(inputStream, targetStream);
|
IOUtils.copy(inputStream, targetStream);
|
||||||
|
|
||||||
assertEquals(initialString, new String(targetStream.toByteArray()));
|
assertEquals(initialString, new String(targetStream.toByteArray()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user