Write() public method

Write data to the stream.

If you wish to use the ZlibStream to compress data while writing, you can create a ZlibStream with CompressionMode.Compress, and a writable output stream. Then call Write() on that ZlibStream, providing uncompressed data as input. The data sent to the output stream will be the compressed form of the data written. If you wish to use the ZlibStream to decompress data while writing, you can create a ZlibStream with CompressionMode.Decompress, and a writable output stream. Then call Write() on that stream, providing previously compressed data. The data sent to the output stream will be the decompressed form of the data written.

A ZlibStream can be used for Read() or Write(), but not both.

public Write ( byte buffer, int offset, int count ) : void
buffer byte The buffer holding data to write to the stream.
offset int the offset within that data array to find the first byte to write.
count int the number of bytes to write.
return void
        /// <summary>
        /// 지정된 데이타를 압축한다.
        /// </summary>
        /// <param name="input">압축할 Data</param>
        /// <returns>압축된 Data</returns>
        public override byte[] Compress(byte[] input) {
            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressStartMsg);

            // check input data
            if(input.IsZeroLength()) {
                if(IsDebugEnabled)
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);

                return CompressorTool.EmptyBytes;
            }

            byte[] output;

            using(var outStream = new MemoryStream(input.Length)) {
                using(var zlib = new ZlibStream(outStream, CompressionMode.Compress)) {
                    zlib.Write(input, 0, input.Length);
                }
                output = outStream.ToArray();
            }

            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);

            return output;
        }
Beispiel #2
0
 public static byte[] Compress(byte[] input)
 {
     using (MemoryStream ms = new MemoryStream()) {
         using (ZlibStream zls = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestSpeed)) {
             zls.Write(input, 0, input.Length);
         }
         return ms.ToArray();
     }
 }
Beispiel #3
0
        public static byte[] ZlibCompress(byte[] data)
        {
            MemoryStream res = new MemoryStream();
            var          fw  = new Ionic.Zlib.ZlibStream(res, Ionic.Zlib.CompressionMode.Compress);

            fw.Write(data, 0, data.Length);
            fw.Close();
            res.Flush();
            return(res.ToArray());
        }
Beispiel #4
0
 /// <summary>
 /// Compress to CompressedData
 /// </summary>
 public static MemoryStream Compress(byte[] buffer)
 {
     var output = new MemoryStream();
     using (ZlibStream zout = new ZlibStream(output, CompressionMode.Compress))
     {
         zout.Write(buffer, 0, buffer.Length);
         zout.Flush();
     }
     return output;
 }
Beispiel #5
0
 internal static byte[] Compress(byte[] buffer)
 {
     using (var output = new MemoryStream())
     {
         using (Stream compressor = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression))
         {
             compressor.Write(buffer, 0, buffer.Length);
         }
         return output.ToArray();
     }
 }
Beispiel #6
0
 public static byte[] ZibCompress(byte[] input)
 {
     using (var ms = new System.IO.MemoryStream())
     {
         using (var zs = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, true))
         {
             zs.Write(input, 0, input.Length);
         }
         return ms.ToArray();
     }
 }
 public static UnmanagedMemory ZibCompress(byte[] input)
 {
     using (var ms = new TraderMemoryStream(500 * 1024))
     {
         using (var zs = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, true))
         {
             zs.Write(input, 0, input.Length);
         }
         return ms.Buffer;
     }
 }
        public static byte[] CompressZlib(byte[] bytes)
        {
            using (var ms = new MemoryStream())
            {
                using (var zip = new ZlibStream(ms, CompressionMode.Compress, true))
                {
                    zip.Write(bytes, 0, bytes.Length);
                }

                return ms.ToArray();
            }
        }
        static byte[] ZlibDecompress(byte[] input, int length)
        {
            using (var ms = new System.IO.MemoryStream())
            {
                using (var compressor = new Ionic.Zlib.ZlibStream(ms, CompressionMode.Decompress, CompressionLevel.BestCompression))
                {
                    compressor.Write(input, 0, length);
                }

                return ms.ToArray();
            }
        }
    private byte[] Compress(byte[] a)
    {
        using (var ms = new System.IO.MemoryStream())
        {
            using (var compressor =
                       new Ionic.Zlib.ZlibStream(ms,
                                                 CompressionMode.Compress,
                                                 CompressionLevel.Level9))
            {
                compressor.Write(a, 0, a.Length);
            }

            return(ms.ToArray());
        }
    }
