1 |
mojays |
2 |
package appl.parallel.data.xulugridfile; |
2 |
|
|
|
3 |
|
|
import java.io.ByteArrayInputStream; |
4 |
|
|
import java.io.ByteArrayOutputStream; |
5 |
|
|
import java.io.EOFException; |
6 |
|
|
import java.io.IOException; |
7 |
|
|
// fuer Doku |
8 |
|
|
import java.io.RandomAccessFile; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* The methods of this class can be used to efficiently read or write values |
12 |
|
|
* into a Bytestream (it is e.g. used by the {@link XuluGridFile}). |
13 |
|
|
* |
14 |
|
|
* @author Dominik Appl |
15 |
|
|
* |
16 |
|
|
*/ |
17 |
|
|
public class BufferedHelper { |
18 |
|
|
|
19 |
|
|
/** |
20 |
|
|
* Reads a long value out of the given bytestream Used for buffered reading. |
21 |
|
|
* Used the code of {@link RandomAccessFile#readInt()} |
22 |
|
|
* |
23 |
|
|
* @exception IOException |
24 |
|
|
* if an I/O error occurs. |
25 |
|
|
*/ |
26 |
|
|
public static final int readIntFromStream(ByteArrayInputStream input) |
27 |
|
|
throws IOException { |
28 |
|
|
int ch1 = input.read(); |
29 |
|
|
int ch2 = input.read(); |
30 |
|
|
int ch3 = input.read(); |
31 |
|
|
int ch4 = input.read(); |
32 |
|
|
if ((ch1 | ch2 | ch3 | ch4) < 0) |
33 |
|
|
throw new EOFException(); |
34 |
|
|
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Writes a int value in the the given bytestream Used for buffered writing. |
39 |
|
|
* Used the code of {@link RandomAccessFile#writeInt(int)} |
40 |
|
|
* |
41 |
|
|
* @exception IOException |
42 |
|
|
* if an I/O error occurs. |
43 |
|
|
* |
44 |
|
|
*/ |
45 |
|
|
public static final void writeInt(int v, ByteArrayOutputStream output) |
46 |
|
|
throws IOException { |
47 |
|
|
output.write((v >>> 24) & 0xFF); |
48 |
|
|
output.write((v >>> 16) & 0xFF); |
49 |
|
|
output.write((v >>> 8) & 0xFF); |
50 |
|
|
output.write((v >>> 0) & 0xFF); |
51 |
|
|
// written += 4; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
/** |
55 |
|
|
* Writes a <code>long</code> to the stream as eight bytes, high byte |
56 |
|
|
* first. The write starts at the current position of the file pointer. Used |
57 |
|
|
* for buffered writing. Used the code of |
58 |
|
|
* {@link RandomAccessFile#writeLong(long)} |
59 |
|
|
* |
60 |
|
|
* @param output the stream to be written to |
61 |
|
|
* @param v |
62 |
|
|
* a <code>long</code> to be written. |
63 |
|
|
* @exception IOException |
64 |
|
|
* if an I/O error occurs. |
65 |
|
|
*/ |
66 |
|
|
public static final void writeLong(long v, ByteArrayOutputStream output) |
67 |
|
|
throws IOException { |
68 |
|
|
output.write((int) (v >>> 56) & 0xFF); |
69 |
|
|
output.write((int) (v >>> 48) & 0xFF); |
70 |
|
|
output.write((int) (v >>> 40) & 0xFF); |
71 |
|
|
output.write((int) (v >>> 32) & 0xFF); |
72 |
|
|
output.write((int) (v >>> 24) & 0xFF); |
73 |
|
|
output.write((int) (v >>> 16) & 0xFF); |
74 |
|
|
output.write((int) (v >>> 8) & 0xFF); |
75 |
|
|
output.write((int) (v >>> 0) & 0xFF); |
76 |
|
|
// written += 8; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/** |
80 |
|
|
* Reads a long value out of the given {@link ByteArrayInputStream}. Used for buffered |
81 |
|
|
* reading. Used the code of {@link RandomAccessFile#readLong()} * |
82 |
|
|
* |
83 |
|
|
* @exception IOException |
84 |
|
|
* if an I/O error occurs. |
85 |
|
|
*/ |
86 |
|
|
public static final long readLongFromStream(ByteArrayInputStream input) |
87 |
|
|
throws IOException { |
88 |
|
|
return ((long) (readIntFromStream(input) << 32) + (readIntFromStream(input) & 0xFFFFFFFFL)); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
} |