0

I have a TCP Socket implementation in Kotlin and use the code below to read a String (using a BufferedReader):

val readBuffer = BufferedReader(InputStreamReader(socket.inputStream))

private fun readLine(reader: BufferedReader): String? {
    val data = CharArray(4096)
    val count = reader.read(data)

    if(count < 0) {
        return null
    }

    return String(data, 0, count)
}

readLine(readBuffer)

My assumption here is that the message I'm going to receive never exceeds 4096 bytes (which is the case), but I was wondering whether this is the correct approach? What happens when the message would exceed 4096 bytes? Is there any other value I could use to initialise the CharArray? I know InputStream has an available() method which returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream [1]. But as it's an estimation, I am not confident is safe to use in this case.

Any insights appreciated!

[1] https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F/java/io/InputStream.html#available()

6
  • 2
    There are no messages in TCP. It's a byte-stream protocol. Messages are up to you to implement. There is no reason therefore why 4096 or 8192 or any other buffer size should not suffice. You have to reassemble messages anyway, which necessarily involves another buffer of some kind. e.g. a StringBuilder, ByteArrayOutputStream, etc. Commented Jun 18, 2024 at 10:05
  • Clear, thank you. But in the case I pass read() a CharArray with a size of 4096, and more data is available (e.g. 6144 bytes), what will be the expected behaviour? Will the read fill my CharArray with 4096 bytes, return 4096 and give the next 4096 bytes on a second read() (which will return 2048)? Or will there be an Overflow exception on the first read() (because more bytes are available and my CharArray can't store them)? Commented Jun 18, 2024 at 10:33
  • 2
    You can read the documentation to get some details, but I guess it's not completely clear. reader.read(data) will place into data no more characters than the length of data, and it will return how many characters it put in data. If more data is available, you will have to call read again. This is usually done in a loop. When EOF or end of stream is reached, nothing will be written into data and -1 is returned. No exception is thrown. Commented Jun 18, 2024 at 18:35
  • 1
    The only way itread can return 0 is if you give it a zero-length array as an argument. Otherwise, it will block until some data is available. Commented Jun 18, 2024 at 18:38
  • Thanks for the valuable input @PresidentJamesK.Polk! Very clear now. Commented Jun 19, 2024 at 10:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.