Beispiel #11
0
        public static void Decomp(byte[] input, Stream outputStream)
        {
            using (var zipStream = new ZlibStream(outputStream, CompressionMode.Decompress))
            {
                using (var inputStream = new MemoryStream(input))
                {
                    byte[] buffer = input.Length > 4096 ? new byte[4096] : new byte[input.Length];

                    int size;

                    while ((size = inputStream.Read(buffer, 0, buffer.Length)) != 0)
                        zipStream.Write(buffer, 0, size);
                }
            }
        }
 public static void CompressFile(string inFile, string strOutFile)
 {
     using (System.IO.Stream input = System.IO.File.OpenRead(inFile))
     {
         using (var raw = System.IO.File.Create(strOutFile))
         {
             using (Stream compressor = new ZlibStream(raw, CompressionMode.Compress))
             {
                 byte[] buffer = new byte[WORKING_BUFFER_SIZE];
                 int    n;
                 while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
                 {
                     compressor.Write(buffer, 0, n);
                 }
             }
         }
     }
 }
Beispiel #13
0
        public static byte[] Compress(this byte[] uncompressed, int level)
        {
            if (level == 0)
                return uncompressed;

            byte[] result;
            using (var ms = new MemoryStream())
            {
                using (var compressor =
                        new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.Level0+level))
                    compressor.Write(uncompressed, 0, uncompressed.Length);
                result =  ms.ToArray();
            }

            // If compression did not improve the situation, then use
            // uncompressed bytes.
            if (result.Length >= uncompressed.Length)
                return uncompressed;

            return result;
        }
        public void SubmitBulkOperationsAsync(string callingUrl, Guid sessionId, OPS operations, object userState)
        {
            MemoryStream operationsXml = null;
            MemoryStream compressedOperationsStream = null;
            ZlibStream zipStream = null;

            try
            {
                operationsXml = new MemoryStream();

                DataContractSerializer serializer = new DataContractSerializer(typeof(OPS));

                serializer.WriteObject(operationsXml, operations);

                byte[] operationsXmlBytes = operationsXml.ToArray();
                compressedOperationsStream = new MemoryStream();

                using (zipStream = new ZlibStream(compressedOperationsStream, CompressionMode.Compress))
                {
                    zipStream.Write(operationsXmlBytes, 0, operationsXmlBytes.Length);
                }

                byte[] compressedOperationsXmlBytes = compressedOperationsStream.ToArray();

                string compressedOperations = Convert.ToBase64String(compressedOperationsXmlBytes);

                BOCAsync(callingUrl, sessionId, compressedOperations, userState);
            }
            finally
            {
                if (operationsXml != null)
                {
                    operationsXml.Dispose();
                    operationsXml = null;
                }
            }
        }
        public static OSD ZCompressOSD(OSD inOsd, bool useHeader)
        {
            OSD osd = null;

            byte[] data = OSDParser.SerializeLLSDBinary(inOsd, useHeader);

            using (MemoryStream msSinkCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkCompressed, 
                    Ionic.Zlib.CompressionMode.Compress, CompressionLevel.BestCompression, true))
                {
                    zOut.Write(data, 0, data.Length);
                }

                msSinkCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSD.FromBinary(msSinkCompressed.ToArray());
            }

            return osd;
        }
 public void AddSlot(slot cv_slot)
 {
     AddShort(cv_slot.s_short);
     if(slot.IsGziped(cv_slot.s_short))
     {
         ZlibStream Compress = new ZlibStream(stream, CompressionMode.Compress);
         Compress.Write(cv_slot.GZipData, 0, cv_slot.GZipData.Length);
     }
 }
Beispiel #17
0
        /// <summary>
        /// Compresses the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="compressionLevel">The compression level.</param>
        /// <returns></returns>
        private static byte[] Compress(byte[] data, CompressionLevel compressionLevel = CompressionLevel.Default)
        {
            // While the CgBI PNG image data is compressed with Deflate, a "real" PNG must use Zlib.
            using (var output = new MemoryStream())
            {
                using (var deflateStream = new ZlibStream(output, CompressionMode.Compress, compressionLevel, true))
                {
                    deflateStream.Write(data, 0, data.Length);
                }

                return output.ToArray();
            }
        }
