/** * 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"); }
/** * 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()); } }
@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]; }
BOMInputStream bin = new BOMInputStream(new ByteArrayInputStream(b, off, len)); BOM bom = bin.getBOM(); while((i=bin.read())!=-1) if(i==0x00) return true;
tempBom = SUPPORTED_BOMS[i]; tempBomSig = tempBom.getSignature(); if(tempBomSig.length>bestMatchLength && startsWith(bytes, tempBomSig)) { bestMatchIndex = i; bestMatchLength = tempBomSig.length;
/** * 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); }
/** * 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); } }
@Override public int read(byte b[]) throws IOException { return read(b, 0, b.length); }
void loadDocument(InputStream in, final String encoding, DocumentListener documentListener) throws IOException { // If the encoding is UTF-something, wrap the stream in a BOMInputStream to filter out the byte-order mark // (see ticket #245) if(encoding != null && encoding.toLowerCase().startsWith("utf")) { in = new BOMInputStream(in); } // If the given encoding is invalid (null or not supported), default to "UTF-8" this.encoding = encoding==null || !Charset.isSupported(encoding) ? "UTF-8" : encoding; textEditorImpl.read(new BufferedReader(new InputStreamReader(in, this.encoding))); // Listen to document changes if(documentListener!=null) textEditorImpl.addDocumentListener(documentListener); }
assert bom.equals(bomIn.getBOM()); assertEOF(bomIn); assert UTF8_BOM.equals(bomIn.getBOM()); assertStreamEquals(new byte[]{(byte)0x27}, bomIn); assertEOF(bomIn); assert bomIn.getBOM() == null; assertStreamEquals(b, bomIn); assert bomIn.getBOM() == null; assertEOF(bomIn); assert !UTF8_BOM.equals(bomIn.getBOM()); assertEOF(bomIn);
private BOMInputStream getBOMInputStream(byte b[]) throws IOException { return new BOMInputStream(new ByteArrayInputStream(b)); }