2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.common.util;
22
/** Single threaded buffered InputStream
23
* Internal Solr use only, subject to change.
25
public class FastInputStream extends InputStream implements DataInput {
26
private final InputStream in;
27
private final byte[] buf;
31
public FastInputStream(InputStream in) {
32
// use default BUFSIZE of BufferedOutputStream so if we wrap that
33
// it won't cause double buffering.
34
this(in, new byte[8192], 0, 0);
37
public FastInputStream(InputStream in, byte[] tempBuffer, int start, int end) {
39
this.buf = tempBuffer;
45
public static FastInputStream wrap(InputStream in) {
46
return (in instanceof FastInputStream) ? (FastInputStream)in : new FastInputStream(in);
50
public int read() throws IOException {
53
if (pos >= end) return -1;
55
return buf[pos++] & 0xff;
58
public int readUnsignedByte() throws IOException {
61
if (pos >= end) throw new EOFException();
63
return buf[pos++] & 0xff;
66
public void refill() throws IOException {
67
// this will set end to -1 at EOF
68
end = in.read(buf, 0, buf.length);
73
public int available() throws IOException {
78
public int read(byte b[], int off, int len) throws IOException {
79
int r=0; // number of bytes read
80
// first read from our buffer;
82
r = Math.min(end-pos, len);
83
System.arraycopy(buf, pos, b, off, r);
87
if (r == len) return r;
89
// amount left to read is >= buffer size
90
if (len-r >= buf.length) {
91
int ret = in.read(b, off+r, len-r);
92
if (ret==-1) return r==0 ? -1 : r;
99
// first read from our buffer;
101
int toRead = Math.min(end-pos, len-r);
102
System.arraycopy(buf, pos, b, off+r, toRead);
108
return r > 0 ? r : -1;
112
public void close() throws IOException {
116
public void readFully(byte b[]) throws IOException {
117
readFully(b, 0, b.length);
120
public void readFully(byte b[], int off, int len) throws IOException {
122
int ret = read(b, off, len);
124
throw new EOFException();
131
public int skipBytes(int n) throws IOException {
137
if (end-pos<0) return -1;
144
if (end-pos <= 0) return r;
145
int toRead = Math.min(end-pos, n-r);
153
public boolean readBoolean() throws IOException {
154
return readByte()==1;
157
public byte readByte() throws IOException {
160
if (pos >= end) throw new EOFException();
166
public short readShort() throws IOException {
167
return (short)((readUnsignedByte() << 8) | readUnsignedByte());
170
public int readUnsignedShort() throws IOException {
171
return (readUnsignedByte() << 8) | readUnsignedByte();
174
public char readChar() throws IOException {
175
return (char)((readUnsignedByte() << 8) | readUnsignedByte());
178
public int readInt() throws IOException {
179
return ((readUnsignedByte() << 24)
180
|(readUnsignedByte() << 16)
181
|(readUnsignedByte() << 8)
182
| readUnsignedByte());
185
public long readLong() throws IOException {
186
return (((long)readUnsignedByte()) << 56)
187
| (((long)readUnsignedByte()) << 48)
188
| (((long)readUnsignedByte()) << 40)
189
| (((long)readUnsignedByte()) << 32)
190
| (((long)readUnsignedByte()) << 24)
191
| (readUnsignedByte() << 16)
192
| (readUnsignedByte() << 8)
193
| (readUnsignedByte());
196
public float readFloat() throws IOException {
197
return Float.intBitsToFloat(readInt());
200
public double readDouble() throws IOException {
201
return Double.longBitsToDouble(readLong());
204
public String readLine() throws IOException {
205
return new DataInputStream(this).readLine();
208
public String readUTF() throws IOException {
209
return new DataInputStream(this).readUTF();