pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / UnsafeByteArrayInputStream.java
1 package org.apache.lucene.util;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6
7 /**
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements.  See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License.  You may obtain a copy of the License at
14  *
15  *     http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23
24 /**
25  * This class, much like {@link ByteArrayInputStream} uses a given buffer as a
26  * source of an InputStream. Unlike ByteArrayInputStream, this class does not
27  * "waste" memory by creating a local copy of the given buffer, but rather uses
28  * the given buffer as is. Hence the name Unsafe. While using this class one
29  * should remember that the byte[] buffer memory is shared and might be changed
30  * from outside.
31  * 
32  * For reuse-ability, a call for {@link #reInit(byte[])} can be called, and
33  * initialize the stream with a new buffer.
34  * 
35  * @lucene.experimental
36  */
37 public class UnsafeByteArrayInputStream extends InputStream {
38
39   private byte[] buffer;
40   private int markIndex;
41   private int upperLimit;
42   private int index;
43
44   /**
45    * Creates a new instance by not using any byte[] up front. If you use this
46    * constructor, you MUST call either of the {@link #reInit(byte[]) reInit}
47    * methods before you consume any byte from this instance.<br>
48    * This constructor is for convenience purposes only, so that if one does not
49    * have the byte[] at the moment of creation, one is not forced to pass a
50    * <code>new byte[0]</code> or something. Obviously in that case, one will
51    * call either {@link #reInit(byte[]) reInit} methods before using the class.
52    */
53   public UnsafeByteArrayInputStream() {
54     markIndex = upperLimit = index = 0;
55   }
56
57   /**
58    * Creates an UnsafeByteArrayInputStream which uses a given byte array as
59    * the source of the stream. Default range is [0 , buffer.length)
60    * 
61    * @param buffer
62    *            byte array used as the source of this stream
63    */
64   public UnsafeByteArrayInputStream(byte[] buffer) {
65     reInit(buffer, 0, buffer.length);
66   }
67
68   /**
69    * Creates an UnsafeByteArrayInputStream which uses a given byte array as
70    * the source of the stream, at the specific range: [startPos, endPos)
71    * 
72    * @param buffer
73    *            byte array used as the source of this stream
74    * @param startPos
75    *            first index (inclusive) to the data lying in the given buffer
76    * @param endPos
77    *            an index (exclusive) where the data ends. data @
78    *            buffer[endPos] will never be read
79    */
80   public UnsafeByteArrayInputStream(byte[] buffer, int startPos, int endPos) {
81     reInit(buffer, startPos, endPos);
82   }
83
84   @Override
85   public void mark(int readlimit) {
86     markIndex = index;
87   }
88
89   @Override
90   public boolean markSupported() {
91     return true;
92   }
93
94   /**
95    * Initialize the stream with a given buffer, using the default limits of
96    * [0, buffer.length)
97    * 
98    * @param buffer
99    *            byte array used as the source of this stream
100    */
101   public void reInit(byte[] buffer) {
102     reInit(buffer, 0, buffer.length);
103   }
104
105   /**
106    * Initialize the stream with a given byte array as the source of the
107    * stream, at the specific range: [startPos, endPos)
108    * 
109    * @param buffer
110    *            byte array used as the source of this stream
111    * @param startPos
112    *            first index (inclusive) to the data lying in the given buffer
113    * @param endPos
114    *            an index (exclusive) where the data ends. data @
115    *            buffer[endPos] will never be read
116    */
117   public void reInit(byte[] buffer, int startPos, int endPos) {
118     this.buffer = buffer;
119     markIndex = startPos;
120     upperLimit = endPos;
121     index = markIndex;
122   }
123
124   @Override
125   public int available() throws IOException {
126     return upperLimit - index;
127   }
128
129   /**
130    * Read a byte. Data returned as an integer [0,255] If end of stream
131    * reached, returns -1
132    */
133   @Override
134   public int read() throws IOException {
135     return index < upperLimit ? buffer[index++] & 0xff : -1;
136   }
137
138   /**
139    * Resets the stream back to its original state. Basically - moving the
140    * index back to start position.
141    */
142   @Override
143   public void reset() throws IOException {
144     index = markIndex;
145   }
146
147 }