Oleh Ponomarenko
12/06/2019, 2:23 PMval bytes = fileOut.readBytes().sliceArray(range)
And then I send the bytes. I do nothing with this after. As I understand JVM garbage collector should clean this data myself after a while but does it not always.
I google this, I think I can use something like try-with-resources in Java - use
. But this extension is unable for the Class.
Do you have some advice for me? Thanks.karelpeeters
12/06/2019, 2:26 PMOleh Ponomarenko
12/06/2019, 2:27 PMThis method is not recommended on huge files. It has an internal limitation of 2 GB byte array size.
karelpeeters
12/06/2019, 2:28 PMOleh Ponomarenko
12/06/2019, 2:28 PMkarelpeeters
12/06/2019, 2:38 PMDataInputStream
which is a bit more ergonomic:
val file = FileInputStream("test.txt")
val data = DataInputStream(file)
val result = ByteArray(100)
data.readFully(result, 1000, result.size)
1000 until 1100
. Be careful about character encodings or unexpected eofs of course.Oleh Ponomarenko
12/06/2019, 2:41 PMkarelpeeters
12/06/2019, 2:41 PMOleh Ponomarenko
12/06/2019, 2:43 PMHolger Steinhauer [Mod]
12/06/2019, 4:09 PMFor example, if you’re parsing binary data and you don’t want to read the entire file at once, you must create an input stream (for binary data) or an input stream reader (for strings) - the example below will read 16 bytes:
karelpeeters
12/06/2019, 4:13 PMfile.read(bytes)
does not read 16 bytes, it reads up to 16 bytes.skip
to get where you want to be, and then repeatedly call read
in a loop until you've read enough bytes. That's exactly what DataInputStream.readFully
does.