added InputStream to String examples
This commit is contained in:
parent
0f9fef5337
commit
52600ea3ab
|
@ -2,14 +2,17 @@ package org.baeldung.java;
|
|||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
@ -24,12 +27,32 @@ import com.google.common.io.InputSupplier;
|
|||
|
||||
public class CoreJavaIoUnitTest {
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
private static final int DEFAULT_SIZE = 150000000;
|
||||
|
||||
// private static final int DEFAULT_SIZE = 8;
|
||||
|
||||
// tests - InputStream to String
|
||||
|
||||
// 11s
|
||||
@Test
|
||||
public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final StringBuilder textBuilder = new StringBuilder();
|
||||
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
|
||||
int c = 0;
|
||||
while ((c = reader.read()) != -1) {
|
||||
textBuilder.append((char) c);
|
||||
}
|
||||
}
|
||||
assertEquals(textBuilder.toString(), originalString);
|
||||
}
|
||||
|
||||
// 8s
|
||||
@Test
|
||||
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(8);
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
// When
|
||||
|
@ -43,7 +66,7 @@ public class CoreJavaIoUnitTest {
|
|||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(8);
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
final InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
|
||||
|
@ -62,7 +85,7 @@ public class CoreJavaIoUnitTest {
|
|||
|
||||
@Test
|
||||
public final void givenUsingGuavaAndJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(8);
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
// When
|
||||
|
@ -76,7 +99,7 @@ public class CoreJavaIoUnitTest {
|
|||
|
||||
@Test
|
||||
public final void givenUsingCommonsIo_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(8);
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
// When
|
||||
|
@ -86,7 +109,7 @@ public class CoreJavaIoUnitTest {
|
|||
|
||||
@Test
|
||||
public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||
final String originalString = randomAlphabetic(8);
|
||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||
|
||||
// When
|
||||
|
|
Loading…
Reference in New Issue