Пример #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void write(String fileName) throws java.io.IOException
        public virtual void write(string fileName)
        {
            System.IO.FileStream fileOutputStream     = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            GZIPOutputStream     gzipOutputStream     = new GZIPOutputStream(fileOutputStream);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(gzipOutputStream);
            StateOutputStream    stream = new StateOutputStream(bufferedOutputStream);

            if (log.InfoEnabled)
            {
                Console.WriteLine(string.Format("Writing state to file '{0}'", fileName));
            }

            try
            {
                write(stream);
            }
            finally
            {
                stream.close();
            }

            //if (log.DebugEnabled)
            {
                Console.WriteLine(string.Format("Done writing state to file '{0}'", fileName));
            }
        }
Пример #2
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);
        }
Пример #3
0
        /// <summary>
        ///     Compresses a GZIP file.
        /// </summary>
        /// <param name="bytes"> The uncompressed bytes. </param>
        /// <returns> The compressed bytes. </returns>
        /// <exception cref="IOException"> if an I/O error occurs. </exception>
        public static byte[] Gzip(byte[] bytes)
        {
            /* create the streams */
            var @is = new ByteArrayInputStream(bytes);

            try
            {
                var bout = new ByteArrayOutputStream();
                var os   = new GZIPOutputStream(bout);
                try
                {
                    /* copy data between the streams */
                    var buf = new byte[4096];
                    var len = 0;
                    while ((len = @is.read(buf, 0, buf.Length)) != -1)
                    {
                        os.write(buf, 0, len);
                    }
                }
                finally
                {
                    os.close();
                }

                /* return the compressed bytes */
                return(bout.toByteArray());
            }
            finally
            {
                @is.close();
            }
        }
Пример #4
0
        /// <summary>
        /// Constructor which takes a GISModel and a File and invokes the
        /// GISModelWriter appropriate for the suffix.
        /// </summary>
        /// <param name="model"> The GISModel which is to be persisted. </param>
        /// <param name="f"> The File in which the model is to be stored. </param>
        public SuffixSensitivePerceptronModelWriter(AbstractModel model, Jfile f) : base(model)
        {
            OutputStream output;
            string       filename = f.Name;

            // handle the zipped/not zipped distinction
            if (filename.EndsWith(".gz", StringComparison.Ordinal))
            {
                output   = new GZIPOutputStream(new FileOutputStream(f));
                filename = filename.Substring(0, filename.Length - 3);
            }
            else
            {
                output = new DataOutputStream(new FileOutputStream(f));
            }

            // handle the different formats
            if (filename.EndsWith(".bin", StringComparison.Ordinal))
            {
                suffixAppropriateWriter = new BinaryPerceptronModelWriter(model, new DataOutputStream(output));
            }
            else // default is ".txt"
            {
                suffixAppropriateWriter = new PlainTextPerceptronModelWriter(model,
                                                                             new BufferedWriter(new OutputStreamWriter(output)));
            }
        }
Пример #5
0
        public GenericModelWriter(AbstractModel model, Jfile file)
        {
            string filename = file.Name;
            OutputStream os;
            // handle the zipped/not zipped distinction
            if (filename.EndsWith(".gz", StringComparison.Ordinal))
            {
                os = new GZIPOutputStream(new FileOutputStream(file));
                filename = filename.Substring(0, filename.Length - 3);
            }
            else
            {
                os = new FileOutputStream(file);
            }

            // handle the different formats
            if (filename.EndsWith(".bin", StringComparison.Ordinal))
            {
                init(model, new DataOutputStream(os));
            }
            else // filename ends with ".txt"
            {
                init(model, new BufferedWriter(new OutputStreamWriter(os)));
            }
        }
Пример #6
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);
     }
 }
Пример #7
0
        public override void serializeBody(OutputStream stream)
        {
            ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
            GZIPOutputStream      stream3 = new GZIPOutputStream(stream2);

            this.method.serialize(stream3);
            stream3.flush();
            stream3.close();
            StreamingUtils.writeTLBytes(stream2.toByteArray(), stream);
        }
