1 5 package org.h2.util; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.io.Reader ; 10 11 15 public class ExactUTF8InputStreamReader extends Reader { 16 17 private InputStream in; 18 19 public ExactUTF8InputStreamReader(InputStream in) { 20 this.in = in; 21 } 22 23 public void close() throws IOException { 24 } 25 26 public int read(char[] cbuf, int off, int len) throws IOException { 27 for(int i=0; i<len; i++, off++) { 28 int x = in.read(); 29 if(x < 0) { 30 return i == 0 ? -1 : i; 31 } 32 x = x & 0xff; 33 if(x < 0x80) { 34 cbuf[off] = (char)x; 35 } else if(x >= 0xe0) { 36 cbuf[off] = (char)(((x & 0xf) << 12) + ((in.read() & 0x3f) << 6) + (in.read() & 0x3f)); 37 } else { 38 cbuf[off] = (char)(((x & 0x1f) << 6) + (in.read() & 0x3f)); 39 } 40 } 41 return len; 42 } 43 44 } 45 | Popular Tags |