Beispiel #18
0
        private void writeToStreamOld(Stream s, bool le, bool readOnly)
        {
            bool compress = true;
            bool base64 = true;

            using (var tw = new StreamWriter(s, System.Text.Encoding.ASCII))
            {
                tw.NewLine = "\n";
                tw.WriteLine("Type: Floppy");

                if (readOnly)
                    tw.WriteLine("Access: Read-Only");
                else
                    tw.WriteLine("Access: Read-Write");

                if (le)
                    tw.WriteLine("Byte-Order: Little-Endian");
                else
                    tw.WriteLine("Byte-Order: Big-Endian");

                int lb = 0, hb = 1;
                if (!le)
                {
                    lb = 1;
                    hb = 0;
                }

                byte[] bytes, cbytes;

                bytes = new byte[1440 * 1024];
                for (int i = 0; i < _words.Length; ++i)
                {
                    var j = 2 * i;
                    bytes[j + lb] = (byte)_words[i];
                    bytes[j + hb] = (byte)(_words[i] >> 8);
                }

                if (compress)
                {
                    tw.WriteLine("Compression: Zlib");
                    using (var ms = new MemoryStream())
                    {
                        using (var cmpr = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression, leaveOpen: true))
                        {
                            cmpr.Write(bytes, 0, bytes.Length);
                            cmpr.Flush();
                        }
                        cbytes = ms.ToArray();
                    }
                }
                else
                {
                    tw.WriteLine("Compression: None");
                    cbytes = bytes;
                }

                if (base64)
                {
                    var encoded = Convert.ToBase64String(cbytes, Base64FormattingOptions.InsertLineBreaks);

                    tw.WriteLine("Encoding: Base64");
                    tw.WriteLine("Content-Length: {0}", encoded.Length);
                    tw.WriteLine();
                    tw.WriteLine(encoded);
                }
                else
                {
                    tw.WriteLine("Encoding: None");
                    tw.WriteLine("Content-Length: {0}", cbytes.Length);
                    tw.WriteLine();
                    tw.Flush();
                    tw.BaseStream.Write(bytes, 0, cbytes.Length);
                }
            }
        }
Beispiel #19
0
 /// <summary>
 /// Compresses a byte array using the specified compression type.
 /// </summary>
 /// <param name="bytes">The byte array to be compressed.</param>
 /// <param name="level">Amount of compression to use.</param>
 /// <param name="type">Type of compression to use.</param>
 /// <returns>Compressed byte array.</returns>
 public static byte[] Compress(this byte[] bytes, CompressionLevel level, CompressionType type)
 {
     using (MemoryStream memory = new MemoryStream())
     {
         switch (type) {
             case CompressionType.Zlib:
                 using (ZlibStream stream = new ZlibStream(memory, CompressionMode.Compress, level, true))
                     stream.Write(bytes, 0, bytes.Length);
                 break;
             case CompressionType.GZip:
                 using (GZipStream stream = new GZipStream(memory, CompressionMode.Compress, level, true))
                     stream.Write(bytes, 0, bytes.Length);
                 break;
             default:
                 throw new ArgumentException("Unknown compression type.");
         }
         memory.Position = 0;
         bytes = new byte[memory.Length];
         memory.Read(bytes, 0, (int)memory.Length);
     }
     return bytes;
 }
Beispiel #20
0
 /// <summary>
 /// Compress the contents of the provided array
 /// </summary>
 /// <param name="data">An uncompressed byte array</param>
 /// <returns></returns>
 public static byte[] Compress(byte[] data)
 {
     MemoryStream out1 = new MemoryStream();
     ZlibStream deflaterOutputStream = new ZlibStream(out1, CompressionMode.Compress);
     try
     {
         //for (int i = 0; i < data.Length; i++)
         //deflaterOutputStream.WriteByte(data[i]);
         deflaterOutputStream.Write(data, 0, data.Length);   //Tony Qu changed the code
         return out1.ToArray();
     }
     catch (IOException e)
     {
         throw new RecordFormatException(e.ToString());
     }
     finally
     {
         out1.Close();
         if (deflaterOutputStream != null)
         {
             deflaterOutputStream.Close();
         }
     }
 }
        private byte[] Compress(byte[] revlogEntryData, int offset, int count)
        {
            using(var memoryStream = new MemoryStream())
            {
                using(var zlibStream = new ZlibStream(memoryStream, CompressionMode.Compress))
                {
                    zlibStream.Write(revlogEntryData, offset, count);
                    zlibStream.Flush();
                } // using

                return memoryStream.ToArray();
            } // using
        }
        public static OSD ZDecompressBytesToOsd(byte[] input)
        {
            OSD osd = null;

            using (MemoryStream msSinkUnCompressed = new MemoryStream())
            {
                using (Ionic.Zlib.ZlibStream zOut = new Ionic.Zlib.ZlibStream(msSinkUnCompressed, CompressionMode.Decompress, true))
                {
                    zOut.Write(input, 0, input.Length);
                }

                msSinkUnCompressed.Seek(0L, SeekOrigin.Begin);
                osd = OSDParser.DeserializeLLSDBinary(msSinkUnCompressed.ToArray());
            }

            return osd;
        }
 private static byte[] ZibCompress(byte[] data)
 {
     //using (var cms = new MemoryStream(data))
     using (var msSinkCompressed = new System.IO.MemoryStream())
     {
         using (var zOut = new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true))
         {
             //CopyStream(cms, zOut);
             zOut.Write(data, 0, data.Length);
         }
         return msSinkCompressed.ToArray();
     }
 }
