pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / ByteArrayDataInput.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.util.BytesRef;
21
22 /** @lucene.experimental */
23 public final class ByteArrayDataInput extends DataInput {
24
25   private byte[] bytes;
26
27   private int pos;
28   private int limit;
29
30   public ByteArrayDataInput(byte[] bytes) {
31     reset(bytes);
32   }
33
34   public ByteArrayDataInput(byte[] bytes, int offset, int len) {
35     reset(bytes, offset, len);
36   }
37
38   public ByteArrayDataInput() {
39     reset(BytesRef.EMPTY_BYTES);
40   }
41
42   public void reset(byte[] bytes) {
43     reset(bytes, 0, bytes.length);
44   }
45
46   public int getPosition() {
47     return pos;
48   }
49   
50   public void reset(byte[] bytes, int offset, int len) {
51     this.bytes = bytes;
52     pos = offset;
53     limit = offset + len;
54   }
55
56   public boolean eof() {
57     return pos == limit;
58   }
59
60   public void skipBytes(int count) {
61     pos += count;
62     assert pos <= limit;
63   }
64
65   @Override
66   public short readShort() {
67     return (short) (((bytes[pos++] & 0xFF) <<  8) |  (bytes[pos++] & 0xFF));
68   }
69  
70   @Override
71   public int readInt() {
72     assert pos+4 <= limit;
73     return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)
74       | ((bytes[pos++] & 0xFF) <<  8) |  (bytes[pos++] & 0xFF);
75   }
76  
77   @Override
78   public long readLong() {
79     assert pos+8 <= limit;
80     final int i1 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) |
81       ((bytes[pos++] & 0xff) << 8) | (bytes[pos++] & 0xff);
82     final int i2 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) |
83       ((bytes[pos++] & 0xff) << 8) | (bytes[pos++] & 0xff);
84     return (((long)i1) << 32) | (i2 & 0xFFFFFFFFL);
85   }
86
87   @Override
88   public int readVInt() {
89     checkBounds();
90     byte b = bytes[pos++];
91     int i = b & 0x7F;
92     for (int shift = 7; (b & 0x80) != 0; shift += 7) {
93       checkBounds();
94       b = bytes[pos++];
95       i |= (b & 0x7F) << shift;
96     }
97     return i;
98   }
99  
100   @Override
101   public long readVLong() {
102     checkBounds();
103     byte b = bytes[pos++];
104     long i = b & 0x7F;
105     for (int shift = 7; (b & 0x80) != 0; shift += 7) {
106       checkBounds();
107       b = bytes[pos++];
108       i |= (b & 0x7FL) << shift;
109     }
110     return i;
111   }
112
113   // NOTE: AIOOBE not EOF if you read too much
114   @Override
115   public byte readByte() {
116     checkBounds();
117     return bytes[pos++];
118   }
119
120   // NOTE: AIOOBE not EOF if you read too much
121   @Override
122   public void readBytes(byte[] b, int offset, int len) {
123     assert pos + len <= limit;
124     System.arraycopy(bytes, pos, b, offset, len);
125     pos += len;
126   }
127
128   private boolean checkBounds() {
129     assert pos < limit;
130     return true;
131   }
132 }