Example usage for java.io PushbackInputStream read

List of usage examples for java.io PushbackInputStream read

Introduction

In this page you can find the example usage for java.io PushbackInputStream read.

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

/**
 * Creates a reader from the given input stream and encoding.
 * This method assumes the input stream working buffer is at least
 * 128 byte long. The input stream is restored before this method
 * returns. The 4 first bytes are skipped before creating the reader.
 *//*from  www. j  a  v  a 2 s.  c  o  m*/
protected static Reader createXMLDeclarationReader(PushbackInputStream pbis, String enc) throws IOException {
    byte[] buf = new byte[128];
    int len = pbis.read(buf);

    if (len > 0) {
        pbis.unread(buf, 0, len);
    }

    return new InputStreamReader(new ByteArrayInputStream(buf, 4, len), enc);
}

From source file:Main.java

/**
 * Checks the InputStream if it contains  GZIP compressed data
 *
 * @param inputStream InputStream to be checked
 * @return true or false if the stream contains GZIP compressed data
 * @throws java.io.IOException/*w w  w .j a va2s  .  c om*/
 */
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
    if (inputStream == null)
        return false;

    byte[] signature = new byte[2];
    int readStatus = inputStream.read(signature);
    inputStream.unread(signature);
    int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
    return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}

From source file:eu.europeana.uim.sugarcrmclient.internal.ExtendedSaajSoapMessageFactory.java

/**
 * Detects if the incoming stream is Gzip encoded
 * /*from w  w w. j  a v a2 s.  co  m*/
 * @param pb
 * @return an InputStream/GZIPInputStream 
 * @throws IOException
 */
public static InputStream decompressStream(PushbackInputStream pb) throws IOException {
    byte[] signature = new byte[2];
    pb.read(signature);
    pb.unread(signature);

    if (signature[0] == 31 && signature[1] == -117)
        return new GZIPInputStream(pb);
    else
        return pb;
}

From source file:it.unifi.rcl.chess.traceanalysis.Trace.java

/**
 * Checks if the input stream is compressed, and in case it returns a GZIPInputStream
 * @param stream/*from w w w.  ja  va  2s  . c om*/
 * @return
 * @throws IOException
 */
private static boolean checkGZIP(InputStream stream) throws IOException {
    PushbackInputStream pb = new PushbackInputStream(stream, 2); //we need a pushbackstream to look ahead
    byte[] signature = new byte[2];
    pb.read(signature); //read the signature
    pb.unread(signature); //push back the signature to the stream
    if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) //check if matches standard gzip magic number
        return true;
    else
        return false;
}

From source file:eu.scape_project.up2ti.container.ArcContainer.java

@Override
public void init(String containerFileName, InputStream containerFileStream) throws IOException {
    this.containerFileName = containerFileName;
    // Read first two bytes to check if we have a gzipped input stream
    PushbackInputStream pb = new PushbackInputStream(containerFileStream, 2);
    byte[] signature = new byte[2];
    pb.read(signature);
    pb.unread(signature);/*  ww w  .  j  a  v a2  s .c om*/
    // use compressed reader if gzip magic number is matched
    if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) {
        reader = ArcReaderFactory.getReaderCompressed(pb);
    } else {
        reader = ArcReaderFactory.getReaderUncompressed(pb);
    }
    archiveRecords = new ArrayList<ArcRecordBase>();
    // initialise object by create temporary files and the bidirectional 
    // file-record map.
    arcRecContentsToTempFiles();
}

From source file:eu.europeana.uim.sugarcrmclient.internal.ExtendedSaajSoapMessageFactory.java

/**
 * Checks for the UTF-8 Byte Order Mark, and removes it if present. The SAAJ RI cannot cope with these BOMs.
 *
 * @see <a href="http://jira.springframework.org/browse/SWS-393">SWS-393</a>
 * @see <a href="http://unicode.org/faq/utf_bom.html#22">UTF-8 BOMs</a>
 *///from  w w  w. j  av  a 2 s  .c  o  m
