Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly does bufferedReader() work in Kotlin?

So I'm trying to read data from a .json file in my Android project:

val file = context.assets.open("myfile.json").bufferedReader().readText()

This works fine and successfully prints out my .json file as one String.

However I want to know what exactly is bufferedReader() and why .readText() can't be called directly on the opened .json file.

PS: readText() returns a String. However:

val json2: JSONObject = JSONObject("mystring") returns:

Caused by: org.json.JSONException: Value mystring of type java.lang.String cannot be converted to JSONObject

How does this make sense?

like image 492
Zorgan Avatar asked Apr 04 '19 08:04

Zorgan


People also ask

What is BufferedReader in Kotlin?

BufferedReader(in: Reader!) Creates a buffering character-input stream that uses a default-sized input buffer.

Why does BufferedReader throw IOException?

It may occur due to the file deleted or viruses in the file. Sometimes BufferedReader takes data from a network stream where the reading system can fail at any time. So this type of error can occur in input operation when a BufferedReader is used. This is why a buffered reader throws IOException.

What is the BufferedReader?

The BufReader<R> struct adds buffering to any reader. It can be excessively inefficient to work directly with a Read instance. For example, every call to read on TcpStream results in a system call. A BufReader<R> performs large, infrequent reads on the underlying Read and maintains an in-memory buffer of the results.

How do you read lines in Kotlin?

To read a line of string in Kotlin, you can use readline() function.


1 Answers

The readText function is defined as an extension on Reader:

public fun Reader.readText(): String {
    val buffer = StringWriter()
    copyTo(buffer)
    return buffer.toString()
}

An InputStream isn't a Reader, so you have to transform it into some Reader:

public inline fun InputStream.reader(charset: Charset = Charsets.UTF_8): InputStreamReader = 
    InputStreamReader(this, charset)

You can use the reader as a buffered reader with the alternative bufferedReader function:

public inline fun InputStream.bufferedReader(charset: Charset = Charsets.UTF_8): BufferedReader = 
    reader(charset).buffered()

Reader and also BufferedReader are part of the Java standard library and the buffered version is described like this:

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders...

It basically wraps a Reader and adds support for reading single lines etc.

like image 67
s1m0nw1 Avatar answered Sep 24 '22 14:09

s1m0nw1