public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int pos = 0; while (true) { int numRead = inStream.Read(buf2, pos, 4096); if (numRead <= 0) { break; } pos += numRead; } for (int i = 0; i < buf.Length; ++i) { Assertion.AssertEquals(buf2[i], buf[i]); } }
public void GetDataSet() { SoapContext sc = HttpSoapContext.ResponseContext; if (null == sc) { throw new ApplicationException("Only SOAP requests allowed"); } SqlConnection conn = new SqlConnection(@"data source=(local)\NetSDK;" + "initial catalog=Northwind;integrated security=SSPI"); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM customers", conn); DataSet ds = new DataSet("CustomerDS"); da.Fill(ds, "Customers"); // dispose of all objects that are no longer necessary da.Dispose(); conn.Dispose(); MemoryStream memoryStream = new MemoryStream(1024); GZipOutputStream gzipStream = new GZipOutputStream(memoryStream); ds.WriteXml(gzipStream); gzipStream.Finish(); memoryStream.Seek(0, SeekOrigin.Begin); DimeAttachment dimeAttachment = new DimeAttachment("application/x-gzip", TypeFormatEnum.MediaType, memoryStream); sc.Attachments.Add(dimeAttachment); }
/// <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 compressedStream = new MemoryStream(input.Length)) { using(var gzs = new GZipOutputStream(compressedStream)) { gzs.SetLevel(ZipLevel); gzs.Write(input, 0, input.Length); gzs.Finish(); } output = compressedStream.ToArray(); } if(IsDebugEnabled) log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length); return output; }
public void GZip_Compress_Extract_Test() { var plainStream = PlainText.ToStream(); plainStream.Seek(0, SeekOrigin.Begin); var plainData = Encoding.UTF8.GetBytes(PlainText); byte[] compressedData; byte[] extractedData; // Compress using(var compressedStream = new MemoryStream()) using(var gzs = new GZipOutputStream(compressedStream)) { gzs.SetLevel(5); gzs.Write(plainData, 0, plainData.Length); gzs.Finish(); compressedData = compressedStream.ToArray(); } Assert.IsNotNull(compressedData); // Extract using(var compressedStream = new MemoryStream(compressedData)) { // compressedStream.Seek(0, SeekOrigin.Begin); using(var gzs = new GZipInputStream(compressedStream)) using(var extractedStream = new MemoryStream()) { StreamTool.CopyStreamToStream(gzs, extractedStream); extractedData = extractedStream.ToArray(); } } Assert.IsNotNull(extractedData); string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0'); Assert.AreEqual(PlainText, extractedText); }
public void TestGZip() { MemoryStream ms = new MemoryStream(); GZipOutputStream outStream = new GZipOutputStream(ms); byte[] buf = new byte[100000]; System.Random rnd = new Random(); rnd.NextBytes(buf); outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); ms.Seek(0, SeekOrigin.Begin); GZipInputStream inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } Assert.AreEqual(0, count); for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
public void DoubleClose() { var memStream = new TrackedMemoryStream(); var s = new GZipOutputStream(memStream); s.Finish(); s.Close(); s.Close(); memStream = new TrackedMemoryStream(); using (GZipOutputStream no2 = new GZipOutputStream(memStream)) { s.Close(); } }
private const int COMPRESS_LEVEL = 7;// 0-9, 9 being the highest compression #endregion #region ICompressionProvider Members public byte[] Compress(byte[] data) { using (var outputStream = new MemoryStream()) { using (var compressStream = new GZipOutputStream(outputStream)) { compressStream.SetLevel(COMPRESS_LEVEL); compressStream.Write(data, 0, data.Length); compressStream.Finish(); compressStream.Close(); return outputStream.ToArray(); } } }
public void WriteAfterFinish() { TrackedMemoryStream memStream=new TrackedMemoryStream(); GZipOutputStream s=new GZipOutputStream(memStream); s.Finish(); try { s.WriteByte(7); Assert.Fail("Write should fail"); } catch { } }
public void DoubleFooter() { TrackedMemoryStream memStream=new TrackedMemoryStream(); GZipOutputStream s=new GZipOutputStream(memStream); s.Finish(); Int64 length=memStream.Length; s.Close(); Assert.AreEqual(length, memStream.ToArray().Length); }
public void TrailingGarbage() { /* ARRANGE */ var ms = new MemoryStream(); var outStream = new GZipOutputStream(ms); // input buffer to be compressed byte[] buf = new byte[100000]; var rnd = new Random(); rnd.NextBytes(buf); // compress input buffer outStream.Write(buf, 0, buf.Length); outStream.Flush(); outStream.Finish(); // generate random trailing garbage and add to the compressed stream byte[] garbage = new byte[4096]; rnd.NextBytes(garbage); ms.Write(garbage, 0, garbage.Length); // rewind the concatenated stream ms.Seek(0, SeekOrigin.Begin); /* ACT */ // decompress concatenated stream var inStream = new GZipInputStream(ms); byte[] buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } /* ASSERT */ Assert.AreEqual(0, count); for (int i = 0; i < buf.Length; ++i) { Assert.AreEqual(buf2[i], buf[i]); } }
public void GzipCompressDecompress() { for (int i = 100; i < 10000;i++ ) { var str = new string('B', i) + "a"; var outputStream = new MemoryStream(); var zoutStream = new GZipOutputStream(outputStream); var buf = Encoding.UTF8.GetBytes(str); zoutStream.Write(buf, 0, buf.Length); zoutStream.Flush(); zoutStream.Finish(); outputStream.Seek(0, SeekOrigin.Begin); //var compressed = new StreamReader(ms).ReadToEnd(); //ms.Seek(0, SeekOrigin.Begin); var inStream = new GZipInputStream(outputStream); var buf2 = new byte[buf.Length]; int currentIndex = 0; int count = buf2.Length; while (true) { int numRead = inStream.Read(buf2, currentIndex, count); if (numRead <= 0) { break; } currentIndex += numRead; count -= numRead; } Assert.AreEqual(0, count); for (var j = 0; j < buf.Length; ++j) { Assert.AreEqual(buf2[j], buf[j]); } } }
/// <summary> /// Stores data to cache. /// /// If data does not already exist for this key on the server, or if the key is being /// deleted, the specified value will not be stored. /// The server will automatically delete the value when the expiration time has been reached. /// /// If compression is enabled, and the data is longer than the compression threshold /// the data will be stored in compressed form. /// /// As of the current release, all objects stored will use .NET serialization. /// </summary> /// <param name="cmdname">action to take (set, add, replace)</param> /// <param name="key">key to store cache under</param> /// <param name="value">object to cache</param> /// <param name="expiry">expiration</param> /// <param name="hashCode">if not null, then the int hashcode to use</param> /// <param name="asString">store this object as a string?</param> /// <returns>true/false indicating success</returns> private bool Set(string cmdname, string key, object obj, DateTime expiry, object hashCode, bool asString) { if(expiry < DateTime.Now) return true; if(cmdname == null || cmdname.Trim().Length == 0 || key == null || key.Length == 0) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set key null")); } return false; } if (memcachedProvider == null) { if (log.IsErrorEnabled) { log.Error(GetLocalizedString("failed to get socket").Replace("$$Host$$","localhost")); } return false; } if (expiry == DateTime.MaxValue) expiry = new DateTime(0); // store flags int flags = 0; // byte array to hold data byte[] val; int length = 0; // useful for sharing data between .NET and non-.NET // and also for storing ints for the increment method if(NativeHandler.IsHandled(obj)) { if(asString) { if(obj != null) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set store data as string").Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType().Name)); } try { val = UTF8Encoding.UTF8.GetBytes(obj.ToString()); } catch(ArgumentException ex) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set invalid encoding").Replace("$$Encoding$$", _defaultEncoding), ex); } return false; } } else { val = new byte[0]; length = 0; } } else { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set store with native handler")); } try { val = NativeHandler.Encode(obj); length = val.Length; } catch(ArgumentException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set failed to native handle object"), e); } return false; } } } else { if(obj != null) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set serializing".Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType().Name))); } // always serialize for non-primitive types try { MemoryStream memStream = new MemoryStream(); new BinaryFormatter().Serialize(memStream, obj); val = memStream.GetBuffer(); length = (int) memStream.Length; flags |= F_SERIALIZED; } catch(IOException e) { // if we fail to serialize, then // we bail if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set failed to serialize").Replace("$$Object$$", obj.ToString()), e); } return false; } } else { val = new byte[0]; length = 0; } } // now try to compress if we want to // and if the length is over the threshold if(_compressEnable && length > _compressThreshold) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set trying to compress data")); log.Info(GetLocalizedString("set size prior").Replace("$$Size$$", length.ToString(new NumberFormatInfo()))); } try { MemoryStream memoryStream = new MemoryStream(); GZipOutputStream gos = new GZipOutputStream(memoryStream); gos.Write(val, 0, length); gos.Finish(); // store it and set compression flag val = memoryStream.GetBuffer(); length = (int)memoryStream.Length; flags |= F_COMPRESSED; if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set compression success").Replace("$$Size$$", length.ToString(new NumberFormatInfo()))); } } catch(IOException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set compression failure"), e); } } } // now write the data to the cache server try { OperationResult opResult = new OperationResult(); switch (cmdname) { case "set": opResult = memcachedProvider.Set(key, (uint)flags, GetExpirationTime(expiry), val); break; case "add": opResult = memcachedProvider.Add(key, (uint)flags, GetExpirationTime(expiry), val); break; case "replace": opResult = memcachedProvider.Replace(key, (uint)flags, GetExpirationTime(expiry), val); break; } string line = ""; if (cmdname.Equals("replace") && opResult.ReturnResult == Result.ITEM_NOT_FOUND) line = "NOT_STORED"; else switch (opResult.ReturnResult) { case Result.SUCCESS: line = "STORED"; break; case Result.ITEM_EXISTS: line = "NOT_STORED"; break; case Result.ITEM_NOT_FOUND: line = "NOT_FOUND"; break; case Result.ITEM_MODIFIED: line = "EXISTS"; break; } string cmd = cmdname + " " + key + " " + flags + " " + GetExpirationTime(expiry) + " " + length + "\r\n"; if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set memcached command result").Replace("$$Cmd$$", cmd).Replace("$$Line$$", line)); } if(STORED == line) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set success").Replace("$$Key$$", key)); } return true; } else if(NOTSTORED == line) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set not stored").Replace("$$Key$$", key)); } } else { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set error").Replace("$$Key$$", key).Replace("$$Size$$", length.ToString(new NumberFormatInfo())).Replace("$$Line$$", line)); } } } catch(Exception e) { // exception thrown if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set IOException"), e); } } return false; }
/// <summary> /// Serializes a message to the GenuineChunkedStream stream. /// </summary> /// <param name="stream">Stream to serialize the message to.</param> /// <param name="message">The message being serialized.</param> /// <param name="compress">Indicates whether content should be compressed.</param> public static void Serialize(GenuineChunkedStream stream, Message message, bool compress) { // write a mark whether the content will be compressed BinaryWriter binaryWriter = new BinaryWriter(stream); binaryWriter.Write((bool) compress); // gather messages into a separate stream if compression is required GenuineChunkedStream usedStream = stream; if (compress) usedStream = new GenuineChunkedStream(false); binaryWriter = new BinaryWriter(usedStream); // all simple values binaryWriter.Write((byte) message.GenuineMessageType); binaryWriter.Write(message.MessageId); binaryWriter.Write(message.ReplyToId); // binaryWriter.Write(message.Recipient.Uri); binaryWriter.Write((bool) message.IsOneWay); // set DestinationMarshalByRef if it is specified in headers object broadcastObjRefOrCourt = message.ITransportHeaders[Message.TransportHeadersBroadcastObjRefOrCourt]; if (broadcastObjRefOrCourt != null) message.DestinationMarshalByRef = broadcastObjRefOrCourt; // objref if it exists if (message.DestinationMarshalByRef != null) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryWriter.Write( true ); binaryFormatter.Serialize(binaryWriter.BaseStream, message.DestinationMarshalByRef); } else binaryWriter.Write( false ); // Security Session parameters binaryWriter.Write((Int16) message.SecuritySessionParameters.Attributes); binaryWriter.Write(message.SecuritySessionParameters.RemoteTransportUser); // now headers foreach (DictionaryEntry entry in message.ITransportHeaders) { string key = entry.Key as string; if (key == null || key == "__" || key == Message.TransportHeadersBroadcastObjRefOrCourt || entry.Value == null || key == Message.TransportHeadersGenuineMessageType) continue; string val = entry.Value.ToString(); // now write these strings binaryWriter.Write(key); binaryWriter.Write(val); } // headers end tag binaryWriter.Write("__"); // and finally the content usedStream.WriteStream(message.Stream); // compress the content if (compress) { GZipOutputStream compressingStream = new GZipOutputStream(new NonClosableStream(stream)); GenuineUtility.CopyStreamToStream(usedStream, compressingStream); compressingStream.Finish(); } }
public void flush() { byte[] uncompressed_bytes = uncompressed_output.ToArray(); // write uncompressed size to output new BinaryWriter(output).Write((UInt32)uncompressed_bytes.Length); // write the compressed data GZipOutputStream zipstream = new GZipOutputStream(this.output); zipstream.SetLevel(1); // 0 is no compression, 9 is best compression (slowest) zipstream.Write(uncompressed_bytes, 0, uncompressed_bytes.Length); zipstream.Finish(); }
/// <summary> /// Stores data to cache. /// /// If data does not already exist for this key on the server, or if the key is being /// deleted, the specified value will not be stored. /// The server will automatically delete the value when the expiration time has been reached. /// /// If compression is enabled, and the data is longer than the compression threshold /// the data will be stored in compressed form. /// /// As of the current release, all objects stored will use .NET serialization. /// </summary> /// <param name="cmdname">action to take (set, add, replace)</param> /// <param name="key">key to store cache under</param> /// <param name="value">object to cache</param> /// <param name="expiry">expiration</param> /// <param name="hashCode">if not null, then the int hashcode to use</param> /// <param name="asString">store this object as a string?</param> /// <returns>true/false indicating success</returns> private bool Set(string cmdname, string key, object obj, DateTime expiry, object hashCode, bool asString) { if(expiry < DateTime.Now) return true; if(cmdname == null || cmdname.Trim().Length == 0 || key == null || key.Length == 0) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set key null")); } return false; } // get SockIO obj SockIO sock = SockIOPool.GetInstance(_poolName).GetSock(key, hashCode); if(sock == null) return false; if(expiry == DateTime.MaxValue) expiry = new DateTime(0); // store flags int flags = 0; // byte array to hold data byte[] val; int length = 0; // useful for sharing data between .NET and non-.NET // and also for storing ints for the increment method if(NativeHandler.IsHandled(obj)) { if(asString) { if(obj != null) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set store data as string").Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType().Name)); } try { val = UTF8Encoding.UTF8.GetBytes(obj.ToString()); length = val.Length; } catch(ArgumentException ex) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set invalid encoding").Replace("$$Encoding$$", _defaultEncoding), ex); } sock.Close(); sock = null; return false; } } else { val = new byte[0]; length = 0; } } else { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set store with native handler")); } try { val = NativeHandler.Encode(obj); length = val.Length; } catch(ArgumentException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set failed to native handle object"), e); } sock.Close(); sock = null; return false; } } } else { if(obj != null) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set serializing".Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType().Name))); } // always serialize for non-primitive types try { MemoryStream memStream = new MemoryStream(); new BinaryFormatter().Serialize(memStream, obj); val = memStream.GetBuffer(); length = (int) memStream.Length; flags |= F_SERIALIZED; } catch(IOException e) { // if we fail to serialize, then // we bail if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set failed to serialize").Replace("$$Object$$", obj.ToString()), e); } // return socket to pool and bail sock.Close(); sock = null; return false; } } else { val = new byte[0]; length = 0; } } // now try to compress if we want to // and if the length is over the threshold if(_compressEnable && length > _compressThreshold) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set trying to compress data")); log.Info(GetLocalizedString("set size prior").Replace("$$Size$$", length.ToString(new NumberFormatInfo()))); } try { MemoryStream memoryStream = new MemoryStream(); GZipOutputStream gos = new GZipOutputStream(memoryStream); gos.Write(val, 0, length); gos.Finish(); // store it and set compression flag val = memoryStream.GetBuffer(); length = (int)memoryStream.Length; flags |= F_COMPRESSED; if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set compression success").Replace("$$Size$$", length.ToString(new NumberFormatInfo()))); } } catch(IOException e) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set compression failure"), e); } } } // now write the data to the cache server try { string cmd = cmdname + " " + key + " " + flags + " " + GetExpirationTime(expiry) + " " + length + "\r\n"; sock.Write(UTF8Encoding.UTF8.GetBytes(cmd)); sock.Write(val,0,length); sock.Write(UTF8Encoding.UTF8.GetBytes("\r\n")); sock.Flush(); // get result code string line = sock.ReadLine(); if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set memcached command result").Replace("$$Cmd$$", cmd).Replace("$$Line$$", line)); } if(STORED == line) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set success").Replace("$$Key$$", key)); } sock.Close(); sock = null; return true; } else if(NOTSTORED == line) { if(log.IsInfoEnabled) { log.Info(GetLocalizedString("set not stored").Replace("$$Key$$", key)); } } else { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set error").Replace("$$Key$$", key).Replace("$$Size$$", length.ToString(new NumberFormatInfo())).Replace("$$Line$$", line)); } } } catch(IOException e) { // exception thrown if(log.IsErrorEnabled) { log.Error(GetLocalizedString("set IOException"), e); } try { sock.TrueClose(); } catch(IOException ioe) { if(log.IsErrorEnabled) { log.Error(GetLocalizedString("failed to close some socket").Replace("$$Socket$$", sock.ToString()), ioe); } } sock = null; } if(sock != null) sock.Close(); return false; }
/// zip a utf8 string using gzip into a base64 encoded string public static string ZipString(string ATextToCompress) { Byte[] originalText = Encoding.UTF8.GetBytes(ATextToCompress); MemoryStream memoryStream = new MemoryStream(); GZipOutputStream gzStream = new GZipOutputStream(memoryStream); gzStream.Write(originalText, 0, originalText.Length); gzStream.Flush(); gzStream.Finish(); memoryStream.Position = 0; Byte[] compressedBuffer = new byte[memoryStream.Length]; memoryStream.Read(compressedBuffer, 0, compressedBuffer.Length); gzStream.Close(); return Convert.ToBase64String(compressedBuffer); }
public void CompressAndSendBuffer(ref byte []buff,DataType bDataType,ref int nX ,ref int nY, ref int width, ref int height,ref int length) { byte[] buffer=null; if(buff != null) { try { // Console.WriteLine("Uncompressed = " + buffer.Length.ToString()); //MemoryStream msCompressed = new MemoryStream(); //ICSharpCode.SharpZipLib.Zip zosCOmp=new ICSharpCode.SharpZipLib.Zip( MemoryStream msComp = new MemoryStream(); GZipOutputStream zosComp=new GZipOutputStream(msComp);// zosComp.Write(buff, 0, buff.Length); zosComp.Finish(); buffer =msComp.ToArray(); zosComp.Close(); } catch(Exception ex) { WebMeeting.Client.ClientUI.getInstance().ShowExceptionMessage("Module ::: AppShare public void CompressAndSendBuffer(ref byte []buff,DataType bDataType,ref int nX ,ref int nY, ref int width, ref int height,ref int length)",ex,"",false); } //byte[] bb=UncompressBufferEx(ref buffer,length); //bb=bb; } /* if(buffer!=null) { writer.WriteLine(buffer.Length.ToString()); writer.Flush(); } */ try { OnDataAvailable(ref buffer,bDataType,ref nX,ref nY,ref width,ref height,ref length); } catch(Exception ex) { WebMeeting.Client.ClientUI.getInstance().ShowExceptionMessage("Module ::: AppShare public void CompressAndSendBuffer(ref byte []buff,DataType bDataType,ref int nX ,ref int nY, ref int width, ref int height,ref int length)",ex,"",false); } }