JAVA-620: New module core-java-io-conversions-2
This commit is contained in:
parent
a057b3af44
commit
02a4eee18c
9
core-java-modules/core-java-io-conversions-2/README.md
Normal file
9
core-java-modules/core-java-io-conversions-2/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
## Core Java IO Conversions
|
||||||
|
|
||||||
|
This module contains articles about core Java input/output(IO) conversions.
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string)
|
||||||
|
- [Java InputStream to Byte Array and ByteBuffer](https://www.baeldung.com/convert-input-stream-to-array-of-bytes)
|
||||||
|
- [Java – Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file)
|
||||||
|
- More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions)
|
37
core-java-modules/core-java-io-conversions-2/pom.xml
Normal file
37
core-java-modules/core-java-io-conversions-2/pom.xml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>core-java-io-conversions-2</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-io-conversions-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-java</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-java</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-io-conversions</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.baeldung.inputstreamtobytes;
|
||||||
|
|
||||||
|
import com.google.common.io.ByteSource;
|
||||||
|
import com.google.common.io.ByteStreams;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.ReadableByteChannel;
|
||||||
|
|
||||||
|
import static java.nio.channels.Channels.newChannel;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class InputStreamToByteBufferUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingCoreClasses_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException {
|
||||||
|
byte[] input = new byte[] { 0, 1, 2 };
|
||||||
|
InputStream initialStream = new ByteArrayInputStream(input);
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.allocate(3);
|
||||||
|
while (initialStream.available() > 0) {
|
||||||
|
byteBuffer.put((byte) initialStream.read());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(byteBuffer.position(), input.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingGuava__whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException {
|
||||||
|
InputStream initialStream = ByteSource
|
||||||
|
.wrap(new byte[] { 0, 1, 2 })
|
||||||
|
.openStream();
|
||||||
|
byte[] targetArray = ByteStreams.toByteArray(initialStream);
|
||||||
|
ByteBuffer bufferByte = ByteBuffer.wrap(targetArray);
|
||||||
|
while (bufferByte.hasRemaining()) {
|
||||||
|
bufferByte.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(bufferByte.position(), targetArray.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingCommonsIo_whenByteArrayInputStreamToAByteBuffer_thenLengthMustMatch() throws IOException {
|
||||||
|
byte[] input = new byte[] { 0, 1, 2 };
|
||||||
|
InputStream initialStream = new ByteArrayInputStream(input);
|
||||||
|
ByteBuffer byteBuffer = ByteBuffer.allocate(3);
|
||||||
|
ReadableByteChannel channel = newChannel(initialStream);
|
||||||
|
IOUtils.readFully(channel, byteBuffer);
|
||||||
|
|
||||||
|
assertEquals(byteBuffer.position(), input.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,248 @@
|
|||||||
|
package com.baeldung.inputstreamtostring;
|
||||||
|
|
||||||
|
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.Files;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class JavaInputStreamToXUnitTest {
|
||||||
|
protected final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
private static final int DEFAULT_SIZE = 1500000;
|
||||||
|
|
||||||
|
// tests - InputStream to String
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final 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;
|
||||||
|
while ((c = reader.read()) != -1) {
|
||||||
|
textBuilder.append((char) c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(textBuilder.toString(), originalString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
// When
|
||||||
|
String text;
|
||||||
|
try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) {
|
||||||
|
text = scanner.useDelimiter("\\A").next();
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(text, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
final ByteSource byteSource = new ByteSource() {
|
||||||
|
@Override
|
||||||
|
public final InputStream openStream() throws IOException {
|
||||||
|
return inputStream;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
final String text = byteSource.asCharSource(Charsets.UTF_8).read();
|
||||||
|
|
||||||
|
assertThat(text, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingGuavaAndJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
// When
|
||||||
|
String text;
|
||||||
|
try (final Reader reader = new InputStreamReader(inputStream)) {
|
||||||
|
text = CharStreams.toString(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(text, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCommonsIo_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
// When
|
||||||
|
final String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
|
||||||
|
assertThat(text, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
// When
|
||||||
|
final StringWriter writer = new StringWriter();
|
||||||
|
final String encoding = StandardCharsets.UTF_8.name();
|
||||||
|
IOUtils.copy(inputStream, writer, encoding);
|
||||||
|
|
||||||
|
assertThat(writer.toString(), equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingTempFile_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException {
|
||||||
|
final String originalString = randomAlphabetic(DEFAULT_SIZE);
|
||||||
|
final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
// When
|
||||||
|
Path tempFile = java.nio.file.Files.createTempDirectory("").resolve(UUID.randomUUID().toString() + ".tmp");
|
||||||
|
java.nio.file.Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
String result = new String(java.nio.file.Files.readAllBytes(tempFile));
|
||||||
|
|
||||||
|
assertThat(result, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
// tests - InputStream to byte[]
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingPlainJavaOnFixedSizeStream_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 givenUsingPlainJavaOnUnknownSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException {
|
||||||
|
final InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||||
|
|
||||||
|
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||||
|
int nRead;
|
||||||
|
final byte[] data = new byte[1024];
|
||||||
|
while ((nRead = is.read(data, 0, data.length)) != -1) {
|
||||||
|
buffer.write(data, 0, nRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.flush();
|
||||||
|
final byte[] byteArray = buffer.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 InputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
|
||||||
|
final byte[] targetArray = IOUtils.toByteArray(initialStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
// tests - InputStream to File
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenConvertingToFile_thenCorrect() throws IOException {
|
||||||
|
final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
|
||||||
|
final byte[] buffer = new byte[initialStream.available()];
|
||||||
|
initialStream.read(buffer);
|
||||||
|
|
||||||
|
final File targetFile = new File("src/test/resources/targetFile.tmp");
|
||||||
|
final OutputStream outStream = new FileOutputStream(targetFile);
|
||||||
|
outStream.write(buffer);
|
||||||
|
|
||||||
|
IOUtils.closeQuietly(initialStream);
|
||||||
|
IOUtils.closeQuietly(outStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenConvertingInProgressToFile_thenCorrect() throws IOException {
|
||||||
|
final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
|
||||||
|
final File targetFile = new File("src/test/resources/targetFile.tmp");
|
||||||
|
final OutputStream outStream = new FileOutputStream(targetFile);
|
||||||
|
|
||||||
|
final byte[] buffer = new byte[8 * 1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = initialStream.read(buffer)) != -1) {
|
||||||
|
outStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
IOUtils.closeQuietly(initialStream);
|
||||||
|
IOUtils.closeQuietly(outStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() throws IOException {
|
||||||
|
final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
|
||||||
|
final File targetFile = new File("src/test/resources/targetFile.tmp");
|
||||||
|
|
||||||
|
java.nio.file.Files.copy(initialStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
|
||||||
|
IOUtils.closeQuietly(initialStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenConvertingInputStreamToFile_thenCorrect3() throws IOException {
|
||||||
|
final InputStream initialStream = new FileInputStream(new File("src/test/resources/sample.txt"));
|
||||||
|
final byte[] buffer = new byte[initialStream.available()];
|
||||||
|
initialStream.read(buffer);
|
||||||
|
|
||||||
|
final File targetFile = new File("src/test/resources/targetFile.tmp");
|
||||||
|
Files.write(buffer, targetFile);
|
||||||
|
|
||||||
|
IOUtils.closeQuietly(initialStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException {
|
||||||
|
final InputStream initialStream = FileUtils.openInputStream(new File("src/test/resources/sample.txt"));
|
||||||
|
|
||||||
|
final File targetFile = new File("src/test/resources/targetFile.tmp");
|
||||||
|
|
||||||
|
FileUtils.copyInputStreamToFile(initialStream, targetFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingPlainJava_whenConvertingAnInputStreamToString_thenCorrect() throws IOException {
|
||||||
|
String originalString = randomAlphabetic(8);
|
||||||
|
InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
|
||||||
|
|
||||||
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||||
|
int nRead;
|
||||||
|
byte[] data = new byte[1024];
|
||||||
|
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
|
||||||
|
buffer.write(data, 0, nRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.flush();
|
||||||
|
byte[] byteArray = buffer.toByteArray();
|
||||||
|
|
||||||
|
String text = new String(byteArray, StandardCharsets.UTF_8);
|
||||||
|
assertThat(text, equalTo(originalString));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
Hello World
|
@ -0,0 +1 @@
|
|||||||
|
Hello World
|
Loading…
x
Reference in New Issue
Block a user