Skip to content

Commit

Permalink
Fix locale errors due to inconsistent mark/reset support
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgehog1029 committed May 10, 2022
1 parent 23e01de commit 90ff8df
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import lombok.extern.java.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
Expand All @@ -30,27 +27,32 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format, C
return null;
}

BufferedInputStream bis = new BufferedInputStream(is);

// Let's do the timewalk
boolean isUtf8;
is.mark(3);
bis.mark(3);
{
byte[] buf = new byte[3];
int read = 0;
while (read < 3) {
read = is.read(buf);
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;
}

// the BOM is 0xEF,0xBB,0xBF
isUtf8 = buf[0] == (byte) 0xEF && buf[1] == (byte) 0xBB && buf[2] == (byte) 0xBF;
}
is.reset();
bis.reset();

if (isUtf8) {
log.info("Found UTF-8 locale file " + resourceName);
}

Charset charset = isUtf8 ? StandardCharsets.UTF_8 : StandardCharsets.ISO_8859_1;
Reader reader = new InputStreamReader(is, charset);
Reader reader = new InputStreamReader(bis, charset);

try {
PropertyResourceBundle bundle = new PropertyResourceBundle(reader);
Expand Down

0 comments on commit 90ff8df

Please sign in to comment.