Skip to content

Commit d432620

Browse files
author
Niall Pemberton
committed
IO-178 Fix Bug with firstBytes being consumed by getBOM()
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004087 13f79535-47bb-0310-9956-ffa450edef68
1 parent 94d8c10 commit d432620

2 files changed

Lines changed: 27 additions & 13 deletions

File tree

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,6 @@ public boolean hasBOM(ByteOrderMark bom) throws IOException {
156156
* @throws IOException if an error reading the first bytes of the stream occurs
157157
*/
158158
public ByteOrderMark getBOM() throws IOException {
159-
readFirstBytes();
160-
return byteOrderMark;
161-
}
162-
163-
/**
164-
* This method reads and either preserves or skips the first bytes in the
165-
* stream. It behaves like the single-byte <code>read()</code> method,
166-
* either returning a valid byte or -1 to indicate that the initial bytes
167-
* have been processed already.
168-
* @return the byte read (excluding BOM) or -1 if the end of stream
169-
* @throws IOException if an I/O error occurs
170-
*/
171-
private int readFirstBytes() throws IOException {
172159
if (firstBytes == null) {
173160
int max = 0;
174161
for (ByteOrderMark bom : boms) {
@@ -191,7 +178,19 @@ private int readFirstBytes() throws IOException {
191178
}
192179
}
193180
}
181+
return byteOrderMark;
182+
}
194183

184+
/**
185+
* This method reads and either preserves or skips the first bytes in the
186+
* stream. It behaves like the single-byte <code>read()</code> method,
187+
* either returning a valid byte or -1 to indicate that the initial bytes
188+
* have been processed already.
189+
* @return the byte read (excluding BOM) or -1 if the end of stream
190+
* @throws IOException if an I/O error occurs
191+
*/
192+
private int readFirstBytes() throws IOException {
193+
getBOM();
195194
return (fbIndex < fbLength) ? firstBytes[fbIndex++] : -1;
196195
}
197196

src/test/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,21 @@ public void testReadWithBOMInclude() throws Exception {
175175
assertEquals("getBOM", ByteOrderMark.UTF_8, in.getBOM());
176176
}
177177

178+
public void testGetBOMFirstThenReadInclude() throws Exception {
179+
byte[] data = new byte[] { 'A', 'B', 'C' };
180+
BOMInputStream in = new BOMInputStream(createDataStream(data, true), true);
181+
assertTrue("hasBOM()", in.hasBOM());
182+
assertTrue("hasBOM(UTF-8)", in.hasBOM(ByteOrderMark.UTF_8));
183+
assertEquals("getBOM", ByteOrderMark.UTF_8, in.getBOM());
184+
assertEquals(0xEF, in.read());
185+
assertEquals(0xBB, in.read());
186+
assertEquals(0xBF, in.read());
187+
assertEquals('A', in.read());
188+
assertEquals('B', in.read());
189+
assertEquals('C', in.read());
190+
assertEquals(-1, in.read());
191+
}
192+
178193
public void testReadWithMultipleBOM() throws Exception {
179194
byte[] data = new byte[] { 'A', 'B', 'C' };
180195
BOMInputStream in = new BOMInputStream(createDataStream(data, true),

0 commit comments

Comments
 (0)