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