Skip to content

Commit ea84049

Browse files
author
Niall Pemberton
committed
IO-178 Extend BOMInputStream from excluding UTF-8 BOMs to detecting and optionally excluding any BOM
- Rename BOMExclusionInputStream to BOMInputStream - Add new ByteOrderMark implementation - Enhance BOMInputStream from only processing UTF-8 BOMs to be able to configure for any BOM - Provide ability to get/check the BOM detected - Provide facility to either include or exclude the detected BOM git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004073 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3c14167 commit ea84049

5 files changed

Lines changed: 732 additions & 186 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io;
18+
19+
import java.io.Serializable;
20+
21+
/**
22+
* Byte Order Mark (BOM) representation.
23+
*
24+
* @see org.apache.commons.io.input.BOMInputStream
25+
* @see <a href="http://en.wikipedia.org/wiki/Byte_order_mark">Wikipedia - Byte Order Mark<a>
26+
* @version $Id$
27+
* @Since Commons IO 2.0
28+
*/
29+
public class ByteOrderMark implements Serializable {
30+
31+
private static final long serialVersionUID = 1L;
32+
33+
/** UTF-8 BOM */
34+
public static final ByteOrderMark UTF_8 = new ByteOrderMark("UTF-8", 0xEF, 0xBB, 0xBF);
35+
/** UTF-16BE BOM (Big Endian) */
36+
public static final ByteOrderMark UTF_16BE = new ByteOrderMark("UTF-16BE", 0xFE, 0xFF);
37+
/** UTF-16LE BOM (Little Endian) */
38+
public static final ByteOrderMark UTF_16LE = new ByteOrderMark("UTF-16LE", 0xFF, 0xFE);
39+
40+
private final String charsetName;
41+
private final int[] bytes;
42+
43+
/**
44+
* Construct a new BOM.
45+
*
46+
* @param charsetName The name of the charset the BOM represents
47+
* @param bytes The BOM's bytes
48+
* @throws IllegalArgumentException if the charsetName is null or
49+
* zero length
50+
* @throws IllegalArgumentException if the bytes are null or zero
51+
* length
52+
*/
53+
public ByteOrderMark(String charsetName, int... bytes) {
54+
if (charsetName == null || charsetName.length() == 0) {
55+
throw new IllegalArgumentException("No charsetName specified");
56+
}
57+
if (bytes == null || bytes.length == 0) {
58+
throw new IllegalArgumentException("No bytes specified");
59+
}
60+
this.charsetName = charsetName;
61+
this.bytes = new int[bytes.length];
62+
System.arraycopy(bytes, 0, this.bytes, 0, bytes.length);
63+
}
64+
65+
/**
66+
* Return the name of the {@link java.nio.charset.Charset} the BOM represents.
67+
*
68+
* @return the character set name
69+
*/
70+
public String getCharsetName() {
71+
return charsetName;
72+
}
73+
74+
/**
75+
* Return the length of the BOM's bytes.
76+
*
77+
* @return the length of the BOM's bytes
78+
*/
79+
public int length() {
80+
return bytes.length;
81+
}
82+
83+
/**
84+
* The byte at the specified position.
85+
*
86+
* @param pos The position
87+
* @return The specified byte
88+
*/
89+
public int get(int pos) {
90+
return bytes[pos];
91+
}
92+
93+
/**
94+
* Return a copy of the BOM's bytes.
95+
*
96+
* @return a copy of the BOM's bytes
97+
*/
98+
public byte[] getBytes() {
99+
byte[] copy = new byte[bytes.length];
100+
for (int i = 0; i < bytes.length; i++) {
101+
copy[i] = (byte)bytes[i];
102+
}
103+
return copy;
104+
}
105+
106+
/**
107+
* Indicates if this BOM's bytes equals another.
108+
*
109+
* @param obj The object to compare to
110+
* @return true if the bom's bytes are equal, otherwise
111+
* false
112+
*/
113+
@Override
114+
public boolean equals(Object obj) {
115+
if (!(obj instanceof ByteOrderMark)) {
116+
return false;
117+
}
118+
ByteOrderMark bom = (ByteOrderMark)obj;
119+
if (bytes.length != bom.length()) {
120+
return false;
121+
}
122+
for (int i = 0; i < bytes.length; i++) {
123+
if (bytes[i] != bom.get(i)) {
124+
return false;
125+
}
126+
}
127+
return true;
128+
}
129+
130+
/**
131+
* Return the hashcode for this BOM.
132+
*
133+
* @return the hashcode for this BOM.
134+
* @see java.lang.Object#hashCode()
135+
*/
136+
@Override
137+
public int hashCode() {
138+
int hashCode = getClass().hashCode();
139+
for (int b : bytes) {
140+
hashCode += b;
141+
}
142+
return hashCode;
143+
}
144+
145+
/**
146+
* Provide a String representation of the BOM.
147+
*
148+
* @return the length of the BOM's bytes
149+
*/
150+
@Override
151+
public String toString() {
152+
StringBuilder builder = new StringBuilder();
153+
builder.append(getClass().getSimpleName());
154+
builder.append('[');
155+
builder.append(charsetName);
156+
builder.append(": ");
157+
for (int i = 0; i < bytes.length; i++) {
158+
if (i > 0) {
159+
builder.append(",");
160+
}
161+
builder.append("0x");
162+
Integer.toBinaryString(0);
163+
Integer.toOctalString(0);
164+
Integer.toHexString(0);
165+
builder.append(Integer.toHexString(0xFF & bytes[i]).toUpperCase());
166+
}
167+
builder.append(']');
168+
return builder.toString();
169+
}
170+
171+
}

src/java/org/apache/commons/io/input/BOMExclusionInputStream.java

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)