Example usage for org.apache.commons.compress.compressors.pack200 Pack200CompressorInputStream matches

List of usage examples for org.apache.commons.compress.compressors.pack200 Pack200CompressorInputStream matches

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.pack200 Pack200CompressorInputStream matches.

Prototype

public static boolean matches(final byte[] signature, final int length) 

Source Link

Document

Checks if the signature matches what is expected for a pack200 file (0xCAFED00D).

Usage

From source file:org.apache.tika.parser.pkg.TikaCompressorStreamFactory.java

/**
 * Try to detect the type of compressor stream.
 *
 * @param in input stream//from ww w  .  j  a va 2  s.co  m
 * @return type of compressor stream detected
 * @throws CompressorException if no compressor stream type was detected
 *                             or if something else went wrong
 * @throws IllegalArgumentException if stream is null or does not support mark
 *
 * @since 1.14
 */
public static String detect(final InputStream in) throws CompressorException {
    if (in == null) {
        throw new IllegalArgumentException("Stream must not be null.");
    }

    if (!in.markSupported()) {
        throw new IllegalArgumentException("Mark is not supported.");
    }

    final byte[] signature = new byte[12];
    in.mark(signature.length);
    int signatureLength = -1;
    try {
        signatureLength = IOUtils.readFully(in, signature);
        in.reset();
    } catch (IOException e) {
        throw new CompressorException("IOException while reading signature.", e);
    }

    if (BZip2CompressorInputStream.matches(signature, signatureLength)) {
        return BZIP2;
    }

    if (GzipCompressorInputStream.matches(signature, signatureLength)) {
        return GZIP;
    }

    if (Pack200CompressorInputStream.matches(signature, signatureLength)) {
        return PACK200;
    }

    if (FramedSnappyCompressorInputStream.matches(signature, signatureLength)) {
        return SNAPPY_FRAMED;
    }

    if (ZCompressorInputStream.matches(signature, signatureLength)) {
        return Z;
    }

    if (DeflateCompressorInputStream.matches(signature, signatureLength)) {
        return DEFLATE;
    }

    if (XZUtils.matches(signature, signatureLength)) {
        return XZ;
    }

    if (LZMAUtils.matches(signature, signatureLength)) {
        return LZMA;
    }

    /*            if (FramedLZ4CompressorInputStream.matches(signature, signatureLength)) {
        return LZ4_FRAMED;
    }*/

    throw new CompressorException("No Compressor found for the stream signature.");
}