Skip to content

Commit

Permalink
Fix stream-reading logic in locale control
Browse files Browse the repository at this point in the history
It's pretty likely that we would always read 3 bytes but let's do it
properly
  • Loading branch information
hedgehog1029 committed May 10, 2022
1 parent 90ff8df commit b8a6769
Showing 1 changed file with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format, C
bis.mark(3);
{
byte[] buf = new byte[3];
int nread = 0;
while (nread < 3) {
int read = bis.read(buf);

if (read == -1) throw new EOFException("Locale file is truncated or empty!");
nread += read;
}
readFully(bis, buf);

// the BOM is 0xEF,0xBB,0xBF
isUtf8 = buf[0] == (byte) 0xEF && buf[1] == (byte) 0xBB && buf[2] == (byte) 0xBF;
Expand Down Expand Up @@ -80,4 +74,19 @@ private InputStream getResourceAsStream(String resourceName, ClassLoader loader,

return loader.getResourceAsStream(resourceName);
}

private void readFully(InputStream is, byte[] buf) throws IOException {
int offset = 0;
int length = buf.length;

while (length > 0) {
int n = is.read(buf, offset, length);
if (n == -1) {
throw new EOFException();
}

offset += n;
length -= n;
}
}
}

0 comments on commit b8a6769

Please sign in to comment.