Skip to content

Commit

Permalink
#326 Fix IOOB
Browse files Browse the repository at this point in the history
  • Loading branch information
guanquan.wang committed Jan 5, 2023
1 parent 939028b commit 3baf549
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/java/org/ttzero/excel/entity/I18N.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,51 @@
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

/**
* @author guanquan.wang on 2018-10-13
*/
public class I18N {
private final Properties pro;
/**
* Here converted to HashMap is to improve performance,
* Properties extends to Hashtable All methods are synchronous methods,
* I18N only needs to read without writing, so converting to HashMap can greatly improve performance
*/
private Map<String, String> pro;

public I18N() {
init();
}

void init() {
Locale locale = Locale.getDefault();
String fn = "message._.properties";
pro = new Properties();
try {
InputStream is = I18N.class.getClassLoader().getResourceAsStream("I18N/" + fn.replace("_", locale.toLanguageTag()));
if (is == null) {
is = I18N.class.getClassLoader().getResourceAsStream("I18N/" + fn.replace("_", "zh-CN"));
}
if (is != null) {
try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
Properties pro = new Properties();
pro.load(reader);
Map<String, String> map = new HashMap<>(pro.size());
for (Map.Entry<Object, Object> entry : pro.entrySet()) {
map.put(entry.getKey().toString(), entry.getValue().toString());
}
this.pro = map;
}
}
// FIXME classpath
} catch (IOException e) {
// nothing...
}

if (pro == null) pro = new HashMap<>();
}

/**
Expand All @@ -57,7 +76,7 @@ public I18N() {
* @return I18N string
*/
public String get(String code) {
return pro.getProperty(code, code);
return pro.getOrDefault(code, code);
}

/**
Expand All @@ -68,7 +87,7 @@ public String get(String code) {
* @return I18N string
*/
public String getOrElse(String code, String other) {
return pro.getProperty(code, other);
return pro.getOrDefault(code, other);
}

/**
Expand All @@ -79,7 +98,7 @@ public String getOrElse(String code, String other) {
* @return I18N string
*/
public String get(String code, String... args) {
String msg = pro.getProperty(code, code);
String msg = pro.getOrDefault(code, code);
char[] oldValue = msg.toCharArray();
int[] indexs = search(oldValue);
int len = Math.min(indexs.length, args.length), size = 0;
Expand Down

0 comments on commit 3baf549

Please sign in to comment.