View Javadoc
1 /* 2 * LERandomAccessFile.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 * Version 1.0 1998 January 6 12 * 1.1 1998 January 7 - officially implements DataInput 13 * 1.2 1998 January 9 - add LERandomAccessFile 14 * 1.3 1998 August 27 15 * 1.4 1998 November 10 - add new address and phone. 16 * 1.5 1999 October 8 - use com.mindprod.ledatastream package name. 17 * 18 * Very similar to RandomAccessFile except it reads/writes 19 * little-endian instead of big-endian binary data. 20 * RandomAccessFile methods are final, so we cannot simply extend it. 21 * We have to build a wrapper class. 22 */ 23 package com.mindprod.ledatastream; 24 import java.io.DataInput; 25 import java.io.DataOutput; 26 import java.io.File; 27 import java.io.FileDescriptor; 28 import java.io.IOException; 29 import java.io.RandomAccessFile; 30 31 public class LERandomAccessFile implements DataInput, DataOutput { 32 33 private static final String EmbeddedCopyright = 34 "copyright (c) 1998-2002 Roedy Green, Canadian Mind Products, http://mindprod.com"; 35 36 /*** 37 * constructors 38 */ 39 public LERandomAccessFile(String f, String rw) throws IOException { 40 r = new RandomAccessFile(f, rw); 41 w = new byte[8]; 42 } 43 44 public LERandomAccessFile(File f, String rw) throws IOException { 45 r = new RandomAccessFile(f, rw); 46 w = new byte[8]; 47 } 48 49 // L I T T L E E N D I A N R E A D E R S 50 // Little endian methods for multi-byte numeric types. 51 // Big-endian do fine for single-byte types and strings. 52 53 /*** 54 * like RandomAcessFile.readShort except little endian. 55 */ 56 public final short readShort() throws IOException { 57 r.readFully(w, 0, 2); 58 return (short) ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 59 } 60 61 /*** 62 * like RandomAcessFile.readUnsignedShort except little endian. 63 * Note, returns int even though it reads a short. 64 */ 65 public final int readUnsignedShort() throws IOException { 66 r.readFully(w, 0, 2); 67 return ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 68 } 69 70 /*** 71 * like RandomAcessFile.readChar except little endian. 72 */ 73 public final char readChar() throws IOException { 74 r.readFully(w, 0, 2); 75 return (char) ((w[1] & 0xff) << 8 | (w[0] & 0xff)); 76 } 77 78 /*** 79 * like RandomAcessFile.readInt except little endian. 80 */ 81 public final int readInt() throws IOException { 82 r.readFully(w, 0, 4); 83 return (w[3]) 84 << 24 | (w[2] & 0xff) 85 << 16 | (w[1] & 0xff) 86 << 8 | (w[0] & 0xff); 87 } 88 89 /*** 90 * like RandomAcessFile.readLong except little endian. 91 */ 92 public final long readLong() throws IOException { 93 r.readFully(w, 0, 8); 94 return (long) (w[7]) << 56 | 95 /* long cast necessary or shift done modulo 32 */ 96 (long) (w[6] & 0xff) 97 << 48 | (long) (w[5] & 0xff) 98 << 40 | (long) (w[4] & 0xff) 99 << 32 | (long) (w[3] & 0xff) 100 << 24 | (long) (w[2] & 0xff) 101 << 16 | (long) (w[1] & 0xff) 102 << 8 | (long) (w[0] & 0xff); 103 } 104 105 /*** 106 * like RandomAcessFile.readFloat except little endian. 107 */ 108 public final float readFloat() throws IOException { 109 return Float.intBitsToFloat(readInt()); 110 } 111 112 /*** 113 * like RandomAcessFile.readDouble except little endian. 114 */ 115 public final double readDouble() throws IOException { 116 return Double.longBitsToDouble(readLong()); 117 } 118 119 // L I T T L E E N D I A N W R I T E R S 120 // Little endian methods for multi-byte numeric types. 121 // Big-endian do fine for single-byte types and strings. 122 123 /*** 124 * like RandomAcessFile.writeShort. 125 * also acts as a writeUnsignedShort 126 */ 127 public final void writeShort(int v) throws IOException { 128 w[0] = (byte) v; 129 w[1] = (byte) (v >> 8); 130 r.write(w, 0, 2); 131 } 132 133 /*** 134 * like RandomAcessFile.writeChar. 135 * Note the parm is an int even though this as a writeChar 136 */ 137 public final void writeChar(int v) throws IOException { 138 // same code as writeShort 139 w[0] = (byte) v; 140 w[1] = (byte) (v >> 8); 141 r.write(w, 0, 2); 142 } 143 144 /*** 145 * like RandomAcessFile.writeInt. 146 */ 147 public final void writeInt(int v) throws IOException { 148 w[0] = (byte) v; 149 w[1] = (byte) (v >> 8); 150 w[2] = (byte) (v >> 16); 151 w[3] = (byte) (v >> 24); 152 r.write(w, 0, 4); 153 } 154 155 /*** 156 * like RandomAcessFile.writeLong. 157 */ 158 public final void writeLong(long v) throws IOException { 159 w[0] = (byte) v; 160 w[1] = (byte) (v >> 8); 161 w[2] = (byte) (v >> 16); 162 w[3] = (byte) (v >> 24); 163 w[4] = (byte) (v >> 32); 164 w[5] = (byte) (v >> 40); 165 w[6] = (byte) (v >> 48); 166 w[7] = (byte) (v >> 56); 167 r.write(w, 0, 8); 168 } 169 170 /*** 171 * like RandomAcessFile.writeFloat. 172 */ 173 public final void writeFloat(float v) throws IOException { 174 writeInt(Float.floatToIntBits(v)); 175 } 176 177 /*** 178 * like RandomAcessFile.writeDouble. 179 */ 180 public final void writeDouble(double v) throws IOException { 181 writeLong(Double.doubleToLongBits(v)); 182 } 183 184 /*** 185 * like RandomAcessFile.writeChars, has to flip each char. 186 */ 187 public final void writeChars(String s) throws IOException { 188 int len = s.length(); 189 for (int i = 0; i < len; i++) { 190 writeChar(s.charAt(i)); 191 } 192 } // end writeChars 193 194 // p u r e l y w r a p p e r m e t h o d s 195 196 public final FileDescriptor getFD() throws IOException { 197 return r.getFD(); 198 } 199 200 public final long getFilePointer() throws IOException { 201 return r.getFilePointer(); 202 } 203 204 public final long length() throws IOException { 205 return r.length(); 206 } 207 208 public final int read(byte b[], int off, int len) throws IOException { 209 return r.read(b, off, len); 210 } 211 212 public final int read(byte b[]) throws IOException { 213 return r.read(b); 214 } 215 216 public final int read() throws IOException { 217 return r.read(); 218 } 219 220 public final void readFully(byte b[]) throws IOException { 221 r.readFully(b, 0, b.length); 222 } 223 224 public final void readFully(byte b[], int off, int len) 225 throws IOException { 226 r.readFully(b, off, len); 227 } 228 229 public final int skipBytes(int n) throws IOException { 230 return r.skipBytes(n); 231 } 232 233 /* OK, reads only only 1 byte */ 234 public final boolean readBoolean() throws IOException { 235 return r.readBoolean(); 236 } 237 238 public final byte readByte() throws IOException { 239 return r.readByte(); 240 } 241 242 // note: returns an int, even though says Byte. 243 public final int readUnsignedByte() throws IOException { 244 return r.readUnsignedByte(); 245 } 246 247 public final String readLine() throws IOException { 248 return r.readLine(); 249 } 250 251 public final String readUTF() throws IOException { 252 return r.readUTF(); 253 } 254 255 public final void seek(long pos) throws IOException { 256 r.seek(pos); 257 } 258 259 /* Only writes one byte even though says int */ 260 public final synchronized void write(int b) throws IOException { 261 r.write(b); 262 } 263 264 public final synchronized void write(byte b[], int off, int len) 265 throws IOException { 266 r.write(b, off, len); 267 } 268 269 public final void writeBoolean(boolean v) throws IOException { 270 r.writeBoolean(v); 271 } 272 273 public final void writeByte(int v) throws IOException { 274 r.writeByte(v); 275 } 276 277 public final void writeBytes(String s) throws IOException { 278 r.writeBytes(s); 279 } 280 281 public final void writeUTF(String str) throws IOException { 282 r.writeUTF(str); 283 } 284 285 public final void write(byte b[]) throws IOException { 286 r.write(b, 0, b.length); 287 } 288 289 public final void close() throws IOException { 290 r.close(); 291 } 292 293 // i n s t a n c e v a r i a b l e s 294 295 protected RandomAccessFile r; 296 byte w[]; // work array for buffering input/output 297 298 } // end class LERandomAccessFile

This page was automatically generated by Maven