Esempio n. 1
0
        public virtual void TestGzipCompatibility()
        {
            Random r    = new Random();
            long   seed = r.NextLong();

            r.SetSeed(seed);
            Log.Info("seed: " + seed);
            DataOutputBuffer dflbuf = new DataOutputBuffer();
            GZIPOutputStream gzout  = new GZIPOutputStream(dflbuf);

            byte[] b = new byte[r.Next(128 * 1024 + 1)];
            r.NextBytes(b);
            gzout.Write(b);
            gzout.Close();
            DataInputBuffer gzbuf = new DataInputBuffer();

            gzbuf.Reset(dflbuf.GetData(), dflbuf.GetLength());
            Configuration conf = new Configuration();

            conf.SetBoolean(CommonConfigurationKeys.IoNativeLibAvailableKey, false);
            CompressionCodec codec = ReflectionUtils.NewInstance <GzipCodec>(conf);
            Decompressor     decom = codec.CreateDecompressor();

            NUnit.Framework.Assert.IsNotNull(decom);
            Assert.Equal(typeof(BuiltInGzipDecompressor), decom.GetType());
            InputStream gzin = codec.CreateInputStream(gzbuf, decom);

            dflbuf.Reset();
            IOUtils.CopyBytes(gzin, dflbuf, 4096);
            byte[] dflchk = Arrays.CopyOf(dflbuf.GetData(), dflbuf.GetLength());
            Assert.AssertArrayEquals(b, dflchk);
        }
Esempio n. 2
0
 /// <exception cref="System.IO.IOException"/>
 public static int WriteCompressedByteArray(BinaryWriter writer, byte[] bytes)
 {
     if (bytes != null)
     {
         ByteArrayOutputStream bos   = new ByteArrayOutputStream();
         GZIPOutputStream      gzout = new GZIPOutputStream(bos);
         try
         {
             gzout.Write(bytes, 0, bytes.Length);
             gzout.Close();
             gzout = null;
         }
         finally
         {
             IOUtils.CloseStream(gzout);
         }
         byte[] buffer = bos.ToByteArray();
         int    len    = buffer.Length;
         @out.WriteInt(len);
         @out.Write(buffer, 0, len);
         /* debug only! Once we have confidence, can lose this. */
         return((bytes.Length != 0) ? (100 * buffer.Length) / bytes.Length : 0);
     }
     else
     {
         @out.WriteInt(-1);
         return(-1);
     }
 }
        /// <exception cref="System.IO.IOException"></exception>
        public override void WriteTo(OutputStream outstream)
        {
            Args.NotNull(outstream, "Output stream");
            GZIPOutputStream gzip = new GZIPOutputStream(outstream);

            try
            {
                wrappedEntity.WriteTo(gzip);
            }
            finally
            {
                gzip.Close();
            }
        }
Esempio n. 4
0
        /// <exception cref="System.IO.IOException"/>
        internal virtual void GzipConcatTest(Configuration conf, Type decomClass)
        {
            Random r    = new Random();
            long   seed = r.NextLong();

            r.SetSeed(seed);
            Log.Info(decomClass + " seed: " + seed);
            int Concat = r.Next(4) + 3;
            int Buflen = 128 * 1024;
            DataOutputBuffer dflbuf = new DataOutputBuffer();
            DataOutputBuffer chkbuf = new DataOutputBuffer();

            byte[] b = new byte[Buflen];
            for (int i = 0; i < Concat; ++i)
            {
                GZIPOutputStream gzout = new GZIPOutputStream(dflbuf);
                r.NextBytes(b);
                int len = r.Next(Buflen);
                int off = r.Next(Buflen - len);
                chkbuf.Write(b, off, len);
                gzout.Write(b, off, len);
                gzout.Close();
            }
            byte[]           chk   = Arrays.CopyOf(chkbuf.GetData(), chkbuf.GetLength());
            CompressionCodec codec = ReflectionUtils.NewInstance <GzipCodec>(conf);
            Decompressor     decom = codec.CreateDecompressor();

            NUnit.Framework.Assert.IsNotNull(decom);
            Assert.Equal(decomClass, decom.GetType());
            DataInputBuffer gzbuf = new DataInputBuffer();

            gzbuf.Reset(dflbuf.GetData(), dflbuf.GetLength());
            InputStream gzin = codec.CreateInputStream(gzbuf, decom);

            dflbuf.Reset();
            IOUtils.CopyBytes(gzin, dflbuf, 4096);
            byte[] dflchk = Arrays.CopyOf(dflbuf.GetData(), dflbuf.GetLength());
            Assert.AssertArrayEquals(chk, dflchk);
        }
