View Javadoc
1 /* 2 * LEDataInputStream.java 3 * 4 * copyright (c) 1998-2002 Roedy Green, Canadian Mind Products 5 * #327 - 964 Heywood Avenue 6 * Victoria, BC Canada V8V 2Y5 7 * tel: (250) 361-9093 8 * mailto:roedy@mindprod.com 9 * http://mindprod.com 10 * 11 * 12 * Version 1.0 1998 January 6 13 * 1.1 1998 January 7 - officially implements DataInput 14 * 1.2 1998 January 9 - add LERandomAccessFile 15 * 1.3 1998 August 27 - fix bug, readFully instead of read. 16 * 1.4 1998 November 10 - add address and phone. 17 * 1.5 1999 October 8 - use com.mindprod.ledatastream package name. 18 * 19 * Very similar to DataInputStream except it reads little-endian instead of 20 * big-endian binary data. 21 * We can't extend DataInputStream directly since it has only final methods. 22 * This forces us implement LEDataInputStream with a DataInputStream object, 23 * and use wrapper methods. 24 */ 25 package com.mindprod.ledatastream; 26 import java.io.DataInput; 27 import java.io.DataInputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 31 public class LEDataInputStream implements DataInput { 32 33 private static final String EmbeddedCopyright = 34 "copyright (c) 1998-2002 Roedy Green, Canadian Mind Products, http://mindprod.com"; 35 36 /*** 37 * constructor 38 */ 39 public LEDataInputStream(InputStream in) { 40 this.in = in; 41 this.d = new DataInputStream(in); 42 w = new byte[8]; 43 } 44 45 // L I T T L E E N D I A N R E A D E R S 46 // Little endian methods for multi-byte numeric types. 47 // Big-endian do fine for single-byte types and strings. 48 /*** 49 * like DataInputStream.readShort except little endian. 50 */ 51 public final short readShort() throws IOException { 52 d.readFully(w, 0, 2); 53 return (short) ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 54 } 55 56 /*** 57 * like DataInputStream.readUnsignedShort except little endian. 58 * Note, returns int even though it reads a short. 59 */ 60 public final int readUnsignedShort() throws IOException { 61 d.readFully(w, 0, 2); 62 return ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 63 } 64 65 /*** 66 * like DataInputStream.readChar except little endian. 67 */ 68 public final char readChar() throws IOException { 69 d.readFully(w, 0, 2); 70 return (char) ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 71 } 72 73 /*** 74 * like DataInputStream.readInt except little endian. 75 */ 76 public final int readInt() throws IOException { 77 d.readFully(w, 0, 4); 78 return (w[3]) 79 << 24 | (w[2] & 0xff) 80 << 16 | (w[1] & 0xff) 81 << 8 | (w[0] & 0xff); 82 } 83 84 /*** 85 * like DataInputStream.readLong except little endian. 86 */ 87 public final long readLong() throws IOException { 88 d.readFully(w, 0, 8); 89 return (long) (w[7]) << 56 | 90 /* long cast needed or shift done modulo 32 */ 91 (long) (w[6] & 0xff) 92 << 48 | (long) (w[5] & 0xff) 93 << 40 | (long) (w[4] & 0xff) 94 << 32 | (long) (w[3] & 0xff) 95 << 24 | (long) (w[2] & 0xff) 96 << 16 | (long) (w[1] & 0xff) 97 << 8 | (long) (w[0] & 0xff); 98 } 99 100 /*** 101 * like DataInputStream.readFloat except little endian. 102 */ 103 public final float readFloat() throws IOException { 104 return Float.intBitsToFloat(readInt()); 105 } 106 107 /*** 108 * like DataInputStream.readDouble except little endian. 109 */ 110 public final double readDouble() throws IOException { 111 return Double.longBitsToDouble(readLong()); 112 } 113 114 // p u r e l y w r a p p e r m e t h o d s 115 // We can't simply inherit since dataInputStream is final. 116 117 /* Watch out, may return fewer bytes than requested. */ 118 public final int read(byte b[], int off, int len) throws IOException { 119 // For efficiency, we avoid one layer of wrapper 120 return in.read(b, off, len); 121 } 122 123 public final void readFully(byte b[]) throws IOException { 124 d.readFully(b, 0, b.length); 125 } 126 127 public final void readFully(byte b[], int off, int len) 128 throws IOException { 129 d.readFully(b, off, len); 130 } 131 132 public final int skipBytes(int n) throws IOException { 133 return d.skipBytes(n); 134 } 135 136 /* only reads one byte */ 137 public final boolean readBoolean() throws IOException { 138 return d.readBoolean(); 139 } 140 141 public final byte readByte() throws IOException { 142 return d.readByte(); 143 } 144 145 // note: returns an int, even though says Byte. 146 public final int readUnsignedByte() throws IOException { 147 return d.readUnsignedByte(); 148 } 149 150 /*** 151 * @see java.io.DataInput#readLine() 152 * @deprecated 153 */ 154 public final String readLine() throws IOException { 155 return d.readLine(); 156 } 157 158 public final String readUTF() throws IOException { 159 return d.readUTF(); 160 } 161 162 // Note. This is a STATIC method! 163 public final static String readUTF(DataInput in) throws IOException { 164 return DataInputStream.readUTF(in); 165 } 166 167 public final void close() throws IOException { 168 d.close(); 169 } 170 171 public final void reset() throws IOException { 172 d.reset(); 173 w = new byte[8]; 174 } 175 176 // i n s t a n c e v a r i a b l e s 177 178 protected DataInputStream d; 179 // to get at high level readFully methods of DataInputStream 180 protected InputStream in; 181 // to get at the low-level read methods of InputStream 182 byte w[]; // work array for buffering input 183 184 } // end class LEDataInputStream

This page was automatically generated by Maven