All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javolution.xml.internal.stream.UnicodeBomInputStream Maven / Gradle / Ivy

Go to download

Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.

There is a newer version: 6.11.8
Show newest version
/* ____________________________________________________________________________
 * 
 * File:    UnicodeBOMInputStream.java
 * Author:  Gregory Pakosz.
 * Date:    02 - November - 2005    
 * ____________________________________________________________________________
 */
package javolution.xml.internal.stream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

/**
 * The UnicodeBOMInputStream class wraps any
 * InputStream and detects the presence of any Unicode BOM
 * (Byte Order Mark) at its beginning, as defined by
 * RFC 3629 - UTF-8, a transformation format of ISO 10646
 * 

*

The * Unicode FAQ * defines 5 types of BOMs:

    *
  • 00 00 FE FF  = UTF-32, big-endian
  • *
  • FF FE 00 00  = UTF-32, little-endian
  • *
  • FE FF        = UTF-16, big-endian
  • *
  • FF FE        = UTF-16, little-endian
  • *
  • EF BB BF     = UTF-8
  • *

*

*

Use the {@link #getBOM()} method to know whether a BOM has been detected * or not. *

*

Use the {@link #skipBOM()} method to remove the detected BOM from the * wrapped InputStream object.

*/ public class UnicodeBomInputStream extends InputStream { private final PushbackInputStream in; private final BOM bom; private boolean skipped = false; /** * Constructs a new UnicodeBOMInputStream that wraps the * specified InputStream. * * @param inputStream an InputStream. * @throws NullPointerException when inputStream is * null. * @throws IOException on reading from the specified InputStream * when trying to detect the Unicode BOM. */ public UnicodeBomInputStream(final InputStream inputStream) throws NullPointerException, IOException { if (inputStream == null) throw new NullPointerException("invalid input stream: null is not allowed"); in = new PushbackInputStream(inputStream, 4); final byte bom[] = new byte[4]; final int read = in.read(bom); switch (read) { case 4: if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) { this.bom = BOM.UTF_32_LE; break; } else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) { this.bom = BOM.UTF_32_BE; break; } case 3: if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) { this.bom = BOM.UTF_8; break; } case 2: if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) { this.bom = BOM.UTF_16_LE; break; } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) { this.bom = BOM.UTF_16_BE; break; } default: this.bom = BOM.NONE; break; } if (read > 0) in.unread(bom, 0, read); } /** * Returns the BOM that was detected in the wrapped * InputStream object. * * @return a BOM value. */ public final BOM getBOM() { // BOM type is immutable. return bom; } /** * Skips the BOM that was found in the wrapped * InputStream object. * * @return this UnicodeBOMInputStream. * @throws IOException when trying to skip the BOM from the wrapped * InputStream object. */ public final synchronized UnicodeBomInputStream skipBOM() throws IOException { if (!skipped) { in.skip(bom.bytes.length); skipped = true; } return this; } /** * {@inheritDoc} */ public int read() throws IOException { return in.read(); } /** * {@inheritDoc} */ public int read(final byte b[]) throws IOException, NullPointerException { return in.read(b, 0, b.length); } /** * {@inheritDoc} */ public int read(final byte b[], final int off, final int len) throws IOException, NullPointerException { return in.read(b, off, len); } /** * {@inheritDoc} */ public long skip(final long n) throws IOException { return in.skip(n); } /** * {@inheritDoc} */ public int available() throws IOException { return in.available(); } /** * {@inheritDoc} */ public void close() throws IOException { in.close(); } /** * {@inheritDoc} */ public synchronized void mark(final int readlimit) { in.mark(readlimit); } /** * {@inheritDoc} */ public synchronized void reset() throws IOException { in.reset(); } /** * {@inheritDoc} */ public boolean markSupported() { return in.markSupported(); } } // UnicodeBOMInputStream




© 2015 - 2024 Weber Informatics LLC | Privacy Policy