Kotlin stream to string

In Kotlin, how do I read the entire contents of an InputStream into a String?

Kotlin has a specific extension just for this purpose.

The simplest:

val inputAsString = input.bufferedReader().use < it.readText() >// defaults to UTF-8 

And in this example, you could decide between bufferedReader() or just reader() . The call to the function Closeable.use() will automatically close the input at the end of the lambda’s execution.

Further reading:

If you do this type of thing a lot, you could write this as an extension function:

fun InputStream.readTextAndClose(charset: Charset = Charsets.UTF_8): String < return this.bufferedReader(charset).use < it.readText() >> 

Which you could then call easily as:

val inputAsString = input.readTextAndClose() // defaults to UTF-8 

On a side note, all Kotlin extension functions that require knowing the charset already default to UTF-8 , so if you require a different encoding you need to adjust the code above in calls to include encoding for reader(charset) or bufferedReader(charset) .

Warning: You might see examples that are shorter:

val inputAsString = input.reader().readText() 

But these do not close the stream. Make sure you check the API documentation for all of the IO functions you use to be sure which ones close and which do not. Usually, if they include the word use (such as useLines() or use() ) they close the stream after. An exception is that File.readText() differs from Reader.readText() in that the former does not leave anything open and the latter does indeed require an explicit close.

See also: Kotlin IO related extension functions

【Method 1 | Manually Close Stream】

private fun getFileText(uri: Uri):String < val inputStream = contentResolver.openInputStream(uri)!! val bytes = inputStream.readBytes() //see below val text = String(bytes, StandardCharsets.UTF_8) //specify charset inputStream.close() return text >
  • inputStream.readBytes() requires manually close the stream: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-input-stream/read-bytes.html

【Method 2 | Automatically Close Stream】

private fun getFileText(uri: Uri): String < return contentResolver.openInputStream(uri). bufferedReader().use > 
  • You can specify the charset inside bufferedReader() , default is UTF-8 : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-input-stream/buffered-reader.html
  • bufferedReader() is an upgrade version of reader() , it is more versatile: How exactly does bufferedReader() work in Kotlin?
  • use() can automatically close the stream when the block is done: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/use.html

An example that reads contents of an InputStream to a String

import java.io.File import java.io.InputStream import java.nio.charset.Charset fun main(args: Array)

For Reference — Kotlin Read File

Источник

kotlin-inputstream-to-string

Kotlin provides an easy way to perform the conversion. However, there are still some nuances to consider when working with resources. Plus, we’ll cover special cases, like reading up to a stop character.

2. Buffered Reader

InputStream is an abstraction around an ordered stream of bytes. An underlying data source can be a file, a network connection or any other source emitting bytes. Let’s use a simple file that contains the following data:

Computer programming can be a hassle It's like trying to take a defended castle

The first solution that we might try is to read the file manually line by line:

val reader = BufferedReader(inputStream.reader()) val content = StringBuilder() try < var line = reader.readLine() while (line != null) < content.append(line) line = reader.readLine() >> finally

First, we used the BufferedReader class to wrap the InputStream and then read until no lines left in the stream. Furthermore, we surrounded reading logic by the try-finally statement to finally close the stream. Altogether, there’s a lot of boilerplate code.

Could we make it more compact and readable?

Absolutely! At first, we can simplify the snippet by using the readText() function. It reads the input stream completely as a String. Accordingly, we can refactor our snippet as follows:

val reader = BufferedReader(inputStream.reader()) var content: String try < content = reader.readText() >finally

However, we still have that try-finally block. Fortunately, Kotlin allows handling resource management in a pseudo-automatic fashion. Let’s look at the next code lines:

val content = inputStream.bufferedReader().use(BufferedReader::readText) assertEquals(fileFullContent, content)

This one-line solution looks simple, nevertheless, a lot is happening under the hood. One important point in the code above is the call of the use() function. This extension function executes a block on a resource that implements the Closable interface. Finally, when the block is executed Kotlin closes the resource for us.

3. Stop Character

At the same time, there might be a case when we need to read content up to a specific character. Let’s define an extension function for the InputStream class:

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() > return stringBuilder.toString() >

This function reads bytes from an input stream until a stop character appears. At the same time, in order to prevent the infinite loop, we call the available() method to check whether the stream has any data left. So, if there is no stop character in a stream, then a whole stream will be read.

On the other hand, not all subclasses of the InputStream class provide an implementation for the available() method. Consequently, we have to ensure that the method is implemented correctly before using the extension function.

Let’s get back to our example and read text up to the first whitespace character (‘ ‘):

val content = inputStream.use < it.readUpToChar(' ') >assertEquals("Computer", content)

As a result, we’ll get the text up to the stop character. In the same way, don’t forget to wrap the block with the use() function to automatically close the stream.

4. Conclusion

In this article, we’ve seen how to convert an InputStream to a String in Kotlin. Kotlin provides a concise way to work with streams of data, but it’s always worth knowing what is going on internally.

As usual, the implementation of all these examples is over on Github.

Источник

Kotlin Program to Convert InputStream to String

In the above program, the input stream is created from a String and stored in a variable stream . We also require a string builder sb to create the string from the stream.

Then, we created a buffered reader br from the InputStreamReader to read the lines from the stream . Using a while loop, we read each line and append it to the string builder. Finally, we closed the bufferedReader.

Since, the reader can throw IOException , we have the throws IOException in the main function as:

public static void main(String[] args) throws IOException

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) .

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains. 

Источник

Читайте также:  Основы PHP и MySQL
Оцените статью