File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\store\DataReader.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.store;
8
9 import java.io.EOFException;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.Reader;
13 import org.h2.util.IOUtils;
14
15 /**
16 * This class is backed by an input stream and supports reading values and
17 * variable size data.
18 */
19 public class DataReader extends Reader {
20
21 private final InputStream in;
22
23 /**
24 * Create a new data reader.
25 *
26 * @param in the input stream
27 */
28 public DataReader(InputStream in) {
29 this.in = in;
30 }
31
32 /**
33 * Read a byte.
34 *
35 * @return the byte
36 */
37 public byte readByte() throws IOException {
38 int x = in.read();
39 if (x < 0) {
40 throw new FastEOFException();
41 }
42 return (byte) x;
43 }
44
45 /**
46 * Read a variable size integer.
47 *
48 * @return the value
49 */
50 public int readVarInt() throws IOException {
51 int b = readByte();
52 if (b >= 0) {
53 return b;
54 }
55 int x = b & 0x7f;
56 b = readByte();
57 if (b >= 0) {
58 return x | (b << 7);
59 }
60 x |= (b & 0x7f) << 7;
61 b = readByte();
62 if (b >= 0) {
63 return x | (b << 14);
64 }
65 x |= (b & 0x7f) << 14;
66 b = readByte();
67 if (b >= 0) {
68 return x | b << 21;
69 }
70 return x | ((b & 0x7f) << 21) | (readByte() << 28);
71 }
72
73 /**
74 * Read a variable size long.
75 *
76 * @return the value
77 */
78 public long readVarLong() throws IOException {
79 long x = readByte();
80 if (x >= 0) {
81 return x;
82 }
83 x &= 0x7f;
84 for (int s = 7;; s += 7) {
85 long b = readByte();
86 x |= (b & 0x7f) << s;
87 if (b >= 0) {
88 return x;
89 }
90 }
91 }
92
93 /**
94 * Read an integer.
95 *
96 * @return the value
97 */
98 // public int readInt() throws IOException {
99 // return (read() << 24) + ((read() & 0xff) << 16) +
100 // ((read() & 0xff) << 8) + (read() & 0xff);
101 //}
102
103 /**
104 * Read a long.
105 *
106 * @return the value
107 */
108 // public long readLong() throws IOException {
109 // return ((long) (readInt()) << 32) + (readInt() & 0xffffffffL);
110 // }
111
112 /**
113 * Read a number of bytes.
114 *
115 * @param buff the target buffer
116 * @param offset the offset within the target buffer
117 * @param len the number of bytes to read
118 */
119 public void readFully(byte[] buff, int offset, int len) throws IOException {
120 int got = IOUtils.readFully(in, buff, offset, len);
121 if (got < len) {
122 throw new FastEOFException();
123 }
124 }
125
126 /**
127 * Read a string from the stream.
128 *
129 * @return the string
130 */
131 public String readString() throws IOException {
132 int len = readVarInt();
133 return readString(len);
134 }
135
136 private String readString(int len) throws IOException {
137 char[] chars = new char[len];
138 for (int i = 0; i < len; i++) {
139 chars[i] = readChar();
140 }
141 return new String(chars);
142 }
143
144 /**
145 * Read one character from the input stream.
146 *
147 * @return the character
148 */
149 private char readChar() throws IOException {
150 int x = readByte() & 0xff;
151 if (x < 0x80) {
152 return (char) x;
153 } else if (x >= 0xe0) {
154 return (char) (((x & 0xf) << 12) + ((readByte() & 0x3f) << 6) + (readByte() & 0x3f));
155 } else {
156 return (char) (((x & 0x1f) << 6) + (readByte() & 0x3f));
157 }
158 }
159
160 public void close() throws IOException {
161 // ignore
162 }
163
164 public int read(char[] buff, int off, int len) throws IOException {
165 int i = 0;
166 try {
167 for (; i < len; i++) {
168 buff[i] = readChar();
169 }
170 return len;
171 } catch (EOFException e) {
172 return i;
173 }
174 }
175
176 /**
177 * Constructing such an EOF exception is fast, because the stack trace is
178 * not filled in. If used in a static context, this will also avoid
179 * classloader memory leaks.
180 */
181 static class FastEOFException extends EOFException {
182
183 private static final long serialVersionUID = 1L;
184
185 public synchronized Throwable fillInStackTrace() {
186 return null;
187 }
188
189 }
190
191 }
192