/** * 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()); } }
/** * 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>BOMReader</code>. A {@link BOMInputStream} is created on top of the specified * <code>InputStream</code> to auto-detect a potential {@link BOM} and use the associated encoding. * If no BOM is found at the beginning of the stream, <code>UTF-8</code> encoding is assumed. * * @param in the underlying InputStream * @throws IOException if an error occurred while detecting the BOM or initializing this reader. */ public BOMReader(InputStream in) throws IOException { this(new BOMInputStream(in), "UTF-8"); }
/** * 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); bomIn = getBOMInputStream(b); assert UTF8_BOM.equals(bomIn.getBOM()); assertStreamEquals(new byte[]{(byte)0x27}, bomIn); assertEOF(bomIn); bomIn = getBOMInputStream(b); assert bomIn.getBOM() == null; assertStreamEquals(b, bomIn); bomIn = getBOMInputStream(b); assert bomIn.getBOM() == null; assertEOF(bomIn); b = UTF16_BE_BOM.getSignature(); bomIn = getBOMInputStream(b); assert !UTF8_BOM.equals(bomIn.getBOM()); assertEOF(bomIn);
BOMInputStream bin = new BOMInputStream(new ByteArrayInputStream(b, off, len)); BOM bom = bin.getBOM(); if(bom.equals(BOMConstants.UTF16_BE_BOM) || bom.equals(BOMConstants.UTF16_LE_BOM) || bom.equals(BOMConstants.UTF32_BE_BOM) || bom.equals(BOMConstants.UTF32_LE_BOM)) { return false; while((i=bin.read())!=-1) if(i==0x00) return 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()); }
/** * 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); }
/** * 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); }
/** * 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; } }
private void write(OutputStream out) throws IOException { textEditorImpl.write(new BOMWriter(out, textViewerDelegate.getEncoding())); }
@Override public void write(int c) throws IOException { checkWriteBOM(); super.write(c); }
@Override public int read() throws IOException { if(oneByteBuf==null) oneByteBuf = new byte[1]; int ret = read(oneByteBuf, 0, 1); return ret==-1?-1:oneByteBuf[0]; }
private void assertStreamEquals(byte bytes[], InputStream in) throws IOException { for (byte b : bytes) assert b == (byte) (in.read() & 0xFF); assertEOF(in); } }
/** * Creates a new <code>BOMReader</code>. A {@link BOMInputStream} is created on top of the specified * <code>InputStream</code> to auto-detect a potential {@link BOM} and use the associated encoding. * If no BOM is found at the beginning of the stream, the specified default encoding is assumed. * * @param in the underlying InputStream * @param defaultEncoding the encoding used if the stream doesn't contain a BOM * @throws IOException if an error occurred while detecting the BOM or initializing this reader. */ public BOMReader(InputStream in, String defaultEncoding) throws IOException { this(new BOMInputStream(in), defaultEncoding); }
@Override public void write(String str, int off, int len) throws IOException { checkWriteBOM(); super.write(str, off, len); } }
@Override public int read(byte b[]) throws IOException { return read(b, 0, b.length); }
private BOMInputStream getBOMInputStream(byte b[]) throws IOException { return new BOMInputStream(new ByteArrayInputStream(b)); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { checkWriteBOM(); super.write(cbuf, off, len); }