/** * Creates a new <code>BOMWriter</code> that will write the specified BOM at the beginning of the stream and * subsequently encode characters in the encoding returned by {@link BOM#getEncoding()}. * * @param out the <code>OutputStream</code> to write the encoded data to * @param bom the byte-order mark to write at the beginning of the stream. * @throws UnsupportedEncodingException if the BOM's encoding is not a character encoding supported by the Java runtime. */ public BOMWriter(OutputStream out, BOM bom) throws UnsupportedEncodingException { this(out, bom.getEncoding(), bom); }
/** * Creates a new <code>BOMWriter</code> that will encode characters in the specified encoding and, if the * encoding has a corresponding {@link BOM}, write at the beginning of the stream. If a Non-Unicode encoding * is passed, no BOM will be written to the stream and this <code>Writer</code> will act as a regular * {@link OutputStreamWriter}. * * <p>It is important to note that a BOM will always be written for Unicode encodings, * even if the particular encoding specifies that no BOM should be written (<code>UnicodeLittleUnmarked</code> for * instance). See {@link BOM#getInstance(String)} for more information about this.</p> * * @param out the <code>OutputStream</code> to write the encoded data to * @param encoding character encoding to use for encoding characters. * @throws UnsupportedEncodingException if the specified encoding is not a character encoding supported by the Java runtime. * @see BOM#getInstance(String) */ public BOMWriter(OutputStream out, String encoding) throws UnsupportedEncodingException { this(out, encoding, BOM.getInstance(encoding)); }
/** * Checks if a BOM is waiting for being written, and if there is, writes it to the underlying output stream. * * @throws IOException if an error occurred while writing the BOM */ protected void checkWriteBOM() throws IOException { if(!bomWriteChecked) { if(bom!=null) out.write(bom.getSignature()); bomWriteChecked = true; } }
/** * Tests {@link BOM} comparison methods. */ @Test public void testBOMComparisons() { // Tests BOM#sigStartsWith method assert UTF8_BOM.sigStartsWith(new byte[]{(byte)0xEF, (byte)0xBB}); assert UTF8_BOM.sigStartsWith(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}); assert !UTF8_BOM.sigStartsWith(new byte[]{(byte)0xAA}); assert !UTF8_BOM.sigStartsWith(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF, (byte)0xAA}); // Tests BOM#sigEquals method assert UTF8_BOM.sigEquals(UTF8_BOM.getSignature()); assert !UTF8_BOM.sigEquals(UTF16_LE_BOM.getSignature()); // Tests BOM#equals method assert UTF8_BOM.equals(UTF8_BOM); assert !UTF8_BOM.equals(UTF16_LE_BOM); assert !UTF8_BOM.equals(new Object()); }
/** * Tests {@link BOM#getInstance(String)}. */ @Test public void testBOMResolution() { for (BOM bom : SUPPORTED_BOMS) { // Test case variations assert bom.equals(BOM.getInstance(bom.getEncoding().toLowerCase())); assert bom.equals(BOM.getInstance(bom.getEncoding().toUpperCase())); } // Test non-UTF encodings assert BOM.getInstance("ISO-8859-1") == null; assert BOM.getInstance("Shift_JIS") == null; // Test UTF aliases assert BOMConstants.UTF16_BE_BOM.equals(BOM.getInstance("UnicodeBig")); assert BOMConstants.UTF16_BE_BOM.equals(BOM.getInstance("UnicodeBigUnmarked")); assert BOMConstants.UTF16_BE_BOM.equals(BOM.getInstance("UTF-16")); assert BOMConstants.UTF16_LE_BOM.equals(BOM.getInstance("UnicodeLittle")); assert BOMConstants.UTF16_LE_BOM.equals(BOM.getInstance("UnicodeLittleUnmarked")); assert BOMConstants.UTF32_BE_BOM.equals(BOM.getInstance("UTF-32")); }
/** * Tests {@link BOMWriter}. * * @throws IOException should not happen */ @Test public void testBOMWriter() throws IOException { String testString = "This is a test"; ByteArrayOutputStream baos; BOMWriter bomWriter; BOMInputStream bomIn; for (BOM bom : SUPPORTED_BOMS) { baos = new ByteArrayOutputStream(); bomWriter = new BOMWriter(baos, bom.getEncoding()); bomWriter.write(testString); bomWriter.close(); bomIn = getBOMInputStream(baos.toByteArray()); assert bom.equals(bomIn.getBOM()); assertStreamEquals(testString.getBytes(bom.getEncoding()), bomIn); assertEOF(bomIn); } }
bomIn = getBOMInputStream(bom.getSignature()); assert bom.equals(bomIn.getBOM()); assertEOF(bomIn); assert UTF8_BOM.equals(bomIn.getBOM()); assertStreamEquals(new byte[]{(byte)0x27}, bomIn); assertEOF(bomIn); b = UTF16_BE_BOM.getSignature(); bomIn = getBOMInputStream(b); assert !UTF8_BOM.equals(bomIn.getBOM()); assertEOF(bomIn);
if(BOMConstants.SUPPORTED_BOMS[i].getEncoding().equalsIgnoreCase(encoding)) return BOMConstants.SUPPORTED_BOMS[i]; aliases = BOMConstants.SUPPORTED_BOMS[i].getAliases(); for (String alias : aliases) if (alias.equalsIgnoreCase(encoding))
/** * Returns <code>true</code> if and only if the given Object is a <code>BOM</code> instance with the same * signature as this instance. * * * @param o the Object to test for equality * @return true if the specified Object is a BOM instance with the same signature as this instance */ public boolean equals(Object o) { return (o instanceof BOM) && ((BOM)o).sigEquals(sig); }
/** * Returns <code>true</code> if this BOM's signature matches the given byte sequence. * * @param bytes the byte sequence to compare against this BOM's signature * @return true if this BOM's signature matches the given byte sequence */ public boolean sigEquals(byte bytes[]) { return bytes.length==sig.length && sigStartsWith(bytes); }
/** * Creates a new <code>BOMReader</code> using the given {@link BOMInputStream}. If the <code>BOMInputStream</code> * does not contain a {@link BOM}, the specified default encoding is assumed. * * @param bomIn the underlying BOMInputStream * @param defaultEncoding the encoding used if the stream doesn't contain a BOM * @throws UnsupportedEncodingException if the encoding associated with the BOM or the default encoding is not * supported by the Java runtime */ public BOMReader(BOMInputStream bomIn, String defaultEncoding) throws UnsupportedEncodingException { super(bomIn, bomIn.getBOM()==null?defaultEncoding:bomIn.getBOM().getEncoding()); } }
tempBomSig = tempBom.getSignature(); if(tempBomSig.length>bestMatchLength && startsWith(bytes, tempBomSig)) { bestMatchIndex = i;