1 18 package org.apache.batik.util.io; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 30 public class UTF16Decoder extends AbstractCharDecoder { 31 32 35 protected boolean bigEndian; 36 37 42 public UTF16Decoder(InputStream is) throws IOException { 43 super(is); 44 int b1 = is.read(); 46 if (b1 == -1) { 47 endOfStreamError("UTF-16"); 48 } 49 int b2 = is.read(); 50 if (b2 == -1) { 51 endOfStreamError("UTF-16"); 52 } 53 int m = (((b1 & 0xff) << 8) | (b2 & 0xff)); 54 switch (m) { 55 case 0xfeff: 56 bigEndian = true; 57 break; 58 case 0xfffe: 59 break; 60 default: 61 charError("UTF-16"); 62 } 63 } 64 65 71 public UTF16Decoder(InputStream is, boolean be) { 72 super(is); 73 bigEndian = be; 74 } 75 76 80 public int readChar() throws IOException { 81 if (position == count) { 82 fillBuffer(); 83 } 84 if (count == -1) { 85 return END_OF_STREAM; 86 } 87 byte b1 = buffer[position++]; 88 if (position == count) { 89 fillBuffer(); 90 } 91 if (count == -1) { 92 endOfStreamError("UTF-16"); 93 } 94 byte b2 = buffer[position++]; 95 int c = (bigEndian) 96 ? (((b1 & 0xff) << 8) | (b2 & 0xff)) 97 : (((b2 & 0xff) << 8) | (b1 & 0xff)); 98 if (c == 0xfffe) { 99 charError("UTF-16"); 100 } 101 return c; 102 } 103 } 104 | Popular Tags |