/** * 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"); }
/** * Checks if the signature matches what is expected for a jar file * (in this case it is the same as for a zip file). * * @param signature * the bytes to check * @param length * the number of bytes to check * @return true, if this stream is a jar archive stream, false otherwise */ public new static bool matches(byte[] signature, int length) { return(ZipArchiveInputStream.matches(signature, length)); }