Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (1.47 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
    public static byte CalcXOR(byte[] data, int offset, int count)
14
    {
15
        byte x = data[offset];
16
        for (int i = 1; i < count; i++) x ^= data[offset + i];
17
        return x;
18
    }
19
20
    private static byte CalcXOR(String data)
21
    {
22
        byte x = (byte)(data.charAt(0));
23
        for (int i = 1; i < data.length(); i++) x ^= (byte)(data.charAt(i));
24
        return x;
25
    }
26
27
    public static byte[] GetBytesFromFile(File file) throws IOException {
28
        InputStream is = new FileInputStream(file);
29
30
        // Get the size of the file
31
        long length = file.length();
32
33
        if (length > Integer.MAX_VALUE) {
34
            throw new IOException("File is too large!");
35
        }
36
37
        // Create the byte array to hold the data
38
        byte[] bytes = new byte[(int)length];
39
40
        // Read in the bytes
41
        int offset = 0;
42
        int numRead = 0;
43
        while (offset < bytes.length
44
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
45
            offset += numRead;
46
        }
47
48
        // Ensure all the bytes have been read in
49
        if (offset < bytes.length) {
50
            throw new IOException("Could not completely read file "+file.getName());
51
        }
52
53
        // Close the input stream and return bytes
54
        is.close();
55
        return bytes;
56
    }
57
}