I have a client in windows codepage 1252, encoding to base64 ("123" becomes "eNozNDIGAAEtAJc=") and compress the string using zlib (eventually become "xÚ342") , then another server writen in Java/JBoss and need to decode from base 64 and decompress using java.util.zip:
Inflater decompresser = new Inflater();
decompresser.setInput(b, 0, b.length); //output, 0, compressedDataLength);
while ((resultLength = decompresser.inflate(result, 0, result.length)) != 0) {
tempOut.write(new String(result, 0, resultLength, "UTF-8"));
}
decompresser.end();Everything was fine. but if I have a client in windows codepage 936, encoding to base64 ("123" becomes "eD80MgYAAS0AAAA=") and compress the string using zlib (eventually become "x?42"),, and how can i run the decoding from base 64 and decompress using java.util.zip without problem?a chinese version of JVM??
I have tried something like this but seems all failed:
String defaultEncodingName = System.getProperty( "file.encoding" );
System.out.println("defaultEncodingName : " + defaultEncodingName);
System.out.println("System.getProperty( sun.jnu.encoding ) : " + System.getProperty( "sun.jnu.encoding" ));
System.out.println("System.getProperty( user.country ) : " + System.getProperty( "user.country" ));
System.out.println("System.getProperty( user.region ) : " + System.getProperty( "user.region" ));
System.out.println("System.getProperty( user.language ) : " + System.getProperty( "user.language" ));
// in JDK 1.5+, will typically be "windows-1252"
// First, get the Charset/encoding then convert to String.
String defaultEncodingName2 = Charset.defaultCharset().name();
System.out.println("defaultEncodingName2 : " + defaultEncodingName2);
// I'm told this circumlocution has the nice property you can even use
// it in an unsigned Applet.
String defaultEncodingName3 = new OutputStreamWriter( System.out ).getEncoding();
System.out.println("defaultEncodingName3 : " + defaultEncodingName3);
System.out.println(Locale.getDefault());
// TODO Auto-generated method stub
byte[] result = new byte[1024];
File tempFile = null;
InputStream is = null;
int resultLength;
try
{
Locale.setDefault(new Locale("zh", "CN"));
System.out.println("After setting new locale, " + Locale.getDefault());
defaultEncodingName = System.getProperty( "file.encoding" );
System.out.println("defaultEncodingName : " + defaultEncodingName);
tempFile = File.createTempFile("XXX", ".xml");
BufferedWriter tempOut = new BufferedWriter(new FileWriter(tempFile));
//byte[] b = Base64.decode("eNrzSM3JyVcozy/KSQEAGKsEPQ==");
byte[] b = Base64.decode("eD9IPz8oPz8BABg/PQAAAAA=");//Hello world
Inflater decompresser = new Inflater();
decompresser.setInput(b, 0, b.length); //output, 0, compressedDataLength);
System.out.println("b.length :" +b.length);
String strContent = "";
while ((resultLength = decompresser.inflate(result, 0, result.length)) != 0) {
strContent = new String(result, 0, resultLength, "UTF-8");
System.out.println("strContent :" +strContent);
tempOut.write(strContent);
}
decompresser.end();
tempOut.close();
result = null;
where base64.decode(...) is something like:
public static byte[]
decode(String s) {
int end = 0; // end state
if (s.endsWith("=")) {
end++;
}
if (s.endsWith("==")) {
end++;
}
int len = (s.length() + 3)/4 * 3 - end;
byte[] result = new byte[len];
int dst = 0;
try {
for(int src = 0; src< s.length(); src++) {
int code = charSet.indexOf(s.charAt(src));
if (code == -1) {
break;
}
switch (src%4) {
case 0:
result[dst] = (byte) (code<<2);
break;
case 1:
result[dst++] |= (byte) ((code>>4) & 0x3);
result[dst] = (byte) (code<<4);
break;
case 2:
result[dst++] |= (byte) ((code>>2) & 0xf);
result[dst] = (byte) (code<<6);
break;
case 3:
result[dst++] |= (byte) (code & 0x3f);
break;
}
}
} catch (ArrayIndexOutOfBoundsException e) {}
return result;
}
if decode & decompress these bytes ("eNrzSM3JyVcozy/KSQEAGKsEPQ==") from english locale PC in English JVM, everything is fine, I could get "Hello world".
but decode & decompress these bytes ("eD9IPz8oPz8BABg/PQAAAAA=") from chinese locale PC in English JVM, I could not get "Hello world"
Inflater decompresser = new Inflater();
decompresser.setInput(b, 0, b.length); //output, 0, compressedDataLength);
while ((resultLength = decompresser.inflate(result, 0, result.length)) != 0) {
tempOut.write(new String(result, 0, resultLength, "UTF-8"));
}
decompresser.end();Everything was fine. but if I have a client in windows codepage 936, encoding to base64 ("123" becomes "eD80MgYAAS0AAAA=") and compress the string using zlib (eventually become "x?42"),, and how can i run the decoding from base 64 and decompress using java.util.zip without problem?a chinese version of JVM??
I have tried something like this but seems all failed:
String defaultEncodingName = System.getProperty( "file.encoding" );
System.out.println("defaultEncodingName : " + defaultEncodingName);
System.out.println("System.getProperty( sun.jnu.encoding ) : " + System.getProperty( "sun.jnu.encoding" ));
System.out.println("System.getProperty( user.country ) : " + System.getProperty( "user.country" ));
System.out.println("System.getProperty( user.region ) : " + System.getProperty( "user.region" ));
System.out.println("System.getProperty( user.language ) : " + System.getProperty( "user.language" ));
// in JDK 1.5+, will typically be "windows-1252"
// First, get the Charset/encoding then convert to String.
String defaultEncodingName2 = Charset.defaultCharset().name();
System.out.println("defaultEncodingName2 : " + defaultEncodingName2);
// I'm told this circumlocution has the nice property you can even use
// it in an unsigned Applet.
String defaultEncodingName3 = new OutputStreamWriter( System.out ).getEncoding();
System.out.println("defaultEncodingName3 : " + defaultEncodingName3);
System.out.println(Locale.getDefault());
// TODO Auto-generated method stub
byte[] result = new byte[1024];
File tempFile = null;
InputStream is = null;
int resultLength;
try
{
Locale.setDefault(new Locale("zh", "CN"));
System.out.println("After setting new locale, " + Locale.getDefault());
defaultEncodingName = System.getProperty( "file.encoding" );
System.out.println("defaultEncodingName : " + defaultEncodingName);
tempFile = File.createTempFile("XXX", ".xml");
BufferedWriter tempOut = new BufferedWriter(new FileWriter(tempFile));
//byte[] b = Base64.decode("eNrzSM3JyVcozy/KSQEAGKsEPQ==");
byte[] b = Base64.decode("eD9IPz8oPz8BABg/PQAAAAA=");//Hello world
Inflater decompresser = new Inflater();
decompresser.setInput(b, 0, b.length); //output, 0, compressedDataLength);
System.out.println("b.length :" +b.length);
String strContent = "";
while ((resultLength = decompresser.inflate(result, 0, result.length)) != 0) {
strContent = new String(result, 0, resultLength, "UTF-8");
System.out.println("strContent :" +strContent);
tempOut.write(strContent);
}
decompresser.end();
tempOut.close();
result = null;
where base64.decode(...) is something like:
public static byte[]
decode(String s) {
int end = 0; // end state
if (s.endsWith("=")) {
end++;
}
if (s.endsWith("==")) {
end++;
}
int len = (s.length() + 3)/4 * 3 - end;
byte[] result = new byte[len];
int dst = 0;
try {
for(int src = 0; src< s.length(); src++) {
int code = charSet.indexOf(s.charAt(src));
if (code == -1) {
break;
}
switch (src%4) {
case 0:
result[dst] = (byte) (code<<2);
break;
case 1:
result[dst++] |= (byte) ((code>>4) & 0x3);
result[dst] = (byte) (code<<4);
break;
case 2:
result[dst++] |= (byte) ((code>>2) & 0xf);
result[dst] = (byte) (code<<6);
break;
case 3:
result[dst++] |= (byte) (code & 0x3f);
break;
}
}
} catch (ArrayIndexOutOfBoundsException e) {}
return result;
}
if decode & decompress these bytes ("eNrzSM3JyVcozy/KSQEAGKsEPQ==") from english locale PC in English JVM, everything is fine, I could get "Hello world".
but decode & decompress these bytes ("eD9IPz8oPz8BABg/PQAAAAA=") from chinese locale PC in English JVM, I could not get "Hello world"
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货