View difference between Paste ID: RueiK6Mb and c27wfZYK
SHOW: | | - or go back to the newest paste.
1
class EncodindsCheck implements Checker {
2
	private static final int UTF8_HEADER_SIZE = 8;
3
4
	@Override
5
	public boolean check(File currentFile) {
6-
		if (isUTF8(currentFile)) {
6+
		return isUTF8(currentFile);
7-
			return true;
7+
8-
		} else {
8+
9-
			return false;
9+
10
		// validate input
11
		if (null == file) {
12
			throw new IllegalArgumentException("input file can't be null");
13
		}
14
		if (file.isDirectory()) {
15
			throw new IllegalArgumentException(
16
					"input file refers to a directory");
17
		}
18
19
		// read input file
20
		byte[] buffer;
21
		try {
22
			buffer = readUTFHeaderBytes(file);
23
		} catch (IOException e) {
24
			throw new IllegalArgumentException(
25
					"Can't read input file, error = " + e.getLocalizedMessage());
26
		}
27
28
		if ((buffer[0] & 0xF8) == 0xF0) {
29
			if (((buffer[1] & 0xC0) == 0x80)
30
					&& ((buffer[2] == 0x80) && ((buffer[3] == 0x80))))
31
				return true;
32
		} else if ((buffer[0] & 0xF0) == 0xE0) {
33
			if (((buffer[1] & 0xC0) == 0x80) && ((buffer[2] & 0xC0) == 0x80))
34
				return true;
35
		} else if ((buffer[0] & 0xE0) == 0xC0) {
36
			if (((buffer[1] & 0xC0) == 0x80))
37
				return true;
38
		}
39
40
		return false;
41
	}
42
43
	private static byte[] readUTFHeaderBytes(File input) throws IOException {
44
		byte[] buffer = new byte[UTF8_HEADER_SIZE];
45
		// read data
46
		FileInputStream fis = new FileInputStream(input);
47
		fis.read(buffer);
48
		fis.close();
49
		return buffer;
50
	}
51
}