~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/solrj/src/java/org/apache/solr/common/util/FastInputStream.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
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
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
package org.apache.solr.common.util;
19
 
 
20
 
import java.io.*;
21
 
 
22
 
/** Single threaded buffered InputStream
23
 
 *  Internal Solr use only, subject to change.
24
 
 */
25
 
public class FastInputStream extends InputStream implements DataInput {
26
 
  private final InputStream in;
27
 
  private final byte[] buf;
28
 
  private int pos;
29
 
  private int end;
30
 
 
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);
35
 
  }
36
 
 
37
 
  public FastInputStream(InputStream in, byte[] tempBuffer, int start, int end) {
38
 
    this.in = in;
39
 
    this.buf = tempBuffer;
40
 
    this.pos = start;
41
 
    this.end = end;
42
 
  }
43
 
 
44
 
 
45
 
  public static FastInputStream wrap(InputStream in) {
46
 
    return (in instanceof FastInputStream) ? (FastInputStream)in : new FastInputStream(in);
47
 
  }
48
 
 
49
 
  @Override
50
 
  public int read() throws IOException {
51
 
    if (pos >= end) {
52
 
      refill();
53
 
      if (pos >= end) return -1;
54
 
    }
55
 
    return buf[pos++] & 0xff;     
56
 
  }
57
 
 
58
 
  public int readUnsignedByte() throws IOException {
59
 
    if (pos >= end) {
60
 
      refill();
61
 
      if (pos >= end) throw new EOFException();
62
 
    }
63
 
    return buf[pos++] & 0xff;
64
 
  }
65
 
 
66
 
  public void refill() throws IOException {
67
 
    // this will set end to -1 at EOF
68
 
    end = in.read(buf, 0, buf.length);
69
 
    pos = 0;
70
 
  }
71
 
 
72
 
  @Override
73
 
  public int available() throws IOException {
74
 
    return end - pos;
75
 
  }
76
 
 
77
 
  @Override
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;
81
 
    if (end-pos > 0) {
82
 
      r = Math.min(end-pos, len);
83
 
      System.arraycopy(buf, pos, b, off, r);      
84
 
      pos += r;
85
 
    }
86
 
 
87
 
    if (r == len) return r;
88
 
 
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;
93
 
      r += ret;
94
 
      return r;
95
 
    }
96
 
 
97
 
    refill();
98
 
 
99
 
    // first read from our buffer;
100
 
    if (end-pos > 0) {
101
 
      int toRead = Math.min(end-pos, len-r);
102
 
      System.arraycopy(buf, pos, b, off+r, toRead);
103
 
      pos += toRead;
104
 
      r += toRead;
105
 
      return r;
106
 
    }
107
 
    
108
 
    return r > 0 ? r : -1;
109
 
  }
110
 
 
111
 
  @Override
112
 
  public void close() throws IOException {
113
 
    in.close();
114
 
  }
115
 
 
116
 
  public void readFully(byte b[]) throws IOException {
117
 
    readFully(b, 0, b.length);
118
 
  }
119
 
 
120
 
  public void readFully(byte b[], int off, int len) throws IOException {
121
 
    while (len>0) {
122
 
      int ret = read(b, off, len);
123
 
      if (ret==-1) {
124
 
        throw new EOFException();
125
 
      }
126
 
      off += ret;
127
 
      len -= ret;
128
 
    }
129
 
  }
130
 
 
131
 
  public int skipBytes(int n) throws IOException {
132
 
    if (end-pos >= n) {
133
 
      pos += n;
134
 
      return n;
135
 
    }
136
 
 
137
 
    if (end-pos<0) return -1;
138
 
    
139
 
    int r = end-pos;
140
 
    pos = end;
141
 
 
142
 
    while (r < n) {
143
 
      refill();
144
 
      if (end-pos <= 0) return r;
145
 
      int toRead = Math.min(end-pos, n-r);
146
 
      r += toRead;
147
 
      pos += toRead;
148
 
    }
149
 
 
150
 
    return r;
151
 
  }
152
 
 
153
 
  public boolean readBoolean() throws IOException {
154
 
    return readByte()==1;
155
 
  }
156
 
 
157
 
  public byte readByte() throws IOException {
158
 
    if (pos >= end) {
159
 
      refill();
160
 
      if (pos >= end) throw new EOFException();
161
 
    }
162
 
    return buf[pos++];
163
 
  }
164
 
 
165
 
 
166
 
  public short readShort() throws IOException {
167
 
    return (short)((readUnsignedByte() << 8) | readUnsignedByte());
168
 
  }
169
 
 
170
 
  public int readUnsignedShort() throws IOException {
171
 
    return (readUnsignedByte() << 8) | readUnsignedByte();
172
 
  }
173
 
 
174
 
  public char readChar() throws IOException {
175
 
    return (char)((readUnsignedByte() << 8) | readUnsignedByte());
176
 
  }
177
 
 
178
 
  public int readInt() throws IOException {
179
 
    return  ((readUnsignedByte() << 24)
180
 
            |(readUnsignedByte() << 16)
181
 
            |(readUnsignedByte() << 8)
182
 
            | readUnsignedByte());
183
 
  }
184
 
 
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());
194
 
  }
195
 
 
196
 
  public float readFloat() throws IOException {
197
 
    return Float.intBitsToFloat(readInt());    
198
 
  }
199
 
 
200
 
  public double readDouble() throws IOException {
201
 
    return Double.longBitsToDouble(readLong());    
202
 
  }
203
 
 
204
 
  public String readLine() throws IOException {
205
 
    return new DataInputStream(this).readLine();
206
 
  }
207
 
 
208
 
  public String readUTF() throws IOException {
209
 
    return new DataInputStream(this).readUTF();
210
 
  }
211
 
}