From what? A BufferedSource/Buffer? It's not meant...
# squarelibraries
j
From what? A BufferedSource/Buffer? It's not meant for random access so reading will consume. You use a peeked reader by calling peek() and then skip(offset).
l
It's a
BufferedSource
but I think that hint helped me already. Thanks :)
@jw I have another problem. When I skip to the offset the bytes I read afterwards are not the expected ones. I tried to print the size of the buffer and noticed it's 0 first and becomes 8192, but my file is of size 500 MB:
Copy code
val file = File("D:\\Test\\Infos.pak") // 500 MB

    file.source().use { fileSource ->
        fileSource.buffer().use { bufferedSource ->
            val buffer = bufferedSource.peek()
            println("Size: ${buffer.buffer.size}") // prints 0
            while (true) {
                buffer.skip(12)
                println("Size: ${buffer.buffer.size}") // prints 8180

                val offsetFiles = buffer.readLongLe()
                println("offsetFiles: $offsetFiles")
                buffer.skip(0x2BCB61)
                println("next: ${Integer.toHexString(buffer.readInt())}") // printed bytes are false
                break
            }
        }
    }
Why does it print 0 first and after skip 8180? I found the description to Buffer class:
This buffer grows with your data. Just like ArrayList, each buffer starts small. It consumes only the memory it needs to.
Is 8192 bytes the max size a buffer can hold? Then I'm wondering how to read the 500 MB file once?
j
You can't, and perhaps you shouldn't
More memory efficient to stream through the file
l
I'm not so familiar with data streams. I guess you mean using inputstream?:
Copy code
bufferedSource.inputStream()
Btw, it's for a desktop app
j
Have you tried RandomAccessFile?
j
I don't think so, no
👍 1