Add manual example, fix names
This commit is contained in:
parent
a707c0c959
commit
990610ee29
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue