01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.util;
07:
08: import java.io.IOException;
09: import java.io.InputStream;
10: import java.io.Reader;
11:
12: /**
13: * The regular InputStreamReader may read some more bytes than required.
14: * If this is a problem, use this class.
15: */
16: public class ExactUTF8InputStreamReader extends Reader {
17:
18: private InputStream in;
19:
20: public ExactUTF8InputStreamReader(InputStream in) {
21: this .in = in;
22: }
23:
24: public void close() throws IOException {
25: }
26:
27: public int read(char[] chars, int off, int len) throws IOException {
28: for (int i = 0; i < len; i++, off++) {
29: int x = in.read();
30: if (x < 0) {
31: return i == 0 ? -1 : i;
32: }
33: x = x & 0xff;
34: if (x < 0x80) {
35: chars[off] = (char) x;
36: } else if (x >= 0xe0) {
37: chars[off] = (char) (((x & 0xf) << 12)
38: + ((in.read() & 0x3f) << 6) + (in.read() & 0x3f));
39: } else {
40: chars[off] = (char) (((x & 0x1f) << 6) + (in.read() & 0x3f));
41: }
42: }
43: return len;
44: }
45:
46: }
|