知道java的字符集编码,那么java是怎么读取内存中的字节转换成你需要的字符的呢,其实很简单,
java中的是通过StringCoding来完成字符转换的,他是一个内嵌类,现将源代码拷至如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class StringCoding
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private StringCoding()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* The cached coders for each thread
*/
private static ThreadLocal decoder = new ThreadLocal();
private static ThreadLocal encoder = new ThreadLocal();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static boolean warnUnsupportedCharset = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static Object deref(ThreadLocal tl)
{
SoftReference sr = (SoftReference) tl.get();
if (sr == null)
return null;
return sr.get();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static void set(ThreadLocal tl, Object ob)
{
tl.set(new SoftReference(ob));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Trim the given byte array to the given length
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static byte[] trim(byte[] ba, int len)
{
if (len == ba.length)
return ba;
byte[] tba = new byte[len];
System.arraycopy(ba, 0, tba, 0, len);
return tba;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Trim the given char array to the given length
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static char[] trim(char[] ca, int len)
{
if (len == ca.length)
return ca;
char[] tca = new char[len];
System.arraycopy(ca, 0, tca, 0, len);
return tca;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static int scale(int len, float expansionFactor)
{
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when len is larger than 2**24.
return (int) (len * (double) expansionFactor);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static Charset lookupCharset(String csn)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (Charset.isSupported(csn))
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return Charset.forName(csn);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (UnsupportedCharsetException x)
{
throw new Error(x);
}
}
return null;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static void warnUnsupportedCharset(String csn)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (warnUnsupportedCharset)
{
// Use sun.misc.MessageUtils rather than the Logging API or
// System.err since this method may be called during VM
// initialization before either is available.
MessageUtils.err("WARNING: Default charset " + csn
+ " not supported, using ISO-8859-1 instead");
warnUnsupportedCharset = false;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// -- Decoding --
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Encapsulates either a ByteToCharConverter or a CharsetDecoder
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static abstract class StringDecoder
{
private final String requestedCharsetName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected StringDecoder(String requestedCharsetName)
{
this.requestedCharsetName = requestedCharsetName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
final String requestedCharsetName()
{
return requestedCharsetName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
abstract String charsetName();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
abstract char[] decode(byte[] ba, int off, int len);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A string decoder based upon a ByteToCharConverter
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static class ConverterSD extends StringDecoder
{
private ByteToCharConverter btc;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private ConverterSD(ByteToCharConverter btc, String rcn)
{
super(rcn);
this.btc = btc;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String charsetName()
{
return btc.getCharacterEncoding();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] decode(byte[] ba, int off, int len)
{
int en = scale(len, btc.getMaxCharsPerByte());
char[] ca = new char[en];
if (len == 0)
return ca;
btc.reset();
int n = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
n = btc.convert(ba, off, off + len, ca, 0, en);
n += btc.flush(ca, btc.nextCharIndex(), en);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (CharConversionException x)
{
// Yes, this is what we've always done
n = btc.nextCharIndex();
}
return trim(ca, n);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A string decoder based upon a CharsetDecoder
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static class CharsetSD extends StringDecoder
{
private final Charset cs;
private final CharsetDecoder cd;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private CharsetSD(Charset cs, String rcn)
{
super(rcn);
this.cs = cs;
this.cd = cs.newDecoder().onMalformedInput(
CodingErrorAction.REPLACE).onUnmappableCharacter(
CodingErrorAction.REPLACE);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String charsetName()
{
if (cs instanceof HistoricallyNamedCharset)
return ((HistoricallyNamedCharset) cs).historicalName();
return cs.name();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] decode(byte[] ba, int off, int len)
{
int en = scale(len, cd.maxCharsPerByte());
char[] ca = new char[en];
if (len == 0)
return ca;
cd.reset();
ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
CharBuffer cb = CharBuffer.wrap(ca);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
CoderResult cr = cd.decode(bb, cb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = cd.flush(cb);
if (!cr.isUnderflow())
cr.throwException();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (CharacterCodingException x)
{
// Substitution is always enabled,
// so this shouldn't happen
throw new Error(x);
}
return trim(ca, cb.position());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static char[] decode(String charsetName, byte[] ba, int off, int len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
StringDecoder sd = (StringDecoder) deref(decoder);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((sd == null)
|| !(csn.equals(sd.requestedCharsetName()) || csn.equals(sd
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
.charsetName())))
{
sd = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Charset cs = lookupCharset(csn);
if (cs != null)
sd = new CharsetSD(cs, csn);
else
sd = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IllegalCharsetNameException x)
{
// FALL THROUGH to ByteToCharConverter, for compatibility
}
if (sd == null)
sd = new ConverterSD(ByteToCharConverter.getConverter(csn), csn);
set(decoder, sd);
}
return sd.decode(ba, off, len);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static char[] decode(byte[] ba, int off, int len)
{
String csn = Converters.getDefaultEncodingName();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return decode(csn, ba, off, len);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (UnsupportedEncodingException x)
{
Converters.resetDefaultEncodingName();
warnUnsupportedCharset(csn);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return decode("ISO-8859-1", ba, off, len);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (UnsupportedEncodingException x)
{
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// -- Encoding --
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Encapsulates either a CharToByteConverter or a CharsetEncoder
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static abstract class StringEncoder
{
private final String requestedCharsetName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected StringEncoder(String requestedCharsetName)
{
this.requestedCharsetName = requestedCharsetName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
final String requestedCharsetName()
{
return requestedCharsetName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
abstract String charsetName();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
abstract byte[] encode(char[] cs, int off, int len);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A string encoder based upon a CharToByteConverter
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static class ConverterSE extends StringEncoder
{
private CharToByteConverter ctb;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private ConverterSE(CharToByteConverter ctb, String rcn)
{
super(rcn);
this.ctb = ctb;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String charsetName()
{
return ctb.getCharacterEncoding();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] encode(char[] ca, int off, int len)
{
int en = scale(len, ctb.getMaxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0)
return ba;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctb.reset();
int n;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
n = ctb.convertAny(ca, off, (off + len), ba, 0, en);
n += ctb.flushAny(ba, ctb.nextByteIndex(), en);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (CharConversionException x)
{
throw new Error("Converter malfunction: "
+ ctb.getClass().getName(), x);
}
return trim(ba, n);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// A string encoder based upon a CharsetEncoder
//
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static class CharsetSE extends StringEncoder
{
private Charset cs;
private CharsetEncoder ce;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private CharsetSE(Charset cs, String rcn)
{
super(rcn);
this.cs = cs;
this.ce = cs.newEncoder().onMalformedInput(
CodingErrorAction.REPLACE).onUnmappableCharacter(
CodingErrorAction.REPLACE);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String charsetName()
{
if (cs instanceof HistoricallyNamedCharset)
return ((HistoricallyNamedCharset) cs).historicalName();
return cs.name();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
byte[] encode(char[] ca, int off, int len)
{
int en = scale(len, ce.maxBytesPerChar());
byte[] ba = new byte[en];
if (len == 0)
return ba;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ce.reset();
ByteBuffer bb = ByteBuffer.wrap(ba);
CharBuffer cb = CharBuffer.wrap(ca, off, len);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow())
cr.throwException();
cr = ce.flush(bb);
if (!cr.isUnderflow())
cr.throwException();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (CharacterCodingException x)
{
// Substitution is always enabled,
// so this shouldn't happen
throw new Error(x);
}
return trim(ba, bb.position());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static byte[] encode(String charsetName, char[] ca, int off, int len)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws UnsupportedEncodingException
{
StringEncoder se = (StringEncoder) deref(encoder);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((se == null)
|| !(csn.equals(se.requestedCharsetName()) || csn.equals(se
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
.charsetName())))
{
se = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Charset cs = lookupCharset(csn);
if (cs != null)
se = new CharsetSE(cs, csn);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IllegalCharsetNameException x)
{
// FALL THROUGH to CharToByteConverter, for compatibility
}
if (se == null)
se = new ConverterSE(CharToByteConverter.getConverter(csn), csn);
set(encoder, se);
}
return se.encode(ca, off, len);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static byte[] encode(char[] ca, int off, int len)
{
String csn = Converters.getDefaultEncodingName();
System.out.println(csn);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return encode(csn, ca, off, len);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (UnsupportedEncodingException x)
{
Converters.resetDefaultEncodingName();
warnUnsupportedCharset(csn);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
return encode("ISO-8859-1", ca, off, len);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (UnsupportedEncodingException x)
{
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
好测试一下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class testCode
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String args[]) throws UnsupportedEncodingException
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
char[] a =
{ '你', '好' };
System.out.println("default:");
byte[] bDef = StringCoding.encode(a, 0, a.length);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < bDef.length; i++)
{
System.out.println(Integer.toBinaryString(bDef[i])
.substring(24, 32));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.out.println("GBK:");
byte[] b = StringCoding.encode("GBK", a, 0, a.length);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < b.length; i++)
{
System.out.println(Integer.toBinaryString(b[i]).substring(24, 32));
}
System.out.println("UTF-8:");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
byte[] butf_8 = StringCoding.encode("utf-8", a, 0, a.length);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i = 0; i < butf_8.length; i++)
{
System.out.println(Integer.toBinaryString(butf_8[i]).substring(24,
32));
}
byte[] code = new byte[4];
code[0] = (byte)Integer.parseInt("11000100", 2);
code[1] = (byte)Integer.parseInt("11100011", 2);
code[2] = (byte)Integer.parseInt("10111010", 2);
code[3] = (byte)Integer.parseInt("11000011", 2);
System.out.println(StringCoding.decode("GBK",code,0,code.length));
System.out.println(StringCoding.decode("utf-8",code,0,code.length));
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果如下:
default:
11000100
11100011
10111010
11000011
GBK:
11000100
11100011
10111010
11000011
UTF-8:
11100100
10111101
10100000
11100101
10100101
10111101
你好
???
看出不同了吧,window操作系统默认GBK编码方式,特别代码最后几行,我们把Byte输入,按不同的编码输出,就有不同的结果.想必您已轻知道您的程序为什么出现乱码了吧。