Skip to content

Commit

Permalink
Make sure all bytes have been read
Browse files Browse the repository at this point in the history
  • Loading branch information
schmika authored and jbaiter committed Jun 17, 2024
1 parent 635af2b commit 6b9f210
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ public String readUtf8String(int start, int byteLen) throws IOException {
byteLen = this.length() - start;
}
byte[] data = new byte[byteLen];
this.readBytes(data, 0, start, byteLen);
int numRead = 0;
while (numRead < byteLen) {
numRead += this.readBytes(data, numRead, start + numRead, byteLen - numRead);
}
int dataStart = adjustOffset(0, data, AdjustDirection.RIGHT);
int dataEnd = adjustOffset(data.length - 1, data, AdjustDirection.LEFT);
return new String(data, dataStart, dataEnd - dataStart + 1, StandardCharsets.UTF_8);
Expand Down Expand Up @@ -213,7 +216,10 @@ public Section getAsciiSection(int offset) throws IOException {
}
int startOffset = sectionIndex * sectionSize;
int readLen = Math.min(sectionSize, this.length() - startOffset);
this.readBytes(copyBuf, 0, startOffset, readLen);
int numRead = 0;
while(numRead < readLen) {
numRead += this.readBytes(copyBuf, 0, startOffset, readLen);
}
// Construct a String without going through a decoder to save on CPU.
// Given that the method has been deprecated since Java 1.1 and was never removed, I don't think
// this is very risky 😅
Expand Down

0 comments on commit 6b9f210

Please sign in to comment.