BAEL-1033 Introduction to StreamUtils
This commit is contained in:
parent
344fdce2a0
commit
ece39cb689
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.streamutils;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
|
public class CopyStream {
|
||||||
|
public static String getStringFromInputStream(InputStream input) throws IOException {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
IOUtils.copy(input, writer, "UTF-8");
|
||||||
|
return writer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream getNonClosingInputStream() throws IOException {
|
||||||
|
InputStream in = new FileInputStream("src/test/resources/input.txt");
|
||||||
|
return StreamUtils.nonClosing(in);
|
||||||
|
}
|
||||||
|
}
|
@ -8,14 +8,14 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.StringWriter;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
|
import static com.baeldung.streamutils.CopyStream.getStringFromInputStream;
|
||||||
|
|
||||||
public class CopyStreamTest {
|
public class CopyStreamTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -97,14 +97,4 @@ public class CopyStreamTest {
|
|||||||
Assert.assertEquals(inputFileContent, content);
|
Assert.assertEquals(inputFileContent, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getStringFromInputStream(InputStream input) throws IOException {
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
IOUtils.copy(input, writer, "UTF-8");
|
|
||||||
return writer.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public InputStream getNonClosingInputStream() throws IOException {
|
|
||||||
InputStream in = new FileInputStream("src/test/resources/input.txt");
|
|
||||||
return StreamUtils.nonClosing(in);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user