java conversion work

This commit is contained in:
eugenp 2014-06-21 11:49:21 +03:00
parent 59cd8a8baf
commit d8a7a0b70c
2 changed files with 35 additions and 15 deletions

View File

@ -22,20 +22,20 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.InputSupplier;
@SuppressWarnings("unused")
public class JavaInputStreamToXUnitTest {
protected final Logger logger = LoggerFactory.getLogger(getClass());
private static final int DEFAULT_SIZE = 150000000;
// private static final int DEFAULT_SIZE = 8;
private static final int DEFAULT_SIZE = 1500000;
// tests - InputStream to String
// 11s
@Test
public void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
public final void givenUsingJava5_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
final String originalString = randomAlphabetic(DEFAULT_SIZE);
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
@ -49,7 +49,6 @@ public class JavaInputStreamToXUnitTest {
assertEquals(textBuilder.toString(), originalString);
}
// 8s
@Test
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
final String originalString = randomAlphabetic(DEFAULT_SIZE);
@ -120,4 +119,25 @@ public class JavaInputStreamToXUnitTest {
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);
}
}

View File

@ -3,9 +3,9 @@ package org.baeldung.java.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,26 +16,26 @@ import com.google.common.io.CharSource;
public class JavaXToInputStreamUnitTest {
protected final Logger logger = LoggerFactory.getLogger(getClass());
// tests - String - InputStream
@Test
public void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
public final void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
final String initialString = "text";
final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
final byte[] buffer = new byte[targetStream.available()];
targetStream.read(buffer);
final String targetString = new String(buffer);
}
@Test
public void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException {
final String initialString = "text";
final CharSource source = CharSource.wrap(initialString);
final Reader targetStream = source.openStream();
final InputStream targetStream = new ReaderInputStream(CharSource.wrap(initialString).openStream());
}
@Test
public void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException {
public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException {
final String initialString = "text";
final InputStream targetStream = IOUtils.toInputStream(initialString);
}
//
}