root / trunk / src / java / org / lidar / Util.java @ 12
History | View | Annotate | Download (1.52 KB)
1 |
package org.lidar; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
|
8 |
/**
|
9 |
* Util
|
10 |
* @author Andrej Cimpersek
|
11 |
*/
|
12 |
public class Util { |
13 |
|
14 |
public static byte CalcXOR(byte[] data, int offset, int count) { |
15 |
byte x = data[offset];
|
16 |
for (int i = 1; i < count; i++) { |
17 |
x ^= data[offset + i]; |
18 |
} |
19 |
return x;
|
20 |
} |
21 |
|
22 |
private static byte CalcXOR(String data) { |
23 |
byte x = (byte) (data.charAt(0)); |
24 |
for (int i = 1; i < data.length(); i++) { |
25 |
x ^= (byte) (data.charAt(i));
|
26 |
} |
27 |
return x;
|
28 |
} |
29 |
|
30 |
public static byte[] GetBytesFromFile(File file) throws IOException { |
31 |
InputStream is = new FileInputStream(file); |
32 |
|
33 |
// Get the size of the file
|
34 |
long length = file.length();
|
35 |
|
36 |
if (length > Integer.MAX_VALUE) { |
37 |
throw new IOException("File is too large!"); |
38 |
} |
39 |
|
40 |
// Create the byte array to hold the data
|
41 |
byte[] bytes = new byte[(int) length]; |
42 |
|
43 |
// Read in the bytes
|
44 |
int offset = 0; |
45 |
int numRead = 0; |
46 |
while (offset < bytes.length
|
47 |
&& (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
|
48 |
offset += numRead; |
49 |
} |
50 |
|
51 |
// Ensure all the bytes have been read in
|
52 |
if (offset < bytes.length) {
|
53 |
throw new IOException("Could not completely read file " + file.getName()); |
54 |
} |
55 |
|
56 |
// Close the input stream and return bytes
|
57 |
is.close(); |
58 |
return bytes;
|
59 |
} |
60 |
} |