Пример #8
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private java.io.File compressWithGZip(String text) throws java.io.IOException
        private File CompressWithGZip(string text)
        {
            File file = Directory.file("compressed");

            using (GZIPOutputStream @out = new GZIPOutputStream(new FileStream(file, FileMode.Create, FileAccess.Write)))
            {
                @out.write(text.GetBytes());
            }
            return(file);
        }
 /// <summary>Similar to the unix gzip command, only it does not delete the file after compressing it.</summary>
 /// <param name="uncompressedFileName">The file to gzip</param>
 /// <param name="compressedFileName">The file name for the compressed file</param>
 /// <exception cref="System.IO.IOException"/>
 public static void GzipFile(File uncompressedFileName, File compressedFileName)
 {
     using (GZIPOutputStream @out = new GZIPOutputStream(new FileOutputStream(compressedFileName)))
     {
         using (FileInputStream @in = new FileInputStream(uncompressedFileName))
         {
             byte[] buf = new byte[1024];
             for (int len; (len = @in.Read(buf)) > 0;)
             {
                 @out.Write(buf, 0, len);
             }
         }
     }
 }
        /// <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();
            }
        }
Пример #11
0
 /// <summary>Write the word vectors to a file.</summary>
 /// <param name="file">The file to write to.</param>
 /// <exception cref="System.IO.IOException">Thrown if the file could not be written to.</exception>
 public virtual void Serialize(string file)
 {
     using (OutputStream output = new BufferedOutputStream(new FileOutputStream(new File(file))))
     {
         if (file.EndsWith(".gz"))
         {
             using (GZIPOutputStream gzip = new GZIPOutputStream(output))
             {
                 Serialize(gzip);
             }
         }
         else
         {
             Serialize(output);
         }
     }
 }
Пример #12
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();
                }
            }
Пример #13
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);
        }
Пример #14
0
        /// <summary>
        /// gzip Compress
        /// </summary>
        /// <param name="originalBytes">Data to be compressed</param>
        /// <returns>Compressed data</returns>
        private static sbyte[] Compress(sbyte[] originalBytes)
        {
            if (originalBytes == null || originalBytes.Length == 0)
            {
                return(null);
            }
            try
            {
                //using (var result = new MemoryStream())
                //{
                //    var lengthBytes = BitConverter.GetBytes(originalBytes.Length);
                //    result.Write(lengthBytes, 0, 4);

                //    using (var compressionStream = new GZipStream(result,
                //        CompressionMode.Compress))
                //    {
                //        compressionStream.Write((byte[])(Array)originalBytes, 0, originalBytes.Length);
                //        compressionStream.Flush();

                //    }
                //    return (sbyte[])(Array)result.ToArray();
                //}

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    GZIPOutputStream outputStream = new GZIPOutputStream(memoryStream);
                    outputStream.Write((byte[])(Array)originalBytes);
                    outputStream.Finish();
                    return((sbyte[])(Array)memoryStream.ToArray());
                }
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
                return(null);
            }
        }