private InputStream checkForUtf8ByteOrderMark(InputStream inputStream) throws IOException {
    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
    byte[] bom = new byte[3];
    if (pushbackInputStream.read(bom) != -1) {
        // check for the UTF-8 BOM, and remove it if there. See SWS-393
        if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
            pushbackInputStream.unread(bom);
        }
    }
    return pushbackInputStream;
}

From source file:z.hol.net.http.entity.DeflateDecompressingEntity.java

/**
 * Returns the non-null InputStream that should be returned to by all requests to
 * {@link #getContent()}.//from www .  ja  v  a  2  s.  c o  m
 *
 * @return a non-null InputStream
 * @throws IOException if there was a problem
 */
@Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
    /*
     * A zlib stream will have a header.
     *
     * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
     *
     * * CMF is one byte.
     *
     * * FLG is one byte.
     *
     * * DICTID is four bytes, and only present if FLG.FDICT is set.
     *
     * Sniff the content. Does it look like a zlib stream, with a CMF, etc? c.f. RFC1950,
     * section 2.2. http://tools.ietf.org/html/rfc1950#page-4
     *
     * We need to see if it looks like a proper zlib stream, or whether it is just a deflate
     * stream. RFC2616 calls zlib streams deflate. Confusing, isn't it? That's why some servers
     * implement deflate Content-Encoding using deflate streams, rather than zlib streams.
     *
     * We could start looking at the bytes, but to be honest, someone else has already read
     * the RFCs and implemented that for us. So we'll just use the JDK libraries and exception
     * handling to do this. If that proves slow, then we could potentially change this to check
     * the first byte - does it look like a CMF? What about the second byte - does it look like
     * a FLG, etc.
     */

    /* We read a small buffer to sniff the content. */
    byte[] peeked = new byte[6];

    PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length);

    int headerLength = pushback.read(peeked);

    if (headerLength == -1) {
        throw new IOException("Unable to read the response");
    }

    /* We try to read the first uncompressed byte. */
    byte[] dummy = new byte[1];

    Inflater inf = new Inflater();

    try {
        int n;
        while ((n = inf.inflate(dummy)) == 0) {
            if (inf.finished()) {

                /* Not expecting this, so fail loudly. */
                throw new IOException("Unable to read the response");
            }

            if (inf.needsDictionary()) {

                /* Need dictionary - then it must be zlib stream with DICTID part? */
                break;
            }

            if (inf.needsInput()) {
                inf.setInput(peeked);
            }
        }

        if (n == -1) {
            throw new IOException("Unable to read the response");
        }

        /*
         * We read something without a problem, so it's a valid zlib stream. Just need to reset
         * and return an unused InputStream now.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback);
    } catch (DataFormatException e) {

        /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream and try
         * again. */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback, new Inflater(true));
    }
}

From source file:eu.scape_project.tpid.ContainerProcessing.java

/**
 * Prepare input//  w  ww .  j a  v  a 2 s .  com
 *
 * @param pt
 * @throws IOException IO Error
 * @throws java.lang.InterruptedException
 */
