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.

LittleEndianInputStream.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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.BufferedInputStream;
  17. import java.io.FilterInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. /**
  21. * Wraps an {@link InputStream} providing {@link LittleEndianInput}<p>
  22. *
  23. * This class does not buffer any input, so the stream read position maintained
  24. * by this class is consistent with that of the inner stream.
  25. */
  26. public class LittleEndianInputStream extends FilterInputStream implements LittleEndianInput {
  27. private static final int BUFFERED_SIZE = 8096;
  28. private static final int EOF = -1;
  29. private int readIndex = 0;
  30. private int markIndex = -1;
  31. public LittleEndianInputStream(InputStream is) {
  32. super(is.markSupported() ? is : new BufferedInputStream(is, BUFFERED_SIZE));
  33. }
  34. @Override
  35. @SuppressForbidden("just delegating")
  36. public int available() {
  37. try {
  38. return super.available();
  39. } catch (IOException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43. @Override
  44. public byte readByte() {
  45. return (byte)readUByte();
  46. }
  47. @Override
  48. public int readUByte() {
  49. byte buf[] = new byte[1];
  50. try {
  51. checkEOF(read(buf), 1);
  52. } catch (IOException e) {
  53. throw new RuntimeException(e);
  54. }
  55. return LittleEndian.getUByte(buf);
  56. }
  57. /**
  58. * get a float value, reads it in little endian format
  59. * then converts the resulting revolting IEEE 754 (curse them) floating
  60. * point number to a happy java float
  61. *
  62. * @return the float (32-bit) value
  63. */
  64. public float readFloat() {
  65. return Float.intBitsToFloat( readInt() );
  66. }
  67. @Override
  68. public double readDouble() {
  69. return Double.longBitsToDouble(readLong());
  70. }
  71. @Override
  72. public int readInt() {
  73. byte buf[] = new byte[LittleEndianConsts.INT_SIZE];
  74. try {
  75. checkEOF(read(buf), buf.length);
  76. } catch (IOException e) {
  77. throw new RuntimeException(e);
  78. }
  79. return LittleEndian.getInt(buf);
  80. }
  81. /**
  82. * get an unsigned int value from an InputStream
  83. *
  84. * @return the unsigned int (32-bit) value
  85. * @exception RuntimeException
  86. * wraps any IOException thrown from reading the stream.
  87. */
  88. //@Override
  89. public long readUInt() {
  90. long retNum = readInt();
  91. return retNum & 0x00FFFFFFFFL;
  92. }
  93. @Override
  94. public long readLong() {
  95. byte buf[] = new byte[LittleEndianConsts.LONG_SIZE];
  96. try {
  97. checkEOF(read(buf), LittleEndianConsts.LONG_SIZE);
  98. } catch (IOException e) {
  99. throw new RuntimeException(e);
  100. }
  101. return LittleEndian.getLong(buf);
  102. }
  103. @Override
  104. public short readShort() {
  105. return (short)readUShort();
  106. }
  107. @Override
  108. public int readUShort() {
  109. byte buf[] = new byte[LittleEndianConsts.SHORT_SIZE];
  110. try {
  111. checkEOF(read(buf), LittleEndianConsts.SHORT_SIZE);
  112. } catch (IOException e) {
  113. throw new RuntimeException(e);
  114. }
  115. return LittleEndian.getUShort(buf);
  116. }
  117. private static void checkEOF(int actualBytes, int expectedBytes) {
  118. if (expectedBytes != 0 && (actualBytes == -1 || actualBytes != expectedBytes)) {
  119. throw new RuntimeException("Unexpected end-of-file");
  120. }
  121. }
  122. @Override
  123. public void readFully(byte[] buf) {
  124. readFully(buf, 0, buf.length);
  125. }
  126. @Override
  127. public void readFully(byte[] buf, int off, int len) {
  128. try {
  129. checkEOF(_read(buf, off, len), len);
  130. } catch (IOException e) {
  131. throw new RuntimeException(e);
  132. }
  133. }
  134. @Override
  135. public int read(byte[] b, int off, int len) throws IOException {
  136. int readBytes = super.read(b, off, len);
  137. readIndex += readBytes;
  138. return readBytes;
  139. }
  140. @Override
  141. public synchronized void mark(int readlimit) {
  142. super.mark(readlimit);
  143. markIndex = readIndex;
  144. }
  145. @Override
  146. public synchronized void reset() throws IOException {
  147. super.reset();
  148. if (markIndex > -1) {
  149. readIndex = markIndex;
  150. markIndex = -1;
  151. }
  152. }
  153. public int getReadIndex() {
  154. return readIndex;
  155. }
  156. //Makes repeated calls to super.read() until length is read or EOF is reached
  157. private int _read(byte[] buffer, int offset, int length) throws IOException {
  158. //lifted directly from org.apache.commons.io.IOUtils 2.4
  159. int remaining = length;
  160. while (remaining > 0) {
  161. int location = length - remaining;
  162. int count = read(buffer, offset + location, remaining);
  163. if (EOF == count) {
  164. break;
  165. }
  166. remaining -= count;
  167. }
  168. return length - remaining;
  169. }
  170. @Override
  171. public void readPlain(byte[] buf, int off, int len) {
  172. readFully(buf, off, len);
  173. }
  174. public void skipFully(int len) throws IOException {
  175. IOUtils.skipFully(this, len);
  176. }
  177. }