@Test public void testMarkResetBeforeReadWithBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C', 'D' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, true)); assertTrue(in.markSupported()); in.mark(10); in.read(); in.read(); in.reset(); assertEquals('A', in.read()); in.close(); }
@Test public void testSkipWithBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C', 'D' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, true)); in.skip(2L); assertEquals('C', in.read()); in.close(); }
@Test public void testLeadingNonBOMBufferedRead() throws Exception { final byte[] data = new byte[] { (byte) 0xEF, (byte) 0xAB, (byte) 0xCD }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, false)); final byte[] buf = new byte[1024]; assertData(data, buf, in.read(buf)); in.close(); }
@Test public void testLargeBufferWithBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, true)); final byte[] buf = new byte[1024]; assertData(data, buf, in.read(buf)); in.close(); }
@Test public void testAvailableWithBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C', 'D' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, true)); assertEquals(7, in.available()); in.close(); }
@Test public void testEmptyBufferWithoutBOM() throws Exception { final byte[] data = new byte[] {}; final InputStream in = new BOMInputStream(createUtf8DataStream(data, false)); final byte[] buf = new byte[1024]; assertEquals(-1, in.read(buf)); in.close(); }
@Test public void testLargeBufferWithoutBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, false)); final byte[] buf = new byte[1024]; assertData(data, buf, in.read(buf)); in.close(); }
@Test public void testSmallBufferWithoutBOM() throws Exception { final byte[] data = new byte[] { 'A', 'B', 'C' }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, false)); final byte[] buf = new byte[1024]; assertData(new byte[] { 'A', 'B' }, buf, in.read(buf, 0, 2)); assertData(new byte[] { 'C' }, buf, in.read(buf, 0, 2)); in.close(); }
@Test public void testLeadingNonBOMSingleRead() throws Exception { final byte[] data = new byte[] { (byte) 0xEF, (byte) 0xAB, (byte) 0xCD }; final InputStream in = new BOMInputStream(createUtf8DataStream(data, false)); assertEquals(0xEF, in.read()); assertEquals(0xAB, in.read()); assertEquals(0xCD, in.read()); assertEquals(-1, in.read()); in.close(); }
@Test // this is here for coverage public void testClose() throws Exception { final ExpectCloseInputStream del = new ExpectCloseInputStream(); final InputStream in = new BOMInputStream(del); in.close(); del.assertCloseCalled(); del.close(); }
private void readBOMInputStreamTwice(final String resource) throws Exception { final InputStream inputStream = this.getClass().getResourceAsStream(resource); Assert.assertNotNull(inputStream); final BOMInputStream bomInputStream = new BOMInputStream(inputStream); bomInputStream.mark(1000000); this.readFile(bomInputStream); bomInputStream.reset(); this.readFile(bomInputStream); inputStream.close(); bomInputStream.close(); }
@Test public void testReadXmlWithBOMUtf8() throws Exception { final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><X/>".getBytes(StandardCharsets.UTF_8); parseXml(new BOMInputStream(createUtf8DataStream(data, true))); parseXml(createUtf8DataStream(data, true)); }
@Test public void testReadXmlWithBOMUtf16Be() throws Exception { final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16BE\"?><X/>".getBytes(StandardCharsets.UTF_16BE); parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)); parseXml(createUtf16BeDataStream(data, true)); }
@Test public void testReadXmlWithBOMUtf16Le() throws Exception { final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16LE\"?><X/>".getBytes(StandardCharsets.UTF_16LE); parseXml(new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE)); parseXml(createUtf16LeDataStream(data, true)); }
@Test public void testReadXmlWithoutBOMUtf32Le() throws Exception { Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE")); final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE"); parseXml(new BOMInputStream(createUtf32LeDataStream(data, false))); parseXml(createUtf32BeDataStream(data, false)); }
@Test public void testReadXmlWithoutBOMUtf32Be() throws Exception { Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE")); final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF_32BE\"?><X/>".getBytes("UTF_32BE"); parseXml(new BOMInputStream(createUtf32BeDataStream(data, false))); parseXml(createUtf32BeDataStream(data, false)); }
@Test public void testReadXmlWithBOMUtf32Le() throws Exception { Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE")); final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE"); parseXml(new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE)); // XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader parseXml(new XmlStreamReader(createUtf32LeDataStream(data, true))); }
@Test public void skipReturnValueWithBom() throws IOException { final byte[] baseData = new byte[]{(byte) 0x31, (byte) 0x32, (byte) 0x33}; final BOMInputStream is1 = new BOMInputStream(createUtf8DataStream(baseData, true)); assertEquals(2, is1.skip(2)); assertEquals((byte) 0x33, is1.read()); is1.close(); }
@Test public void testReadEmpty() throws Exception { final byte[] data = new byte[] {}; final BOMInputStream in = new BOMInputStream(createUtf8DataStream(data, false)); assertEquals(-1, in.read()); assertFalse("hasBOM()", in.hasBOM()); assertFalse("hasBOM(UTF-8)", in.hasBOM(ByteOrderMark.UTF_8)); assertNull("getBOM", in.getBOM()); in.close(); }
@Test public void testReadSmall() throws Exception { final byte[] data = new byte[] { 'A', 'B' }; final BOMInputStream in = new BOMInputStream(createUtf8DataStream(data, false)); assertEquals('A', in.read()); assertEquals('B', in.read()); assertEquals(-1, in.read()); assertFalse("hasBOM()", in.hasBOM()); assertFalse("hasBOM(UTF-8)", in.hasBOM(ByteOrderMark.UTF_8)); assertNull("getBOM", in.getBOM()); in.close(); }