Beispiel #24
0
        public void CompressResponse()
        {
            if (Nodes == null || Nodes.Count == 0)
            {
                return;
            }

            MemoryStream nodeCollectionXml = null;
            MemoryStream compressedNodeCollectionStream = null;
            ZlibStream zipStream = null;

            try
            {
                nodeCollectionXml = new MemoryStream();

                DataContractSerializer serializer = new DataContractSerializer(typeof(CompressedResponseTuple));

                CompressedResponseTuple responseTuple = new CompressedResponseTuple();
                responseTuple.Nodes = Nodes;
                responseTuple.Relationships = Relationships;

                serializer.WriteObject(nodeCollectionXml, responseTuple);

                byte[] metadataCollectionBytes = nodeCollectionXml.ToArray();
                compressedNodeCollectionStream = new MemoryStream();

                using (zipStream = new ZlibStream(compressedNodeCollectionStream, CompressionMode.Compress))
                {
                    zipStream.Write(metadataCollectionBytes, 0, metadataCollectionBytes.Length);
                }

                byte[] compressedMetadataCollectionBytes = compressedNodeCollectionStream.ToArray();

                CompressedNodeData = Convert.ToBase64String(compressedMetadataCollectionBytes);
                Nodes.Clear();
                Relationships.Clear();
            }
            finally
            {
                if (nodeCollectionXml != null)
                {
                    nodeCollectionXml.Dispose();
                    nodeCollectionXml = null;
                }
            }
        }
        public ChunkPacket MakeChunkArray(int _x, int _y, bool Load)
        {
            int Width = Config.GetSettingInt("Size_X");
            int Height = Config.GetSettingInt("Size_Z");
            int Depth = Config.GetSettingInt("Size_Y");

            ChunkPacket _cPacket = new ChunkPacket();
            _cPacket.X = _x * 16;
            _cPacket.Z = _y * 16;
            _cPacket.GroundUpC = true;
            _cPacket.PrimaryBitMap = 0;
            _cPacket.AddBitMap = 0;
            _cPacket.ModAPI = 0;

            int index = Chunk.GetIndex(_x, _y);
            ushort mask = 1;
            byte[] f_blockData = new byte[0];
            byte[] f_metadata = new byte[0];
            byte[] f_blockLight = new byte[0];
            byte[] f_skyLight = new byte[0];

            Chunk c = World.chunks.ElementAt(Chunk.GetIndex(_x, _y));
            bool[] IsAir = c.IsAir;
            try
            {
                using (MemoryStream ms_zipped = new MemoryStream())
                {
                    using (Ionic.Zlib.ZlibStream compressor = new Ionic.Zlib.ZlibStream(ms_zipped, CompressionMode.Compress, (CompressionLevel)compression, false))
                    {
                        for (int i_h = 0; i_h < 16; i_h++)
                        {
                            if (!IsAir[i_h])
                                continue;

                            byte[] blockData = new byte[((Depth / 16) * Width * Height)];
                            for (int i = 0; i < ((Depth / 16) * Width * Height); i++)
                                blockData[i] = c.GetBlocktype(i * (i_h + 1));
                            compressor.Write(blockData, 0, blockData.Count());
                        }

                        for (int i_h = 0; i_h < 16; i_h++)
                        {
                            if (!IsAir[i_h])
                                continue;

                            byte[] metadata = new byte[((Depth / 16) * Width * Height) / 2];
                            for (int i = 0; i < ((Depth / 16) * Width * Height) / 2; i++)
                                metadata[i] = CombineByte(c.GetData((i * (i_h + 1)) + 1), c.GetData((i * (i_h + 1)) + 0));
                            compressor.Write(metadata, 0, metadata.Count());
                        }

                        for (int i_h = 0; i_h < 16; i_h++)
                        {
                            if (!IsAir[i_h])
                                continue;

                            byte[] blockLight = new byte[((Depth / 16) * Width * Height) / 2];
                            for (int i = 0; i < ((Depth / 16) * Width * Height) / 2; i++)
                                blockLight[i] = CombineByte(c.GetBlockLight((i * (i_h + 1)) + 1), c.GetBlockLight((i * (i_h + 1)) + 0));
                            compressor.Write(blockLight, 0, blockLight.Count());
                        }

                        for (int i_h = 0; i_h < 16; i_h++)
                        {
                            if (!IsAir[i_h])
                                continue;

                            byte[] skyLight = new byte[((Depth / 16) * Width * Height) / 2];
                            for (int i = 0; i < ((Depth / 16) * Width * Height) / 2; i++)
                                skyLight[i] = CombineByte(c.GetSkyLight((i * (i_h + 1)) + 1), c.GetSkyLight((i * (i_h + 1)) + 0));
                            compressor.Write(skyLight, 0, skyLight.Count());
                        }
                        for (int i_h = 0; i_h < 16; i_h++)
                        {
                            if(IsAir[i_h])
                                _cPacket.PrimaryBitMap |= mask;
                            mask <<= 1;
                        }
                        //compressor.Write(f_blockData, 0, f_blockData.Length);
                        //compressor.Write(f_metadata, 0, f_metadata.Length);
                        //compressor.Write(f_blockLight, 0, f_blockLight.Length);
                        //compressor.Write(f_skyLight, 0, f_skyLight.Length);
                    }
                    _cPacket.ChunkData = ms_zipped.ToArray();
                }
            }
            catch (Exception e)
            {
                throw;
            }
            _cPacket.Compressed_Size = _cPacket.ChunkData.Length;
            _cPacket.BuildPacket();
            return _cPacket;
        }
        private string CompressBulkOperationsResponse(List<BulkOperationResponse> bulkOperationsResponses)
        {
            MemoryStream bulkOperationsResponsesXml = null;
            MemoryStream compressedBulkOperationsResponsesStream = null;
            ZlibStream zipStream = null;

            try
            {
                bulkOperationsResponsesXml = new MemoryStream();

                DataContractSerializer serializer = new DataContractSerializer(typeof(List<BulkOperationResponse>));

                serializer.WriteObject(bulkOperationsResponsesXml, bulkOperationsResponses);

                byte[] metadataCollectionBytes = bulkOperationsResponsesXml.ToArray();
                compressedBulkOperationsResponsesStream = new MemoryStream();

                using (zipStream = new ZlibStream(compressedBulkOperationsResponsesStream, CompressionMode.Compress))
                {
                    zipStream.Write(metadataCollectionBytes, 0, metadataCollectionBytes.Length);
                }

                byte[] compressedMetadataCollectionBytes = compressedBulkOperationsResponsesStream.ToArray();

                string compressedResponses = Convert.ToBase64String(compressedMetadataCollectionBytes);

                return compressedResponses;
            }
            finally
            {
                if (bulkOperationsResponsesXml != null)
                {
                    bulkOperationsResponsesXml.Dispose();
                    bulkOperationsResponsesXml = null;
                }
            }
        }
Beispiel #27
0
 private byte[] Compress(byte[] bytes)
 {
     if (this._compression.ToLower() == "gzip")
     {
         using (MemoryStream memory = new MemoryStream())
         {
             using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
             {
                 gzip.Write(bytes, 0, bytes.Length);
             }
             return memory.ToArray();
         }
     }
     if (this._compression.ToLower() == "deflate")
     {
         using (MemoryStream memory = new MemoryStream())
         {
             using (ZlibStream deflate = new ZlibStream(memory, CompressionMode.Compress, true))
             {
                 deflate.Write(bytes, 0, bytes.Length);
             }
             return memory.ToArray();
         }
     }
     //no compression
     return bytes;
 }
Beispiel #28
0
        public void SetData(byte[] data, bool compress)
        {
            if (compress)
            {
                using (var ms = new System.IO.MemoryStream())
                {
                    using (ZlibStream compressor = new ZlibStream(ms, CompressionMode.Compress, CompressionLevel.Default))
                    {
                        compressor.Write(data, 0, data.Length);
                    }

                    this.Data = ms.ToArray();
                }
            }
            else
            {
                this.Data = data;
            }

            this.Size = (UInt32)data.Length;
            this.CompressedSize = (UInt32)this._data.Length;
        }