You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LittleEndianByteArrayInputStream.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.ByteArrayInputStream;
  17. /**
  18. * Adapts a plain byte array to {@link LittleEndianInput}
  19. */
  20. public class LittleEndianByteArrayInputStream extends ByteArrayInputStream implements LittleEndianInput {
  21. /**
  22. * Creates <code>LittleEndianByteArrayInputStream</code>
  23. * that uses <code>buf</code> as its
  24. * buffer array. The initial value of <code>pos</code>
  25. * is <code>offset</code> and the initial value
  26. * of <code>count</code> is the minimum of <code>offset+length</code>
  27. * and <code>buf.length</code>.
  28. * The buffer array is not copied. The buffer's mark is
  29. * set to the specified offset.
  30. *
  31. * @param buf the input buffer.
  32. * @param offset the offset in the buffer of the first byte to read.
  33. * @param length the maximum number of bytes to read from the buffer.
  34. */
  35. public LittleEndianByteArrayInputStream(byte[] buf, int offset, int length) { // NOSONAR
  36. super(buf, offset, length);
  37. }
  38. /**
  39. * Creates <code>LittleEndianByteArrayInputStream</code>
  40. * that uses <code>buf</code> as its
  41. * buffer array. The initial value of <code>pos</code>
  42. * is <code>offset</code> and the initial value
  43. * of <code>count</code> is the minimum of <code>offset+buf.length</code>
  44. * and <code>buf.length</code>.
  45. * The buffer array is not copied. The buffer's mark is
  46. * set to the specified offset.
  47. *
  48. * @param buf the input buffer.
  49. * @param offset the offset in the buffer of the first byte to read.
  50. */
  51. public LittleEndianByteArrayInputStream(byte[] buf, int offset) {
  52. this(buf, offset, buf.length - offset);
  53. }
  54. /**
  55. * Creates a <code>LittleEndianByteArrayInputStream</code>
  56. * so that it uses <code>buf</code> as its
  57. * buffer array.
  58. * The buffer array is not copied.
  59. * The initial value of <code>pos</code>
  60. * is <code>0</code> and the initial value
  61. * of <code>count</code> is the length of
  62. * <code>buf</code>.
  63. *
  64. * @param buf the input buffer.
  65. */
  66. public LittleEndianByteArrayInputStream(byte[] buf) {
  67. super(buf);
  68. }
  69. protected void checkPosition(int i) {
  70. if (i > count - pos) {
  71. throw new IllegalStateException("Buffer overrun, having " + count + " bytes in the stream and position is at " + pos +
  72. ", but trying to increment position by " + i);
  73. }
  74. }
  75. public int getReadIndex() {
  76. return pos;
  77. }
  78. public void setReadIndex(int pos) {
  79. if (pos < 0 || pos >= count) {
  80. throw new IndexOutOfBoundsException("Invalid position: " + pos + " with count " + count);
  81. }
  82. this.pos = pos;
  83. }
  84. @Override
  85. public byte readByte() {
  86. checkPosition(1);
  87. return (byte)read();
  88. }
  89. @Override
  90. public int readInt() {
  91. final int size = LittleEndianConsts.INT_SIZE;
  92. checkPosition(size);
  93. int le = LittleEndian.getInt(buf, pos);
  94. long skipped = super.skip(size);
  95. assert skipped == size : "Buffer overrun";
  96. return le;
  97. }
  98. @Override
  99. public long readLong() {
  100. final int size = LittleEndianConsts.LONG_SIZE;
  101. checkPosition(size);
  102. long le = LittleEndian.getLong(buf, pos);
  103. long skipped = super.skip(size);
  104. assert skipped == size : "Buffer overrun";
  105. return le;
  106. }
  107. @Override
  108. public short readShort() {
  109. final int size = LittleEndianConsts.SHORT_SIZE;
  110. checkPosition(size);
  111. short le = LittleEndian.getShort(buf, pos);
  112. long skipped = super.skip(size);
  113. assert skipped == size : "Buffer overrun";
  114. return le;
  115. }
  116. @Override
  117. public int readUByte() {
  118. return readByte() & 0x00FF;
  119. }
  120. @Override
  121. public int readUShort() {
  122. return readShort() & 0x00FFFF;
  123. }
  124. public long readUInt() {
  125. return readInt() & 0x00FFFFFFFFL;
  126. }
  127. @Override
  128. public double readDouble() {
  129. return Double.longBitsToDouble(readLong());
  130. }
  131. @Override
  132. public void readFully(byte[] buffer, int off, int len) {
  133. checkPosition(len);
  134. read(buffer, off, len);
  135. }
  136. @Override
  137. public void readFully(byte[] buffer) {
  138. checkPosition(buffer.length);
  139. read(buffer, 0, buffer.length);
  140. }
  141. @Override
  142. public void readPlain(byte[] buf, int off, int len) {
  143. readFully(buf, off, len);
  144. }
  145. /**
  146. * Change the limit of the ByteArrayInputStream
  147. * @param size the new limit - is truncated to length of internal buffer
  148. */
  149. public void limit(int size) {
  150. count = Math.min(size, buf.length);
  151. }
  152. }