/** * 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); }
if(BOMConstants.SUPPORTED_BOMS[i].getEncoding().equalsIgnoreCase(encoding)) return BOMConstants.SUPPORTED_BOMS[i];
/** * 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()); } }
/** * 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); } }
/** * 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")); }