My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This helper class was written by sabre150 and published on
// http://forums.sun.com/thread.jspa?threadID=5310966

package org.paradise.etrc.data;

import java.io.InputStream;
import java.io.PushbackInputStream;
import java.io.IOException;

public class BOMStripperInputStream extends PushbackInputStream
{
public static final int[][] BOMS =
{
{
0x00, 0x00, 0xFE, 0xFF
},
{
0xFF, 0xFE, 0x00, 0x00
},
{
0x2B, 0x2F, 0x76, 0x38
},
{
0x2B, 0x2F, 0x76, 0x39
},
{
0x2B, 0x2F, 0x76, 0x2B
},
{
0x2B, 0x2F, 0x76, 0x2F
},
{
0xDD, 0x73, 0x66, 0x73
},
{
0xEF, 0xBB, 0xBF
},
{
0x0E, 0xFE, 0xFF
},
{
0xFB, 0xEE, 0x28
},
{
0xFE, 0xFF
},
{
0xFF, 0xFE
}
};

static private int testForBOM(int[] bom, int[] bytes)
{
for (int index = 0; index < bom.length; index++)
{
if (bom[index] != bytes[index])
return 0;
}
return bom.length;
}

public BOMStripperInputStream(InputStream is) throws IOException
{
super(is, 4);

final int[] bytes =
{
read(), read(), read(), read()
};
int count = 0;
for (int[] bom : BOMS)
{
count = testForBOM(bom, bytes);
if (count != 0)
break;
}
for (int index = bytes.length - 1; index >= count; index--)
{
if (bytes[index] != -1)
unread(bytes[index]);
}
}
}

Change log

r62 by lifanxi on May 25, 2010   Diff
Add credit to the BOMStripperInputStream
class
Go to: 
Project members, sign in to write a code review

Older revisions

r31 by lifanxi on Sep 11, 2009   Diff
[BUG] Fixed a bug when loading text
file encoded in UTF-8, the BOM will
cause errors in getting the data.
All revisions of this file

File info

Size: 1852 bytes, 83 lines
Powered by Google Project Hosting