Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. java charset decode issue
  2. public static void main(String[] _args) throws Exception {
  3.     String str="a1a4:· a5f6:ヶ a8c5:ㄅ";          
  4.     ByteBuffer bf=readToByteBuffer(new ByteArrayInputStream(str.getBytes()));
  5.     System.out.println(Charset.forName("GB2312").decode(bf).toString());
  6. }
  7. private static final int bufferSize = 0x20000;
  8. static ByteBuffer readToByteBuffer(InputStream inStream) throws IOException {
  9.     byte[] buffer = new byte[bufferSize];
  10.     ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);
  11.     int read;
  12.     while (true) {
  13.         read = inStream.read(buffer);
  14.         if (read == -1)
  15.             break;
  16.         outStream.write(buffer, 0, read);
  17.     }
  18.     ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
  19.     return byteData;
  20. }
  21.        
  22. a1a4:? a5f6:ヶ a8c5:ㄅ