Skip to content

Commit

Permalink
Fix apache#1411 Locale deserialize for java {language}_{country}_({va…
Browse files Browse the repository at this point in the history
…riant}_#

| #){script}-{extensions}
  • Loading branch information
takeseem committed Feb 28, 2018
1 parent 3be23d9 commit e7968e5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ public LocaleHandle(String locale) {
private Object readResolve() {
String s = this.value;

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

Locale locale = deJavaLocale(this.value);
if (locale != null) {
return locale;
}

int len = s.length();
char ch = ' ';
Expand Down Expand Up @@ -114,4 +120,30 @@ else if (country != null)
else
return new Locale(language);
}

/** see {@link Locale#toString()}, but ignore extends */
private Locale deJavaLocale(String value) {
if (value.length() == 0) {
return new Locale("");
}

int extStart = value.indexOf("_#");
if (extStart != -1) value = value.substring(0, extStart);

int pos1 = value.indexOf('_');
if (pos1 == -1) { // It is not java;
return null;
}

String lang = value.substring(0, pos1++), country, var;
int pos2 = value.indexOf('_', pos1);
if (pos2 == -1) {
country = value.substring(pos1);
var = "";
} else {
country = value.substring(pos1, pos2);
var = value.substring(pos2 + 1);
}
return new Locale(lang, country, var);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.com.caucho.hessian.io;

import static org.junit.Assert.*;

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(new Locale("zh", "CN"));
assertLocale(new Locale("zh-hant", "CN"));
}

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 e7968e5

Please sign in to comment.