From f6d74e0c86dc7b4c97d49ef3eb0e40e74ca1eb54 Mon Sep 17 00:00:00 2001 From: Alexander Molochko Date: Sun, 7 Apr 2019 00:18:30 +0300 Subject: [PATCH 1/4] InputStream to String in Kotlin BAEL-2654 --- .../inputstream/InputStreamExtension.kt | 17 ++++++++ .../inputstream/InputStreamToStringTest.kt | 43 +++++++++++++++++++ .../src/test/resources/inputstream2string.txt | 2 + 3 files changed, 62 insertions(+) create mode 100644 core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt create mode 100644 core-kotlin-2/src/test/resources/inputstream2string.txt diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt new file mode 100644 index 0000000000..2f765b0193 --- /dev/null +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt @@ -0,0 +1,17 @@ +package com.baeldung.inputstream + +import java.io.InputStream + +fun InputStream.readUpToNullChar(nullChar: Char = '\n'): String { + val stringBuilder = StringBuilder() + var currentChar = this.read().toChar() + while (currentChar != nullChar) { + stringBuilder.append(currentChar) + currentChar = this.read().toChar() + if (this.available() <= 0) { + stringBuilder.append(currentChar) + break + } + } + return stringBuilder.toString() +} \ No newline at end of file diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt new file mode 100644 index 0000000000..93a2dc2fa4 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt @@ -0,0 +1,43 @@ +package com.baeldung.range + +import com.baeldung.inputstream.readUpToNullChar +import kotlinx.io.core.use +import org.junit.Test +import java.io.BufferedReader +import java.io.File +import kotlin.test.assertEquals + +class InputStreamToStringTest { + private val fileName = "src/test/resources/inputstream2string.txt" + private val fileFullContent = "Computer programming can be a hassle\r\n" + + "It's like trying to take a defended castle" + private val fileContentUpToNullChar = "Computer programming can be a hassle\r\n" + + "It" + + @Test + fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() { + val file = File(fileName) + val inputStream = file.inputStream() + val content = inputStream.bufferedReader().use(BufferedReader::readText) + assertEquals(fileFullContent, content) + } + + @Test + fun whenReadFileUpToNullChar_thenPartBeforeNullCharIsReadAsString() { + val file = File(fileName) + val inputStream = file.inputStream() + val content = inputStream.use { it.readUpToNullChar('\'') } + assertEquals(fileContentUpToNullChar, content) + } + + @Test + fun whenReadFileWithoutContainingNullChar_thenFullFileContentIsReadAsString() { + val file = File(fileName) + val inputStream = file.inputStream() + val content = inputStream.use { it.readUpToNullChar('-') } + assertEquals(fileFullContent, content) + } + + +} + diff --git a/core-kotlin-2/src/test/resources/inputstream2string.txt b/core-kotlin-2/src/test/resources/inputstream2string.txt new file mode 100644 index 0000000000..40ef9fc5f3 --- /dev/null +++ b/core-kotlin-2/src/test/resources/inputstream2string.txt @@ -0,0 +1,2 @@ +Computer programming can be a hassle +It's like trying to take a defended castle \ No newline at end of file From a707c0c95918e8d0751cd5890d3489cfe58af20f Mon Sep 17 00:00:00 2001 From: Alexander Molochko Date: Sun, 7 Apr 2019 00:27:48 +0300 Subject: [PATCH 2/4] Update package name --- .../kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt index 93a2dc2fa4..d7f0401af0 100644 --- a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt @@ -1,6 +1,5 @@ -package com.baeldung.range +package com.baeldung.inputstream -import com.baeldung.inputstream.readUpToNullChar import kotlinx.io.core.use import org.junit.Test import java.io.BufferedReader From 990610ee292f73e3794338dd1f4b96bd192e106e Mon Sep 17 00:00:00 2001 From: Alexander Molochko Date: Mon, 8 Apr 2019 16:37:34 +0300 Subject: [PATCH 3/4] Add manual example, fix names --- .../inputstream/InputStreamExtension.kt | 7 +++-- .../inputstream/InputStreamToStringTest.kt | 31 +++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt b/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt index 2f765b0193..e94a2e84ee 100644 --- a/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt +++ b/core-kotlin-2/src/main/kotlin/com/baeldung/inputstream/InputStreamExtension.kt @@ -2,10 +2,10 @@ package com.baeldung.inputstream import java.io.InputStream -fun InputStream.readUpToNullChar(nullChar: Char = '\n'): String { +fun InputStream.readUpToChar(stopChar: Char): String { val stringBuilder = StringBuilder() var currentChar = this.read().toChar() - while (currentChar != nullChar) { + while (currentChar != stopChar) { stringBuilder.append(currentChar) currentChar = this.read().toChar() if (this.available() <= 0) { @@ -14,4 +14,5 @@ fun InputStream.readUpToNullChar(nullChar: Char = '\n'): String { } } return stringBuilder.toString() -} \ No newline at end of file +} + diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt index d7f0401af0..0a053d61ec 100644 --- a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt @@ -10,7 +10,7 @@ class InputStreamToStringTest { private val fileName = "src/test/resources/inputstream2string.txt" private val fileFullContent = "Computer programming can be a hassle\r\n" + "It's like trying to take a defended castle" - private val fileContentUpToNullChar = "Computer programming can be a hassle\r\n" + + private val fileContentUpToStopChar = "Computer programming can be a hassle\r\n" + "It" @Test @@ -22,18 +22,37 @@ class InputStreamToStringTest { } @Test - fun whenReadFileUpToNullChar_thenPartBeforeNullCharIsReadAsString() { + fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() { val file = File(fileName) val inputStream = file.inputStream() - val content = inputStream.use { it.readUpToNullChar('\'') } - assertEquals(fileContentUpToNullChar, content) + val reader = BufferedReader(inputStream.reader()) + val content = StringBuilder() + try { + var line = reader.readLine() + while (line != null) { + content.append(line) + line = reader.readLine() + } + } finally { + reader.close() + } + assertEquals(fileFullContent.replace("\r\n", ""), content.toString()) + } @Test - fun whenReadFileWithoutContainingNullChar_thenFullFileContentIsReadAsString() { + fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() { val file = File(fileName) val inputStream = file.inputStream() - val content = inputStream.use { it.readUpToNullChar('-') } + val content = inputStream.use { it.readUpToChar('\'') } + assertEquals(fileContentUpToStopChar, content) + } + + @Test + fun whenReadFileWithoutContainingStopChar_thenFullFileContentIsReadAsString() { + val file = File(fileName) + val inputStream = file.inputStream() + val content = inputStream.use { it.readUpToChar('-') } assertEquals(fileFullContent, content) } From 139e2819d7a2d653a76e0fc7fd2248a5f1cabe6e Mon Sep 17 00:00:00 2001 From: Alexander Molochko Date: Wed, 10 Apr 2019 13:08:00 +0300 Subject: [PATCH 4/4] Add readtext example --- .../inputstream/InputStreamToStringTest.kt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt index 0a053d61ec..3437ddb68a 100644 --- a/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/inputstream/InputStreamToStringTest.kt @@ -10,8 +10,6 @@ class InputStreamToStringTest { private val fileName = "src/test/resources/inputstream2string.txt" private val fileFullContent = "Computer programming can be a hassle\r\n" + "It's like trying to take a defended castle" - private val fileContentUpToStopChar = "Computer programming can be a hassle\r\n" + - "It" @Test fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() { @@ -20,7 +18,19 @@ class InputStreamToStringTest { val content = inputStream.bufferedReader().use(BufferedReader::readText) assertEquals(fileFullContent, content) } - + @Test + fun whenReadFileWithBufferedReaderReadText_thenFullFileContentIsReadAsString() { + val file = File(fileName) + val inputStream = file.inputStream() + val reader = BufferedReader(inputStream.reader()) + var content: String + try { + content = reader.readText() + } finally { + reader.close() + } + assertEquals(fileFullContent, content) + } @Test fun whenReadFileWithBufferedReaderManually_thenFullFileContentIsReadAsString() { val file = File(fileName) @@ -44,8 +54,8 @@ class InputStreamToStringTest { fun whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() { val file = File(fileName) val inputStream = file.inputStream() - val content = inputStream.use { it.readUpToChar('\'') } - assertEquals(fileContentUpToStopChar, content) + val content = inputStream.use { it.readUpToChar(' ') } + assertEquals("Computer", content) } @Test