/**
         * Create an compressor input stream from an input stream, autodetecting
         * the compressor type from the first few bytes of the stream. The InputStream
         * must support marks, like BufferedInputStream.
         *
         * @param in the input stream
         * @return the compressor input stream
         * @throws CompressorException if the compressor name is not known
         * @throws IllegalArgumentException if the stream is null or does not support mark
         * @since Commons Compress 1.1
         */
        public CompressorInputStream createCompressorInputStream(java.io.InputStream inJ)
        //throws CompressorException
        {
            if (inJ == null)
            {
                throw new java.lang.IllegalArgumentException("Stream must not be null.");
            }

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

            byte[] signature = new byte[12];
            inJ.mark(signature.Length);
            try {
                int signatureLength = inJ.read(signature);
                inJ.reset();

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

                if (GzipCompressorInputStream.matches(signature, signatureLength))
                {
                    return(new GzipCompressorInputStream(inJ));
                }
            } catch (java.io.IOException e) {
                throw new CompressorException("Failed to detect Compressor from InputStream.", e);
            }

            throw new CompressorException("No Compressor found for the stream signature.");
        }
Example #2
0
        /**
         * Create an archive input stream from an input stream, autodetecting
         * the archive type from the first few bytes of the stream. The InputStream
         * must support marks, like BufferedInputStream.
         *
         * @param in the input stream
         * @return the archive input stream
         * @throws ArchiveException if the archiver name is not known
         * @throws IllegalArgumentException if the stream is null or does not support mark
         */
        public ArchiveInputStream createArchiveInputStream(java.io.InputStream inJ)
        //throws ArchiveException
        {
            if (inJ == null)
            {
                throw new java.lang.IllegalArgumentException("Stream must not be null.");
            }

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

            byte[] signature = new byte[12];
            inJ.mark(signature.Length);
            try {
                int signatureLength = inJ.read(signature);
                inJ.reset();
                if (ZipArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new ZipArchiveInputStream(inJ));
                }
                else if (JarArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new JarArchiveInputStream(inJ));
                }
                else if (ArArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new ArArchiveInputStream(inJ));
                }
                else if (CpioArchiveInputStream.matches(signature, signatureLength))
                {
                    return(new CpioArchiveInputStream(inJ));
                }
                // Tar needs a bigger buffer to check the signature; read the first block
                byte[] tarheader = new byte[512];
                inJ.mark(tarheader.Length);
                signatureLength = inJ.read(tarheader);
                inJ.reset();
                if (TarArchiveInputStream.matches(tarheader, signatureLength))
                {
                    return(new TarArchiveInputStream(inJ));
                }
            } catch (java.io.IOException e) {
                throw new ArchiveException("Could not use reset and mark operations.", e);
            }

            throw new ArchiveException("No Archiver found for the stream signature");
        }