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