Esempio n. 5
0
            /// <exception cref="System.IO.IOException"></exception>
            internal virtual void SendRequest()
            {
                // Try to compress the content, but only if that is smaller.
                TemporaryBuffer buf = new TemporaryBuffer.Heap(this._enclosing.http.postBuffer);

                try
                {
                    GZIPOutputStream gzip = new GZIPOutputStream(buf);
                    [email protected](gzip, null);
                    gzip.Close();
                    if ([email protected]() < buf.Length())
                    {
                        buf = this.@out;
                    }
                }
                catch (IOException)
                {
                    // Most likely caused by overflowing the buffer, meaning
                    // its larger if it were compressed. Don't compress.
                    buf = this.@out;
                }
                this.OpenStream();
                if (buf != this.@out)
                {
                    this.conn.SetRequestProperty(HttpSupport.HDR_CONTENT_ENCODING, HttpSupport.ENCODING_GZIP
                                                 );
                }
                this.conn.SetFixedLengthStreamingMode((int)buf.Length());
                OutputStream httpOut = this.conn.GetOutputStream();

                try
                {
                    buf.WriteTo(httpOut, null);
                }
                finally
                {
                    httpOut.Close();
                }
            }
Esempio n. 6
0
 public virtual byte[] ConvertToBytes(IList <Tree> input)
 {
     try
     {
         ByteArrayOutputStream bos         = new ByteArrayOutputStream();
         GZIPOutputStream      gos         = new GZIPOutputStream(bos);
         ObjectOutputStream    oos         = new ObjectOutputStream(gos);
         IList <Tree>          transformed = CollectionUtils.TransformAsList(input, treeBasicCategories);
         IList <Tree>          filtered    = CollectionUtils.FilterAsList(transformed, treeFilter);
         oos.WriteObject(filtered.Count);
         foreach (Tree tree in filtered)
         {
             oos.WriteObject(tree.ToString());
         }
         oos.Close();
         gos.Close();
         bos.Close();
         return(bos.ToByteArray());
     }
     catch (IOException e)
     {
         throw new RuntimeIOException(e);
     }
 }
Esempio n. 7
0
            /// <exception cref="System.IO.IOException"></exception>
            internal virtual void Execute()
            {
                [email protected]();
                if (this.conn == null)
                {
                    // Output hasn't started yet, because everything fit into
                    // our request buffer. Send with a Content-Length header.
                    //
                    if ([email protected]() == 0)
                    {
                        throw new TransportException(this._enclosing.uri, JGitText.Get().startingReadStageWithoutWrittenRequestDataPendingIsNotSupported
                                                     );
                    }
                    // Try to compress the content, but only if that is smaller.
                    TemporaryBuffer buf = new TemporaryBuffer.Heap(this._enclosing.http.postBuffer);
                    try
                    {
                        GZIPOutputStream gzip = new GZIPOutputStream(buf);
                        [email protected](gzip, null);
                        gzip.Close();
                        if ([email protected]() < buf.Length())
                        {
                            buf = this.@out;
                        }
                    }
                    catch (IOException)
                    {
                        // Most likely caused by overflowing the buffer, meaning
                        // its larger if it were compressed. Don't compress.
                        buf = this.@out;
                    }
                    this.OpenStream();
                    if (buf != this.@out)
                    {
                        this.conn.SetRequestProperty(HttpSupport.HDR_CONTENT_ENCODING, HttpSupport.ENCODING_GZIP
                                                     );
                    }
                    this.conn.SetFixedLengthStreamingMode((int)buf.Length());
                    OutputStream httpOut = this.conn.GetOutputStream();
                    try
                    {
                        buf.WriteTo(httpOut, null);
                    }
                    finally
                    {
                        httpOut.Close();
                    }
                }
                [email protected]();
                int status = HttpSupport.Response(this.conn);

                if (status != HttpURLConnection.HTTP_OK)
                {
                    throw new TransportException(this._enclosing.uri, status + " " + this.conn.GetResponseMessage
                                                     ());
                }
                //$NON-NLS-1$
                string contentType = this.conn.GetContentType();

                if (!this.responseType.Equals(contentType))
                {
                    this.conn.GetInputStream().Close();
                    throw this._enclosing.WrongContentType(this.responseType, contentType);
                }
                [email protected](this._enclosing.OpenInputStream(this.conn));
                [email protected](this.execute);
                this.conn = null;
            }