Search code examples
javajava-iofile-read

RandomAccessFile.read() returns value greater than number of characters read into string


I have a RandomAccessFile raFile from which I'm reading data into a buffer in fixed sized chunks:

byte[] fileBuffer = new byte[BUFFER_SIZE];

while((readBytes = raFile.read(fileBuffer) >= 0) {
    String bufferStr = new String(fileBuffer, 0, readBytes);
    String testerStr = new String(fileBuffer);

    System.out.println(readBytes+","+bufferStr.length()+","+testerStr.length());
}

What I had expected was for raFile.read() to read as many bytes as the BUFFER_SIZE (except at the end of the file) and the same value to be copied to readBytes. While this is mostly true, occasionally, I get the following outputs for a BUFFER_SIZE of 4096:

readBytes bufferStr testerStr
 4096             4092              4092
 4096             4090              4090
 4096             4094              4094
 4096             4095              4095

If 4096 bytes are being read, why is the length of bufferStr and testerStr less than this value even when not at file end?

Reference: This says read() returns the total number of bytes read into the buffer.


Solution

  • Because there are characters that need more than one byte. bufferStr.lenght() gives you the number of characters, not the number of bytes.