From 0269c686d7ded41d5a0ffa14daf1e7349ee933f6 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sun, 14 Nov 2021 13:20:22 +0530 Subject: [PATCH] JAVA-8365: Split or move core-java-io-conversions module --- .../core-java-io-conversions-2/README.md | 1 + .../JavaXToInputStreamUnitTest.java | 44 +++++++++++++++++++ .../core-java-io-conversions/README.md | 1 - .../JavaXToInputStreamUnitTest.java | 41 +++++------------ 4 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/stringtoinputstream/JavaXToInputStreamUnitTest.java diff --git a/core-java-modules/core-java-io-conversions-2/README.md b/core-java-modules/core-java-io-conversions-2/README.md index b2153b3669..9abb539066 100644 --- a/core-java-modules/core-java-io-conversions-2/README.md +++ b/core-java-modules/core-java-io-conversions-2/README.md @@ -4,6 +4,7 @@ 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 String to InputStream](https://www.baeldung.com/convert-string-to-input-stream) - [Java – Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file) - [Converting a BufferedReader to a JSONObject](https://www.baeldung.com/java-bufferedreader-to-jsonobject) - [Reading a CSV File into an Array](https://www.baeldung.com/java-csv-file-array) diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/stringtoinputstream/JavaXToInputStreamUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/stringtoinputstream/JavaXToInputStreamUnitTest.java new file mode 100644 index 0000000000..7e7c79f764 --- /dev/null +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/stringtoinputstream/JavaXToInputStreamUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.stringtoinputstream; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.io.CharSource; + +public class JavaXToInputStreamUnitTest { + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + // tests - String - InputStream + + @Test + public final void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "text"; + final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes()); + + IOUtils.closeQuietly(targetStream); + } + + @Test + public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "text"; + final InputStream targetStream = CharSource.wrap(initialString).asByteSource(StandardCharsets.UTF_8).openStream(); + + IOUtils.closeQuietly(targetStream); + } + + @Test + public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException { + final String initialString = "text"; + final InputStream targetStream = IOUtils.toInputStream(initialString); + + IOUtils.closeQuietly(targetStream); + } + +} diff --git a/core-java-modules/core-java-io-conversions/README.md b/core-java-modules/core-java-io-conversions/README.md index 52f5222040..1f12c87241 100644 --- a/core-java-modules/core-java-io-conversions/README.md +++ b/core-java-modules/core-java-io-conversions/README.md @@ -13,5 +13,4 @@ This module contains articles about core Java input/output(IO) conversions. - [Java – Write a Reader to File](https://www.baeldung.com/java-write-reader-to-file) - [Java – Reader to Byte Array](https://www.baeldung.com/java-convert-reader-to-byte-array) - [Java – Reader to InputStream](https://www.baeldung.com/java-convert-reader-to-inputstream) -- [Java String to InputStream](https://www.baeldung.com/convert-string-to-input-stream) - More articles: [[next -->]](/core-java-modules/core-java-io-conversions-2) diff --git a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/filetoinputstream/JavaXToInputStreamUnitTest.java b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/filetoinputstream/JavaXToInputStreamUnitTest.java index c20752639f..9d061307ce 100644 --- a/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/filetoinputstream/JavaXToInputStreamUnitTest.java +++ b/core-java-modules/core-java-io-conversions/src/test/java/com/baeldung/filetoinputstream/JavaXToInputStreamUnitTest.java @@ -1,46 +1,25 @@ package com.baeldung.filetoinputstream; -import com.google.common.io.ByteSource; -import com.google.common.io.CharSource; -import com.google.common.io.Files; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.SequenceInputStream; + 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.StandardCharsets; +import com.google.common.io.ByteSource; +import com.google.common.io.Files; public class JavaXToInputStreamUnitTest { protected final Logger logger = LoggerFactory.getLogger(getClass()); - // tests - String - InputStream - - @Test - public final void givenUsingPlainJava_whenConvertingStringToInputStream_thenCorrect() throws IOException { - final String initialString = "text"; - final InputStream targetStream = new ByteArrayInputStream(initialString.getBytes()); - - IOUtils.closeQuietly(targetStream); - } - - @Test - public final void givenUsingGuava_whenConvertingStringToInputStream_thenCorrect() throws IOException { - final String initialString = "text"; - final InputStream targetStream = CharSource.wrap(initialString).asByteSource(StandardCharsets.UTF_8).openStream(); - - IOUtils.closeQuietly(targetStream); - } - - @Test - public final void givenUsingCommonsIO_whenConvertingStringToInputStream_thenCorrect() throws IOException { - final String initialString = "text"; - final InputStream targetStream = IOUtils.toInputStream(initialString); - - IOUtils.closeQuietly(targetStream); - } - // byte array - InputStream @Test