File: BomStrip.java

package info (click to toggle)
bomstrip 9-17
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 256 kB
  • sloc: pascal: 41; ansic: 34; cpp: 34; java: 33; python: 31; sh: 24; makefile: 19; perl: 7; php: 6; ruby: 6; haskell: 6; awk: 2; sed: 1
file content (37 lines) | stat: -rw-r--r-- 1,006 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class BomStrip {
	
	private static final int BUFFER_LENGTH = 65536;
	private static final int EOF = -1;
	private static final byte UTF8_BOM_1 = (byte) 0xef;
	private static final byte UTF8_BOM_2 = (byte) 0xbb;
	private static final byte UTF8_BOM_3 = (byte) 0xbf;
	
	
	public static void main(String[] args) {
		final byte[] utf8Bom = {UTF8_BOM_1, UTF8_BOM_2, UTF8_BOM_3};
		byte[] buffer = new byte[BUFFER_LENGTH];	
		byte[] bomBuffer = new byte[utf8Bom.length];

		try {
			int nRead = System.in.read(bomBuffer, 0, bomBuffer.length);
			if (nRead != EOF) {
				if (!java.util.Arrays.equals(bomBuffer, utf8Bom)) {
					System.out.write(bomBuffer, 0, nRead);
				}
				boolean eof = false;
				while (!eof) {
					nRead = System.in.read(buffer, 0, buffer.length);
					eof = nRead == EOF;
					if (!eof) {
						System.out.write(buffer, 0, nRead);
					}	
				}
			}
		}
		catch (java.io.IOException e)
		{
			System.err.println("I/O error occurred: " + e.getMessage());
			System.exit(1);	
		}
	}
}