Create Core Kotlin IO module
This commit is contained in:
11
core-kotlin-io/.gitignore
vendored
Normal file
11
core-kotlin-io/.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
/bin/
|
||||
|
||||
#ignore gradle
|
||||
.gradle/
|
||||
|
||||
|
||||
#ignore build and generated files
|
||||
build/
|
||||
node/
|
||||
target/
|
||||
out/
|
||||
3
core-kotlin-io/README.md
Normal file
3
core-kotlin-io/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## Relevant articles:
|
||||
|
||||
- [InputStream to String in Kotlin](https://www.baeldung.com/kotlin-inputstream-to-string)
|
||||
78
core-kotlin-io/pom.xml
Normal file
78
core-kotlin-io/pom.xml
Normal file
@@ -0,0 +1,78 @@
|
||||
<?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-kotlin-io</artifactId>
|
||||
<name>core-kotlin-io</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-kotlin</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../parent-kotlin</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<kotlin.version>1.2.71</kotlin.version>
|
||||
<junit.platform.version>1.1.1</junit.platform.version>
|
||||
<junit.vintage.version>5.2.0</junit.vintage.version>
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.inputstream
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
fun InputStream.readUpToChar(stopChar: Char): String {
|
||||
val stringBuilder = StringBuilder()
|
||||
var currentChar = this.read().toChar()
|
||||
while (currentChar != stopChar) {
|
||||
stringBuilder.append(currentChar)
|
||||
currentChar = this.read().toChar()
|
||||
if (this.available() <= 0) {
|
||||
stringBuilder.append(currentChar)
|
||||
break
|
||||
}
|
||||
}
|
||||
return stringBuilder.toString()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.baeldung.inputstream
|
||||
|
||||
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"
|
||||
|
||||
@Test
|
||||
fun whenReadFileWithBufferedReader_thenFullFileContentIsReadAsString() {
|
||||
val file = File(fileName)
|
||||
val inputStream = file.inputStream()
|
||||
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)
|
||||
val inputStream = file.inputStream()
|
||||
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 whenReadFileUpToStopChar_thenPartBeforeStopCharIsReadAsString() {
|
||||
val file = File(fileName)
|
||||
val inputStream = file.inputStream()
|
||||
val content = inputStream.use { it.readUpToChar(' ') }
|
||||
assertEquals("Computer", content)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenReadFileWithoutContainingStopChar_thenFullFileContentIsReadAsString() {
|
||||
val file = File(fileName)
|
||||
val inputStream = file.inputStream()
|
||||
val content = inputStream.use { it.readUpToChar('-') }
|
||||
assertEquals(fileFullContent, content)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
2
core-kotlin-io/src/test/resources/inputstream2string.txt
Normal file
2
core-kotlin-io/src/test/resources/inputstream2string.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Computer programming can be a hassle
|
||||
It's like trying to take a defended castle
|
||||
Reference in New Issue
Block a user