Project

General

Profile

Statistics
| Revision:

root / trunk / src / java / org / lidar / Util.java

History | View | Annotate | Download (1.52 KB)

1 9 andrej.cim
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 11 andrej.cim
14
    public static byte CalcXOR(byte[] data, int offset, int count) {
15 9 andrej.cim
        byte x = data[offset];
16 11 andrej.cim
        for (int i = 1; i < count; i++) {
17
            x ^= data[offset + i];
18
        }
19 9 andrej.cim
        return x;
20
    }
21
22 11 andrej.cim
    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 9 andrej.cim
        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 11 andrej.cim
        byte[] bytes = new byte[(int) length];
42 9 andrej.cim
43
        // Read in the bytes
44
        int offset = 0;
45
        int numRead = 0;
46
        while (offset < bytes.length
47 11 andrej.cim
                && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
48 9 andrej.cim
            offset += numRead;
49
        }
50
51
        // Ensure all the bytes have been read in
52
        if (offset < bytes.length) {
53 11 andrej.cim
            throw new IOException("Could not completely read file " + file.getName());
54 9 andrej.cim
        }
55
56
        // Close the input stream and return bytes
57
        is.close();
58
        return bytes;
59
    }
60
}