/** * 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."); }
// Gather the records by just splitting on new lines /// <exception cref="System.IO.IOException"/> public virtual string[] ReadRecordsDirectly(Uri testFileUrl, bool bzip) { int MaxDataSize = 1024 * 1024; byte[] data = new byte[MaxDataSize]; FileInputStream fis = new FileInputStream(testFileUrl.GetFile()); int count; if (bzip) { BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis); count = bzIn.Read(data); bzIn.Close(); } else { count = fis.Read(data); } fis.Close(); NUnit.Framework.Assert.IsTrue("Test file data too big for buffer", count < data.Length ); return(Sharpen.Runtime.GetStringForBytes(data, 0, count, "UTF-8").Split("\n")); }
/** * Read informations of a rpm file out of an input stream. * * @param rpmInputStream The input stream representing the rpm file * @throws IOException if an error occurs during read of the rpm file */ private void readFromStream(java.io.InputStream rpmInputStream) {//throws IOException { ByteCountInputStream allCountInputStream = new ByteCountInputStream( rpmInputStream); java.io.InputStream inputStream = new java.io.DataInputStream(allCountInputStream); lead = new RPMLead((java.io.DataInputStream)inputStream); signature = new RPMSignature((java.io.DataInputStream)inputStream, store); if (logger.isLoggable(java.util.logging.Level.FINER)) { logger.finer("Signature Size: " + signature.getSize()); } header = new RPMHeader((java.io.DataInputStream)inputStream, store); if (logger.isLoggable(java.util.logging.Level.FINER)) { logger.finer("Header Size: " + header.getSize()); } DataTypeIf payloadTag = getTag("PAYLOADFORMAT"); String payloadFormat = payloadTag != null?payloadTag.toString() : "cpio"; DataTypeIf payloadCompressionTag = getTag("PAYLOADCOMPRESSOR"); String payloadCompressor = payloadCompressionTag != null?payloadCompressionTag .toString() : "gzip"; if (payloadFormat.equals("cpio")) { if (logger.isLoggable(java.util.logging.Level.FINER)) { logger.finer("PAYLOADCOMPRESSOR: " + payloadCompressor); } if (payloadCompressor.equals("gzip")) { inputStream = new GzipCompressorInputStream(allCountInputStream); } else if (payloadCompressor.equals("bzip2")) { inputStream = new BZip2CompressorInputStream(allCountInputStream); } else if (payloadCompressor.equals("lzma")) { try { java.io.PipedOutputStream pout = new java.io.PipedOutputStream(); inputStream = new java.io.PipedInputStream(pout); byte[] properties = new byte[5]; if (allCountInputStream.read(properties, 0, 5) != 5) { throw (new java.io.IOException("input .lzma is too short")); } SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder(); decoder.SetDecoderProperties(properties); long outSize = 0; for (int i = 0; i < 8; i++) { int v = allCountInputStream.read(); if (v < 0) { throw (new java.io.IOException("lzma error : Can't Read 1")); } outSize |= ((long)v) << (8 * i); } if (outSize == -1) { outSize = java.lang.Long.MAX_VALUE; } Decode decoderRunnable = new Decode(decoder, allCountInputStream, pout, outSize); java.lang.Thread t = new java.lang.Thread(decoderRunnable, "LZMA Decoder"); t.start(); } catch (java.lang.NoClassDefFoundError) { String message = "No LZMA library found. Attach p7zip library to classpath (http://p7zip.sourceforge.net/)"; logger.severe(message); throw new java.io.IOException(message); } } else if (payloadCompressor.equals("none")) { inputStream = allCountInputStream; } else { throw new java.io.IOException("Unsupported compressor type " + payloadCompressor); } ByteCountInputStream countInputStream = new ByteCountInputStream(inputStream); CpioArchiveInputStream cpioInputStream = new CpioArchiveInputStream(countInputStream); CpioArchiveEntry readEntry; java.util.List <String> fileNamesList = new java.util.ArrayList <String>(); String fileEntry; while ((readEntry = cpioInputStream.getNextCPIOEntry()) != null) { if (logger.isLoggable(java.util.logging.Level.FINER)) { logger.finer("Read CPIO entry: " + readEntry.getName() + " ;mode:" + readEntry.getMode()); } if (readEntry.isRegularFile() || readEntry.isSymbolicLink() || readEntry.isDirectory()) { fileEntry = readEntry.getName(); if (fileEntry.startsWith("./")) { fileEntry = fileEntry.substring(1); } fileNamesList.add(fileEntry); } } store.setTag("FILENAMES", TypeFactory.createSTRING_ARRAY((String[])fileNamesList.toArray(new String[fileNamesList.size()]))); setHeaderTagFromSignature("ARCHIVESIZE", "PAYLOADSIZE"); // check ARCHIVESIZE with countInputStream.getCount(); Object archiveSizeObject = getTag("ARCHIVESIZE"); if (archiveSizeObject != null) { if (archiveSizeObject is INT32) { int archiveSize = ((INT32)archiveSizeObject).getData()[0]; if (archiveSize != countInputStream.getCount()) { new java.io.IOException("ARCHIVESIZE not correct"); } } } store.setTag("J_ARCHIVESIZE", TypeFactory .createINT64(new long[] { countInputStream.getCount() })); } else { throw new java.io.IOException("Unsupported Payload type " + payloadFormat); } // filling in signatures // TODO: check signatures! setHeaderTagFromSignature("SIGSIZE", "SIZE"); setHeaderTagFromSignature("SIGLEMD5_1", "LEMD5_1"); setHeaderTagFromSignature("SIGPGP", "PGP"); setHeaderTagFromSignature("SIGLEMD5_2", "LEMD5_2"); setHeaderTagFromSignature("SIGMD5", "MD5"); setHeaderTagFromSignature("SIGGPG", "GPG"); setHeaderTagFromSignature("SIGPGP5", "PGP5"); setHeaderTagFromSignature("DSAHEADER", "DSA"); setHeaderTagFromSignature("RSAHEADER", "RSA"); setHeaderTagFromSignature("SHA1HEADER", "SHA1"); store.setTag("J_FILESIZE", TypeFactory .createINT64(new long[] { allCountInputStream.getCount() })); rpmInputStream.close(); }