Beispiel #1
0
        /*
         * Reads all the bytes from the given input stream.
         *
         * Calls read multiple times on the given input stream until it receives an
         * end of file marker. Returns the combined results as a byte array. Note
         * that this method may block if the underlying stream read blocks.
         *
         * @param is
         *            the input stream to be read.
         * @return the content of the stream as a byte array.
         * @throws IOException
         *             if a read error occurs.
         */
        public static byte[] readFullyAndClose(java.io.InputStream isJ) // throws IOException {
        {
            try {
                // Initial read
                byte[] buffer   = new byte[1024];
                int    count    = isJ.read(buffer);
                int    nextByte = isJ.read();

                // Did we get it all in one read?
                if (nextByte == -1)
                {
                    byte[] dest = new byte[count];
                    java.lang.SystemJ.arraycopy(buffer, 0, dest, 0, count);
                    return(dest);
                }

                // Requires additional reads
                java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(count * 2);
                baos.write(buffer, 0, count);
                baos.write(nextByte);
                while (true)
                {
                    count = isJ.read(buffer);
                    if (count == -1)
                    {
                        return(baos.toByteArray());
                    }
                    baos.write(buffer, 0, count);
                }
            } finally {
                isJ.close();
            }
        }
 /**
  * Returns the {@code Manifest} object associated with this {@code JarFile}
  * or {@code null} if no MANIFEST entry exists.
  *
  * @return the MANIFEST.
  * @throws IOException
  *             if an error occurs reading the MANIFEST file.
  * @throws IllegalStateException
  *             if the jar file is closed.
  * @see Manifest
  */
 public Manifest getManifest() // throws IOException {
 {
     if (closed)
     {
         // archive.35=JarFile has been closed
         throw new java.lang.IllegalStateException("JarFile has been closed"); //$NON-NLS-1$
     }
     if (manifest != null)
     {
         return(manifest);
     }
     try {
         java.io.InputStream isJ = base.getInputStream(manifestEntry);
         if (verifier != null)
         {
             verifier.addMetaEntry(manifestEntry.getName(),
                                   org.apache.harmony.luni.util.InputStreamHelper.readFullyAndClose(isJ));
             isJ = base.getInputStream(manifestEntry);
         }
         try {
             manifest = new Manifest(isJ, verifier != null);
         } finally {
             isJ.close();
         }
         manifestEntry = null;  // Can discard the entry now.
     } catch (java.lang.NullPointerException) {
         manifestEntry = null;
     }
     return(manifest);
 }
 public override void close() //throws IOException
 {
     if (!closed)
     {
         closed = true;
         inJ.close();
     }
 }
 /**
  * Closes the CPIO input stream.
  *
  * @throws IOException
  *             if an I/O error has occurred
  */
 public override void close() //throws IOException
 {
     if (!this.closed)
     {
         inJ.close();
         this.closed = true;
     }
 }
 /*
  * (non-Javadoc)
  *
  * @see java.io.InputStream#close()
  */
 public override void close()
 {
     if (!closed)
     {
         closed = true;
         input.close();
     }
     currentEntry = null;
 }
Beispiel #6
0
 private static void closeQuietly(java.io.InputStream inJ)
 {
     if (inJ == null)
     {
         return;
     }
     try {
         inJ.close();
     } catch (java.io.IOException e) {
     }
 }
Beispiel #7
0
 public override void close() //throws IOException
 {
     java.io.InputStream inShadow = this.inJ;
     if (inShadow != null)
     {
         try {
             if (inShadow != java.lang.SystemJ.inJ)
             {
                 inShadow.close();
             }
         } finally {
             this.data = null;
             this.inJ  = null;
         }
     }
 }
Beispiel #8
0
        /**
         * Close the TarBuffer. If this is an output buffer, also flush the
         * current block before closing.
         * @throws IOException on error
         */
        public void close() //throws IOException
        {
            if (outStream != null)
            {
                flushBlock();

                if (outStream != java.lang.SystemJ.outJ &&
                    outStream != java.lang.SystemJ.err)
                {
                    outStream.close();

                    outStream = null;
                }
            }
            else if (inStream != null)
            {
                if (inStream != java.lang.SystemJ.inJ)
                {
                    inStream.close();

                    inStream = null;
                }
            }
        }
Beispiel #9
0
            /*
             * Parse the provider-configuration file as specified
             * @see <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Provider Configuration File">JAR File Specification</a>
             */
            private java.util.Set <String> parse(java.net.URL u)
            {
                java.io.InputStream    input  = null;
                java.io.BufferedReader reader = null;
                java.util.Set <String> names  = new java.util.HashSet <String>();
                try
                {
                    input  = u.openStream();
                    reader = new java.io.BufferedReader(new java.io.InputStreamReader(input, "utf-8")); //$NON-NLS-1$

                    String line;
                    while ((line = reader.readLine()) != null)
                    {
                        // The comment character is '#' (0x23)
                        // on each line all characters following the first comment character are ignored
                        int sharpIndex = line.indexOf('#');
                        if (sharpIndex >= 0)
                        {
                            line = line.substring(0, sharpIndex);
                        }

                        // Whitespaces are ignored
                        line = line.trim();

                        if (line.length() > 0)
                        {
                            // a java class name, check if identifier correct
                            char[] namechars = line.toCharArray();
                            for (int i = 0; i < namechars.Length; i++)
                            {
                                if (!(java.lang.Character.isJavaIdentifierPart(namechars[i]) || namechars[i] == '.'))
                                {
                                    throw new ServiceConfigurationError(Messages.getString("imageio.99", line));
                                }
                            }
                            names.add(line);
                        }
                    }
                }
                catch (java.io.IOException e)
                {
                    throw new ServiceConfigurationError(e.toString());
                }
                finally
                {
                    try
                    {
                        if (reader != null)
                        {
                            reader.close();
                        }
                        if (input != null)
                        {
                            input.close();
                        }
                    }
                    catch (java.io.IOException e)
                    {
                        throw new ServiceConfigurationError(e.toString());
                    }
                }

                return(names);
            }
Beispiel #10
0
        /**
         * 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();
        }