public void prepareInput(Path pt) throws InterruptedException, IOException {
    FileSystem fs = FileSystem.get(context.getConfiguration());
    InputStream containerFileStream = fs.open(pt);
    String containerFileName = pt.getName();

    ArcReader reader;
    // Read first two bytes to check if we have a gzipped input stream
    PushbackInputStream pb = new PushbackInputStream(containerFileStream, 2);
    byte[] signature = new byte[2];
    pb.read(signature);
    pb.unread(signature);
    // use compressed reader if gzip magic number is matched
    if (signature[0] == (byte) 0x1f && signature[1] == (byte) 0x8b) {
        reader = ArcReaderFactory.getReaderCompressed(pb);
    } else {
        reader = ArcReaderFactory.getReaderUncompressed(pb);
    }
    long currTM = System.currentTimeMillis();
    String unpackHdfsPath = conf.get("unpack_hdfs_path", "tpid_unpacked");
    String hdfsUnpackDirStr = StringUtils.normdir(unpackHdfsPath, Long.toString(currTM));
    String hdfsJoboutputPath = conf.get("tooloutput_hdfs_path", "tpid_tooloutput");
    String hdfsOutputDirStr = StringUtils.normdir(hdfsJoboutputPath, Long.toString(currTM));

    Iterator<ArcRecordBase> arcIterator = reader.iterator();

    // Number of files which should be processed per invokation
    int numItemsPerInvocation = conf.getInt("num_items_per_task", 50);
    int numItemCounter = numItemsPerInvocation;
    // List of input files to be processed
    String inliststr = "";
    // List of output files to be generated
    String outliststr = "";
    try {
        while (arcIterator.hasNext()) {
            ArcRecordBase arcRecord = arcIterator.next();
            String recordKey = getRecordKey(arcRecord, containerFileName);
            String outFileName = RandomStringUtils.randomAlphabetic(25);
            String hdfsPathStr = hdfsUnpackDirStr + outFileName;
            Path hdfsPath = new Path(hdfsPathStr);
            String outputFileSuffix = conf.get("output_file_suffix", ".fits.xml");
            String hdfsOutPathStr = hdfsOutputDirStr + outFileName + outputFileSuffix;
            FSDataOutputStream hdfsOutStream = fs.create(hdfsPath);
            ArcUtils.recordToOutputStream(arcRecord, hdfsOutStream);
            Text key = new Text(recordKey);
            Text value = new Text(fs.getHomeDirectory() + File.separator + hdfsOutPathStr);
            mos.write("keyfilmapping", key, value);
            String scapePlatformInvoke = conf.get("scape_platform_invoke", "fits dirxml");
            Text ptmrkey = new Text(scapePlatformInvoke);
            // for the configured number of items per invokation, add the 
            // files to the input and output list of the command.
            inliststr += "," + fs.getHomeDirectory() + File.separator + hdfsPathStr;
            outliststr += "," + fs.getHomeDirectory() + File.separator + hdfsOutPathStr;
            if (numItemCounter > 1 && arcIterator.hasNext()) {
                numItemCounter--;
            } else if (numItemCounter == 1 || !arcIterator.hasNext()) {
                inliststr = inliststr.substring(1); // cut off leading comma 
                outliststr = outliststr.substring(1); // cut off leading comma 
                String pattern = conf.get("tomar_param_pattern", "%1$s %2$s");
                String ptMrStr = StringUtils.formatCommandOutput(pattern, inliststr, outliststr);
                Text ptmrvalue = new Text(ptMrStr);
                // emit tomar input line where the key is the tool invokation
                // (tool + operation) and the value is the parameter list
                // where input and output strings contain file lists.
                mos.write("tomarinput", ptmrkey, ptmrvalue);
                numItemCounter = numItemsPerInvocation;
                inliststr = "";
                outliststr = "";
            }
        }
    } catch (Exception ex) {
        mos.write("error", new Text("Error"), new Text(pt.toString()));
    }

}

From source file:org.mcxiaoke.commons.http.impl.DeflateDecompressingEntity.java

/**
 * Returns the non-null InputStream that should be returned to by all
 * requests to {@link #getContent()}.//  w  ww. j  a v  a 2 s . c om
 * 
 * @return a non-null InputStream
 * @throws IOException
 *             if there was a problem
 */
@Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
    /*
     * A zlib stream will have a header.
     * 
     * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
     * 
     * * CMF is one byte.
     * 
     * * FLG is one byte.
     * 
     * * DICTID is four bytes, and only present if FLG.FDICT is set.
     * 
     * Sniff the content. Does it look like a zlib stream, with a CMF, etc?
     * c.f. RFC1950, section 2.2. http://tools.ietf.org/html/rfc1950#page-4
     * 
     * We need to see if it looks like a proper zlib stream, or whether it
     * is just a deflate stream. RFC2616 calls zlib streams deflate.
     * Confusing, isn't it? That's why some servers implement deflate
     * Content-Encoding using deflate streams, rather than zlib streams.
     * 
     * We could start looking at the bytes, but to be honest, someone else
     * has already read the RFCs and implemented that for us. So we'll just
     * use the JDK libraries and exception handling to do this. If that
     * proves slow, then we could potentially change this to check the first
     * byte - does it look like a CMF? What about the second byte - does it
     * look like a FLG, etc.
     */

    /* We read a small buffer to sniff the content. */
    byte[] peeked = new byte[6];

    PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length);

    int headerLength = pushback.read(peeked);

    if (headerLength == -1) {
        throw new IOException("Unable to read the response");
    }

    /* We try to read the first uncompressed byte. */
    byte[] dummy = new byte[1];

    Inflater inf = new Inflater();

    try {
        int n;
        while ((n = inf.inflate(dummy)) == 0) {
            if (inf.finished()) {

                /* Not expecting this, so fail loudly. */
                throw new IOException("Unable to read the response");
            }

            if (inf.needsDictionary()) {

                /*
                 * Need dictionary - then it must be zlib stream with DICTID
                 * part?
                 */
                break;
            }

            if (inf.needsInput()) {
                inf.setInput(peeked);
            }
        }

        if (n == -1) {
            throw new IOException("Unable to read the response");
        }

        /*
         * We read something without a problem, so it's a valid zlib stream.
         * Just need to reset and return an unused InputStream now.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback);
    } catch (DataFormatException e) {

        /*
         * Presume that it's an RFC1951 deflate stream rather than RFC1950
         * zlib stream and try again.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback, new Inflater(true));
    }
}

From source file:com.fanfou.app.opensource.http.support.DeflateDecompressingEntity.java

/**
 * Returns the non-null InputStream that should be returned to by all
 * requests to {@link #getContent()}.// w w w  . j a  v a2s .co m
 * 
 * @return a non-null InputStream
 * @throws IOException
 *             if there was a problem
 */
@Override
InputStream getDecompressingInputStream(final InputStream wrapped) throws IOException {
    /*
     * A zlib stream will have a header.
     * 
     * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 |
     * 
     * * CMF is one byte.
     * 
     * * FLG is one byte.
     * 
     * * DICTID is four bytes, and only present if FLG.FDICT is set.
     * 
     * Sniff the content. Does it look like a zlib stream, with a CMF, etc?
     * c.f. RFC1950, section 2.2. http://tools.ietf.org/html/rfc1950#page-4
     * 
     * We need to see if it looks like a proper zlib stream, or whether it
     * is just a deflate stream. RFC2616 calls zlib streams deflate.
     * Confusing, isn't it? That's why some servers implement deflate
     * Content-Encoding using deflate streams, rather than zlib streams.
     * 
     * We could start looking at the bytes, but to be honest, someone else
     * has already read the RFCs and implemented that for us. So we'll just
     * use the JDK libraries and exception handling to do this. If that
     * proves slow, then we could potentially change this to check the first
     * byte - does it look like a CMF? What about the second byte - does it
     * look like a FLG, etc.
     */

    /* We read a small buffer to sniff the content. */
    final byte[] peeked = new byte[6];

    final PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length);

    final int headerLength = pushback.read(peeked);

    if (headerLength == -1) {
        throw new IOException("Unable to read the response");
    }

    /* We try to read the first uncompressed byte. */
    final byte[] dummy = new byte[1];

    final Inflater inf = new Inflater();

    try {
        int n;
        while ((n = inf.inflate(dummy)) == 0) {
            if (inf.finished()) {

                /* Not expecting this, so fail loudly. */
                throw new IOException("Unable to read the response");
            }

            if (inf.needsDictionary()) {

                /*
                 * Need dictionary - then it must be zlib stream with DICTID
                 * part?
                 */
                break;
            }

            if (inf.needsInput()) {
                inf.setInput(peeked);
            }
        }

        if (n == -1) {
            throw new IOException("Unable to read the response");
        }

        /*
         * We read something without a problem, so it's a valid zlib stream.
         * Just need to reset and return an unused InputStream now.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback);
    } catch (final DataFormatException e) {

        /*
         * Presume that it's an RFC1951 deflate stream rather than RFC1950
         * zlib stream and try again.
         */
        pushback.unread(peeked, 0, headerLength);
        return new InflaterInputStream(pushback, new Inflater(true));
    }
}