/** * 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)); }
/** * 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")); }