LittleEndianDataInputStream.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package min3d.parser;
  2. import java.io.DataInput;
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. /**
  7. * Taken from http://www.peterfranza.com/2008/09/26/little-endian-input-stream/
  8. * @author dennis.ippel
  9. *
  10. */
  11. public class LittleEndianDataInputStream extends InputStream implements DataInput {
  12. public LittleEndianDataInputStream(InputStream in) {
  13. this.in = in;
  14. this.d = new DataInputStream(in);
  15. w = new byte[8];
  16. }
  17. public int available() throws IOException {
  18. return d.available();
  19. }
  20. public final short readShort() throws IOException
  21. {
  22. d.readFully(w, 0, 2);
  23. return (short)(
  24. (w[1]&0xff) << 8 |
  25. (w[0]&0xff));
  26. }
  27. public String readString(int length) throws IOException {
  28. if (length == 0) {
  29. return null;
  30. }
  31. byte[] b = new byte[length];
  32. d.readFully(b);
  33. return new String(b, "US-ASCII");
  34. }
  35. /**
  36. * Note, returns int even though it reads a short.
  37. */
  38. public final int readUnsignedShort() throws IOException
  39. {
  40. d.readFully(w, 0, 2);
  41. return (
  42. (w[1]&0xff) << 8 |
  43. (w[0]&0xff));
  44. }
  45. /**
  46. * like DataInputStream.readChar except little endian.
  47. */
  48. public final char readChar() throws IOException
  49. {
  50. d.readFully(w, 0, 2);
  51. return (char) (
  52. (w[1]&0xff) << 8 |
  53. (w[0]&0xff));
  54. }
  55. /**
  56. * like DataInputStream.readInt except little endian.
  57. */
  58. public final int readInt() throws IOException
  59. {
  60. d.readFully(w, 0, 4);
  61. return
  62. (w[3]) << 24 |
  63. (w[2]&0xff) << 16 |
  64. (w[1]&0xff) << 8 |
  65. (w[0]&0xff);
  66. }
  67. /**
  68. * like DataInputStream.readLong except little endian.
  69. */
  70. public final long readLong() throws IOException
  71. {
  72. d.readFully(w, 0, 8);
  73. return
  74. (long)(w[7]) << 56 |
  75. (long)(w[6]&0xff) << 48 |
  76. (long)(w[5]&0xff) << 40 |
  77. (long)(w[4]&0xff) << 32 |
  78. (long)(w[3]&0xff) << 24 |
  79. (long)(w[2]&0xff) << 16 |
  80. (long)(w[1]&0xff) << 8 |
  81. (long)(w[0]&0xff);
  82. }
  83. public final float readFloat() throws IOException {
  84. return Float.intBitsToFloat(readInt());
  85. }
  86. public final double readDouble() throws IOException {
  87. return Double.longBitsToDouble(readLong());
  88. }
  89. public final int read(byte b[], int off, int len) throws IOException {
  90. return in.read(b, off, len);
  91. }
  92. public final void readFully(byte b[]) throws IOException {
  93. d.readFully(b, 0, b.length);
  94. }
  95. public final void readFully(byte b[], int off, int len) throws IOException {
  96. d.readFully(b, off, len);
  97. }
  98. public final int skipBytes(int n) throws IOException {
  99. return d.skipBytes(n);
  100. }
  101. public final boolean readBoolean() throws IOException {
  102. return d.readBoolean();
  103. }
  104. public final byte readByte() throws IOException {
  105. return d.readByte();
  106. }
  107. public int read() throws IOException {
  108. return in.read();
  109. }
  110. public final int readUnsignedByte() throws IOException {
  111. return d.readUnsignedByte();
  112. }
  113. @Deprecated
  114. public final String readLine() throws IOException {
  115. return d.readLine();
  116. }
  117. public final String readUTF() throws IOException {
  118. return d.readUTF();
  119. }
  120. public final void close() throws IOException {
  121. d.close();
  122. }
  123. private DataInputStream d; // to get at high level readFully methods of
  124. // DataInputStream
  125. private InputStream in; // to get at the low-level read methods of
  126. // InputStream
  127. private byte w[]; // work array for buffering input
  128. }