Пример #15
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);
     }
 }
        /// <exception cref="System.IO.IOException"/>
        public override OutputStream Write(Annotation corpus, OutputStream os)
        {
            if (!(os is GZIPOutputStream))
            {
                if (compress)
                {
                    os = new GZIPOutputStream(os);
                }
            }
            PrintWriter pw = new PrintWriter(os);
            // save the coref graph in the new format
            IDictionary <int, CorefChain> chains = corpus.Get(typeof(CorefCoreAnnotations.CorefChainAnnotation));

            SaveCorefChains(chains, pw);
            // save the coref graph on one line
            // Note: this is the old format!
            IList <Pair <IntTuple, IntTuple> > corefGraph = corpus.Get(typeof(CorefCoreAnnotations.CorefGraphAnnotation));

            if (corefGraph != null)
            {
                bool first = true;
                foreach (Pair <IntTuple, IntTuple> arc in corefGraph)
                {
                    if (!first)
                    {
                        pw.Print(" ");
                    }
                    pw.Printf("%d %d %d %d", arc.first.Get(0), arc.first.Get(1), arc.second.Get(0), arc.second.Get(1));
                    first = false;
                }
            }
            pw.Println();
            // save sentences separated by an empty line
            IList <ICoreMap> sentences = corpus.Get(typeof(CoreAnnotations.SentencesAnnotation));

            foreach (ICoreMap sent in sentences)
            {
                // save the parse tree first, on a single line
                Tree tree = sent.Get(typeof(TreeCoreAnnotations.TreeAnnotation));
                if (tree != null)
                {
                    string treeString = tree.ToString();
                    // no \n allowed in the parse tree string (might happen due to tokenization of HTML/XML/RDF tags)
                    treeString = treeString.ReplaceAll("\n", " ");
                    pw.Println(treeString);
                }
                else
                {
                    pw.Println();
                }
                SemanticGraph collapsedDeps = sent.Get(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation));
                SaveDependencyGraph(collapsedDeps, pw);
                SemanticGraph uncollapsedDeps = sent.Get(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation));
                SaveDependencyGraph(uncollapsedDeps, pw);
                SemanticGraph ccDeps = sent.Get(typeof(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation));
                SaveDependencyGraph(ccDeps, pw);
                // save all sentence tokens
                IList <CoreLabel> tokens = sent.Get(typeof(CoreAnnotations.TokensAnnotation));
                if (tokens != null)
                {
                    foreach (CoreLabel token in tokens)
                    {
                        SaveToken(token, haveExplicitAntecedent, pw);
                        pw.Println();
                    }
                }
                // add an empty line after every sentence
                pw.Println();
            }
            pw.Flush();
            return(os);
        }
Пример #17
0
        private sbyte[] getCompressedPrxFile(string dirName, string fileName)
        {
            string proxyFileName;

            if (string.ReferenceEquals(dirName, null) || dirName.Length == 0)
            {
                proxyFileName = fileName;
            }
            else
            {
                proxyFileName = dirName + "/" + fileName;
            }

            IVirtualFile vFileUncompressed = base.ioOpen(proxyFileName, PSP_O_RDONLY, 0);

            if (vFileUncompressed == null)
            {
                return(null);
            }
            sbyte[] bufferUncompressed = Utilities.readCompleteFile(vFileUncompressed);
            vFileUncompressed.ioClose();
            if (bufferUncompressed == null)
            {
                return(null);
            }

            int headerMagic = Utilities.readUnaligned32(bufferUncompressed, 0);

            if (headerMagic != Elf32Header.ELF_MAGIC)
            {
                return(bufferUncompressed);
            }

            int lengthUncompressed = bufferUncompressed.Length;

            System.IO.MemoryStream osCompressed = new System.IO.MemoryStream(PSP_HEADER_SIZE + 9 + lengthUncompressed);
            try
            {
                writePspHeader(osCompressed, bufferUncompressed, fileName);
                // loadcore.prx and sysmem.prx need to be compressed using KL4E.
                // KL4E supports a version where the data is not compressed.
                // Use this simple version as we have no real KL4E compressor.
                if (isKl4eCompression(fileName))
                {
                    writeString(osCompressed, "KL4E", 4);
                    write8(osCompressed, 0x80);                     // Flag indicating that the rest of the data is uncompressed
                    write32(osCompressed, endianSwap32(lengthUncompressed));
                    writeBytes(osCompressed, bufferUncompressed, lengthUncompressed);
                }
                else
                {
                    GZIPOutputStream os = new GZIPOutputStream(osCompressed);
                    os.write(bufferUncompressed, 0, lengthUncompressed);
                    os.close();
                }
            }
            catch (IOException)
            {
            }

            sbyte[] bytes = osCompressed.toByteArray();
            fixPspSizeInHeader(bytes);

            return(bytes);
        }
Пример #18
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;
            }