Пример #1
0
 public virtual bool NeedsDictionary()
 {
     lock (this)
     {
         return(inflater.NeedsDictionary());
     }
 }
        /// <summary>
        /// Decompresses data into the byte array
        /// </summary>
        /// <param name ="b">
        /// the array to read and decompress data into
        /// </param>
        /// <param name ="off">
        /// the offset indicating where the data should be placed
        /// </param>
        /// <param name ="len">
        /// the number of bytes to decompress
        /// </param>
        public override int Read(byte[] b, int off, int len)
        {
            for (;;)
            {
                int count;
                try {
                    count = inf.Inflate(b, off, len);
                } catch (Exception dfe) {
                    throw new Exception(dfe.ToString());
                }

                if (count > 0)
                {
                    return(count);
                }

                if (inf.NeedsDictionary())
                {
                    throw new Exception("Need a dictionary");
                }
                else if (inf.Finished())
                {
                    return(-1);
                }
                else if (inf.NeedsInput())
                {
                    Fill();
                }
                else
                {
                    throw new Exception("Don't know what to do");
                }
            }
        }
        /// <exception cref="System.IO.IOException"></exception>
        public DeflateInputStream(InputStream wrapped)
        {
            byte[] peeked = new byte[6];
            PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.Length);
            int headerLength             = pushback.Read(peeked);

            if (headerLength == -1)
            {
                throw new IOException("Unable to read the response");
            }
            byte[]   dummy = new byte[1];
            Inflater inf   = new Inflater();

            try
            {
                int n;
                while ((n = inf.Inflate(dummy)) == 0)
                {
                    if (inf.IsFinished)
                    {
                        throw new IOException("Unable to read the response");
                    }
                    if (inf.NeedsDictionary())
                    {
                        break;
                    }
                    if (inf.IsNeedingInput)
                    {
                        inf.SetInput(peeked);
                    }
                }
                if (n == -1)
                {
                    throw new IOException("Unable to read the response");
                }
                pushback.Unread(peeked, 0, headerLength);
                sourceStream = new DeflateInputStream.DeflateStream(pushback, new Inflater());
            }
            catch (SharpZipBaseException)
            {
                pushback.Unread(peeked, 0, headerLength);
                sourceStream = new DeflateInputStream.DeflateStream(pushback, new Inflater(true));
            }
            finally
            {
                inf.Finish();
            }
        }
Пример #4
0
        /// <summary>
        /// Writes an array of bytes to the uncompressed output stream.
        /// </summary>
        /// <param name="b"> buffer containing compressed data to decompress and write to
        /// the output stream </param>
        /// <param name="off"> starting offset of the compressed data within {@code b} </param>
        /// <param name="len"> number of bytes to decompress from {@code b} </param>
        /// <exception cref="IndexOutOfBoundsException"> if {@code off < 0}, or if
        /// {@code len < 0}, or if {@code len > b.length - off} </exception>
        /// <exception cref="IOException"> if an I/O error occurs or this stream is already
        /// closed </exception>
        /// <exception cref="NullPointerException"> if {@code b} is null </exception>
        /// <exception cref="ZipException"> if a compression (ZIP) format error occurs </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void write(byte[] b, int off, int len) throws java.io.IOException
        public override void Write(sbyte[] b, int off, int len)
        {
            // Sanity checks
            EnsureOpen();
            if (b == null)
            {
                throw new NullPointerException("Null buffer for read");
            }
            else if (off < 0 || len < 0 || len > b.Length - off)
            {
                throw new IndexOutOfBoundsException();
            }
            else if (len == 0)
            {
                return;
            }

            // Write uncompressed data to the output stream
            try
            {
                for (;;)
                {
                    int n;

                    // Fill the decompressor buffer with output data
                    if (Inf.NeedsInput())
                    {
                        int part;

                        if (len < 1)
                        {
                            break;
                        }

                        part = (len < 512 ? len : 512);
                        Inf.SetInput(b, off, part);
                        off += part;
                        len -= part;
                    }

                    // Decompress and write blocks of output data
                    do
                    {
                        n = Inf.Inflate(Buf, 0, Buf.Length);
                        if (n > 0)
                        {
                            @out.Write(Buf, 0, n);
                        }
                    } while (n > 0);

                    // Check the decompressor
                    if (Inf.Finished())
                    {
                        break;
                    }
                    if (Inf.NeedsDictionary())
                    {
                        throw new ZipException("ZLIB dictionary missing");
                    }
                }
            }
            catch (DataFormatException ex)
            {
                // Improperly formatted compressed (ZIP) data
                String msg = ex.Message;
                if (msg == null)
                {
                    msg = "Invalid ZLIB data format";
                }
                throw new ZipException(msg);
            }
        }