aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
blob: 0a28333e258ca06a06f18a3240dd54140b81fd4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.util;

import java.io.ByteArrayInputStream;

/**
 * Adapts a plain byte array to {@link LittleEndianInput}
 */
public class LittleEndianByteArrayInputStream extends ByteArrayInputStream implements LittleEndianInput {
	/**
	 * Creates <code>LittleEndianByteArrayInputStream</code>
	 * that uses <code>buf</code> as its
	 * buffer array. The initial value of <code>pos</code>
	 * is <code>offset</code> and the initial value
	 * of <code>count</code> is the minimum of <code>offset+length</code>
	 * and <code>buf.length</code>.
	 * The buffer array is not copied. The buffer's mark is
	 * set to the specified offset.
	 *
	 * @param   buf      the input buffer.
	 * @param   offset   the offset in the buffer of the first byte to read.
	 * @param   length   the maximum number of bytes to read from the buffer.
	 */
	public LittleEndianByteArrayInputStream(byte[] buf, int offset, int length) { // NOSONAR
	    super(buf, offset, length);
	}

	/**
	 * Creates <code>LittleEndianByteArrayInputStream</code>
	 * that uses <code>buf</code> as its
	 * buffer array. The initial value of <code>pos</code>
	 * is <code>offset</code> and the initial value
	 * of <code>count</code> is the minimum of <code>offset+buf.length</code>
	 * and <code>buf.length</code>.
	 * The buffer array is not copied. The buffer's mark is
	 * set to the specified offset.
	 *
	 * @param   buf      the input buffer.
	 * @param   offset   the offset in the buffer of the first byte to read.
	 */
	public LittleEndianByteArrayInputStream(byte[] buf, int offset) {
	    this(buf, offset, buf.length - offset);
	}

	/**
	 * Creates a <code>LittleEndianByteArrayInputStream</code>
	 * so that it uses <code>buf</code> as its
	 * buffer array.
	 * The buffer array is not copied.
	 * The initial value of <code>pos</code>
	 * is <code>0</code> and the initial value
	 * of <code>count</code> is the length of
	 * <code>buf</code>.
	 *
	 * @param   buf   the input buffer.
	 */
	public LittleEndianByteArrayInputStream(byte[] buf) {
	    super(buf);
	}

	protected void checkPosition(int i) {
		if (i > count - pos) {
			throw new RuntimeException("Buffer overrun, having " + count + " bytes in the stream and position is at " + pos +
					", but trying to increment position by " + i);
		}
	}

	public int getReadIndex() {
		return pos;
	}

	public void setReadIndex(int pos) {
	   if (pos < 0 || pos >= count) {
	        throw new IndexOutOfBoundsException();
	   }
	   this.pos = pos;
	}
	
	
	@Override
    public byte readByte() {
		checkPosition(1);
		return (byte)read();
	}

	@Override
    public int readInt() {
	    final int size = LittleEndianConsts.INT_SIZE;
		checkPosition(size);
		int le = LittleEndian.getInt(buf, pos);
        long skipped = super.skip(size);
        assert skipped == size : "Buffer overrun";
		return le;
	}

	@Override
    public long readLong() {
	    final int size = LittleEndianConsts.LONG_SIZE;
		checkPosition(size);
		long le = LittleEndian.getLong(buf, pos);
        long skipped = super.skip(size);
        assert skipped == size : "Buffer overrun";
		return le;
	}

	@Override
    public short readShort() {
        final int size = LittleEndianConsts.SHORT_SIZE;
        checkPosition(size);
        short le = LittleEndian.getShort(buf, pos);
        long skipped = super.skip(size);
        assert skipped == size : "Buffer overrun";
        return le;
	}

	@Override
    public int readUByte() {
	    return readByte() & 0x00FF;
	}

	@Override
    public int readUShort() {
        return readShort() & 0x00FFFF;
	}

	public long readUInt() {
	    return readInt() & 0x00FFFFFFFFL; 
    }

    @Override
    public double readDouble() {
        return Double.longBitsToDouble(readLong());
    }
	
	@Override
    public void readFully(byte[] buffer, int off, int len) {
		checkPosition(len);
		read(buffer, off, len);
	}

	@Override
    public void readFully(byte[] buffer) {
        checkPosition(buffer.length);
        read(buffer, 0, buffer.length);
	}

    @Override
    public void readPlain(byte[] buf, int off, int len) {
        readFully(buf, off, len);
    }
}