1 package org.apache.lucene.util;
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
32 * For reuse-ability, a call for {@link #reInit(byte[])} can be called, and
33 * initialize the stream with a new buffer.
35 * @lucene.experimental
37 public class UnsafeByteArrayInputStream extends InputStream {
39 private byte[] buffer;
40 private int markIndex;
41 private int upperLimit;
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.
53 public UnsafeByteArrayInputStream() {
54 markIndex = upperLimit = index = 0;
58 * Creates an UnsafeByteArrayInputStream which uses a given byte array as
59 * the source of the stream. Default range is [0 , buffer.length)
62 * byte array used as the source of this stream
64 public UnsafeByteArrayInputStream(byte[] buffer) {
65 reInit(buffer, 0, buffer.length);
69 * Creates an UnsafeByteArrayInputStream which uses a given byte array as
70 * the source of the stream, at the specific range: [startPos, endPos)
73 * byte array used as the source of this stream
75 * first index (inclusive) to the data lying in the given buffer
77 * an index (exclusive) where the data ends. data @
78 * buffer[endPos] will never be read
80 public UnsafeByteArrayInputStream(byte[] buffer, int startPos, int endPos) {
81 reInit(buffer, startPos, endPos);
85 public void mark(int readlimit) {
90 public boolean markSupported() {
95 * Initialize the stream with a given buffer, using the default limits of
99 * byte array used as the source of this stream
101 public void reInit(byte[] buffer) {
102 reInit(buffer, 0, buffer.length);
106 * Initialize the stream with a given byte array as the source of the
107 * stream, at the specific range: [startPos, endPos)
110 * byte array used as the source of this stream
112 * first index (inclusive) to the data lying in the given buffer
114 * an index (exclusive) where the data ends. data @
115 * buffer[endPos] will never be read
117 public void reInit(byte[] buffer, int startPos, int endPos) {
118 this.buffer = buffer;
119 markIndex = startPos;
125 public int available() throws IOException {
126 return upperLimit - index;
130 * Read a byte. Data returned as an integer [0,255] If end of stream
131 * reached, returns -1
134 public int read() throws IOException {
135 return index < upperLimit ? buffer[index++] & 0xff : -1;
139 * Resets the stream back to its original state. Basically - moving the
140 * index back to start position.
143 public void reset() throws IOException {