java conversion work
This commit is contained in:
parent
59cd8a8baf
commit
d8a7a0b70c
@ -22,20 +22,20 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
|
import com.google.common.io.ByteStreams;
|
||||||
import com.google.common.io.CharStreams;
|
import com.google.common.io.CharStreams;
|
||||||
import com.google.common.io.InputSupplier;
|
import com.google.common.io.InputSupplier;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public class JavaInputStreamToXUnitTest {
|
public class JavaInputStreamToXUnitTest {
|
||||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
private static final int DEFAULT_SIZE = 150000000;
|
private static final int DEFAULT_SIZE = 1500000;
|
||||||
|
|
||||||
// private static final int DEFAULT_SIZE = 8;
|
|
||||||
|
|
||||||
// tests - InputStream to String
|
// tests - InputStream to String
|
||||||
|
|
||||||
// 11s
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
public final void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
@ -49,7 +49,6 @@ public class JavaInputStreamToXUnitTest {
|
|||||||
assertEquals(textBuilder.toString(), originalString);
|
assertEquals(textBuilder.toString(), originalString);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 8s
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
@ -120,4 +119,25 @@ public class JavaInputStreamToXUnitTest {
|
|||||||
assertThat(writer.toString(), equalTo(originalString));
|
assertThat(writer.toString(), equalTo(originalString));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tests - InputStream to byte[]
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingPlainJava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||||
|
final InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||||
|
final byte[] targetArray = new byte[initialStream.available()];
|
||||||
|
initialStream.read(targetArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||||
|
final InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream();
|
||||||
|
final byte[] targetArray = ByteStreams.toByteArray(initialStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||||
|
final ByteArrayInputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||||
|
final byte[] targetArray = IOUtils.toByteArray(initialStream);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,9 @@ package org.baeldung.java.io;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.apache.commons.io.input.ReaderInputStream;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -16,26 +16,26 @@ import com.google.common.io.CharSource;
|
|||||||
public class JavaXToInputStreamUnitTest {
|
public class JavaXToInputStreamUnitTest {
|
||||||
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
|
// tests - String - InputStream
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
public final void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "text";
|
final String initialString = "text";
|
||||||
final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
|
final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
|
||||||
final byte[] buffer = new byte[targetStream.available()];
|
|
||||||
targetStream.read(buffer);
|
|
||||||
final String targetString = new String(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "text";
|
final String initialString = "text";
|
||||||
final CharSource source = CharSource.wrap(initialString);
|
final InputStream targetStream = new ReaderInputStream(CharSource.wrap(initialString).openStream());
|
||||||
final Reader targetStream = source.openStream();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException {
|
||||||
final String initialString = "text";
|
final String initialString = "text";
|
||||||
final InputStream targetStream = IOUtils.toInputStream(initialString);
|
final InputStream targetStream = IOUtils.toInputStream(initialString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user