Skip to content

Commit

Permalink
Fix apache#1411 Java Locale use '_' split language, country, variant.
Browse files Browse the repository at this point in the history
  • Loading branch information
takeseem committed Mar 1, 2018
1 parent 3be23d9 commit 844d7c7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,57 +61,30 @@ public LocaleHandle(String locale) {
}

private Object readResolve() {
String s = this.value;

if (s == null)
if (value == null) {
return null;

int len = s.length();
char ch = ' ';

int i = 0;
for (;
i < len && ('a' <= (ch = s.charAt(i)) && ch <= 'z'
|| 'A' <= ch && ch <= 'Z'
|| '0' <= ch && ch <= '9');
i++) {
}

String language = s.substring(0, i);
String country = null;
String var = null;

if (ch == '-' || ch == '_') {
int head = ++i;

for (;
i < len && ('a' <= (ch = s.charAt(i)) && ch <= 'z'
|| 'A' <= ch && ch <= 'Z'
|| '0' <= ch && ch <= '9');
i++) {
}

country = s.substring(head, i);
if (value.length() == 0) {
return new Locale("");
}

if (ch == '-' || ch == '_') {
int head = ++i;
int extStart = value.indexOf("_#");
if (extStart != -1) value = value.substring(0, extStart);

for (;
i < len && ('a' <= (ch = s.charAt(i)) && ch <= 'z'
|| 'A' <= ch && ch <= 'Z'
|| '0' <= ch && ch <= '9');
i++) {
}
String language = value, country = "", variant = "";
int pos1 = value.indexOf('_');
if (pos1 != -1) {
language = value.substring(0, pos1++);

var = s.substring(head, i);
int pos2 = value.indexOf('_', pos1);
if (pos2 == -1) {
country = value.substring(pos1);
} else {
country = value.substring(pos1, pos2);
variant = value.substring(pos2 + 1);
}
}

if (var != null)
return new Locale(language, country, var);
else if (country != null)
return new Locale(language, country);
else
return new Locale(language);
return new Locale(language, country, variant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.alibaba.com.caucho.hessian.io;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Locale;

import org.junit.Test;

public class LocaleSerializerTest {

/** {@linkplain LocaleSerializer#writeObject(Object, AbstractHessianOutput)} */
@Test
public void locale() throws IOException {
assertLocale(null);
assertLocale(new Locale(""));
assertLocale(new Locale("zh"));
assertLocale(new Locale("zh", "CN"));
assertLocale(new Locale("zh-hant", "CN"));
assertLocale(new Locale("zh-hant", "CN", "xx"));
}

private void assertLocale(Locale loc) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output out = new Hessian2Output(bout);

out.writeObject(loc);
out.flush();

ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
Hessian2Input input = new Hessian2Input(bin);
assertEquals(loc, input.readObject());
}

}

0 comments on commit 844d7c7

Please sign in to comment.