internal ParticleSystemCompiledShaderData(byte[] shaderCode, bool isXbox, int colourIndex, int userIndex, int lifeIndex) { if (shaderCode == null) throw new ArgumentNullException(); //compress the shader code if (isXbox) { //deflate stream isn't supported shaderCode = Xen.Graphics.ShaderSystem.ShaderSystemBase.SimpleCompress(shaderCode); } else { #if !XBOX360 using (MemoryStream stream = new MemoryStream()) using (BinaryWriter writer = new BinaryWriter(stream)) { writer.Write(shaderCode.Length); using (System.IO.Compression.DeflateStream d = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Compress)) { d.Write(shaderCode, 0, shaderCode.Length); d.Flush(); } shaderCode = stream.ToArray(); } #endif } this.CompressedShaderCode = shaderCode; this.ColourSamplerIndex = colourIndex; this.UserSamplerIndex = userIndex; this.LifeSamplerIndex = lifeIndex; }
} // End Sub CompressFile public static void DeflateCompressFile(string FileToCompress, string CompressedFile) { //byte[] buffer = new byte[1024 * 1024 * 64]; byte[] buffer = new byte[1024 * 1024]; // 1MB using (System.IO.FileStream sourceFile = System.IO.File.OpenRead(FileToCompress)) { using (System.IO.FileStream destinationFile = System.IO.File.Create(CompressedFile)) { using (System.IO.Compression.DeflateStream output = new System.IO.Compression.DeflateStream(destinationFile, System.IO.Compression.CompressionMode.Compress)) { int bytesRead = 0; while (bytesRead < sourceFile.Length) { int ReadLength = sourceFile.Read(buffer, 0, buffer.Length); output.Write(buffer, 0, ReadLength); output.Flush(); bytesRead += ReadLength; } // Whend destinationFile.Flush(); } // End Using System.IO.Compression.GZipStream output destinationFile.Close(); } // End Using System.IO.FileStream destinationFile // Close the files. sourceFile.Close(); } // End Using System.IO.FileStream sourceFile } // End Sub CompressFile
public override void Flush() { if (deflateStream != null) { deflateStream.Flush(); } }
/// <summary> /// Compress with Stream /// </summary> public static Stream Compress(Stream input) { System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream( input, System.IO.Compression.CompressionMode.Compress, true); stream.Flush(); return input; }
/// <summary> /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// </summary> /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> public override void Flush() { #if PORTABLE ExceptionHelper.ThrowNotSupported(); #else _deflateStream.Flush(); #endif }
static void Decode(Stream s) { // skip first two bytes Console.WriteLine(s.Length); s.ReadByte(); s.ReadByte(); using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(s, System.IO.Compression.CompressionMode.Decompress)) { ds.Flush(); byte []b = new byte[100]; int a = ds.Read(b, 0, 100); Console.WriteLine(a); string r = new StreamReader(ds).ReadToEnd(); Console.WriteLine(r); } }
public void DecompressData() { byte[] data = new byte[] { 43, 73, 45, 46, 1, 0 }; byte[] data2; using (var streamUncompressed = new System.IO.MemoryStream()) using (var streamCompressed = new System.IO.MemoryStream(data)) { var deflateStream = new System.IO.Compression.DeflateStream(streamCompressed, System.IO.Compression.CompressionMode.Decompress, true); deflateStream.CopyTo(streamUncompressed); deflateStream.Flush(); deflateStream.Close(); data2 = streamUncompressed.ToArray(); } }
public static string Decompress(string strSource, int length) { byte[] buffer = Convert.FromBase64String(strSource); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress); stream.Flush(); int nSize = length; byte[] decompressBuffer = new byte[nSize]; int nSizeIncept = stream.Read(decompressBuffer, 0, nSize); stream.Close(); return System.Text.Encoding.Unicode.GetString(decompressBuffer, 0, nSizeIncept);//转换为普通的字符串 }
public static string Decompress(string strSource, int length) { byte[] buffer = Convert.FromBase64String(strSource); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress); stream.Flush(); int nSize = length; byte[] decompressBuffer = new byte[nSize]; int nSizeIncept = stream.Read(decompressBuffer, 0, nSize); stream.Close(); return(System.Text.Encoding.Unicode.GetString(decompressBuffer, 0, nSizeIncept));//转换为普通的字符串 }
} // End Function Compress // http://www.dotnetperls.com/compress /// <summary> /// Compresses byte array to new byte array. /// </summary> public static byte[] DeflateCompress(byte[] raw) { byte[] baRetVal; using (System.IO.MemoryStream memstrm = new System.IO.MemoryStream()) { using (System.IO.Compression.DeflateStream gzip = new System.IO.Compression.DeflateStream(memstrm, System.IO.Compression.CompressionMode.Compress, true)) { gzip.Write(raw, 0, raw.Length); gzip.Flush(); } // End Using System.IO.Compression.GZipStream gzip memstrm.Flush(); baRetVal = memstrm.ToArray(); memstrm.Close(); } // End Using System.IO.MemoryStream memory return(baRetVal); } // End Function Compress
internal static byte[] Deflate(byte[] source_byte, System.IO.Compression.CompressionMode type_deflate) { byte[] dest_byte = null; using (System.IO.MemoryStream dest_mem = new System.IO.MemoryStream()) { using (System.IO.MemoryStream source_mem = new System.IO.MemoryStream(source_byte)) { if (source_mem.CanSeek) { source_mem.Seek(0, System.IO.SeekOrigin.Begin); } if (type_deflate == System.IO.Compression.CompressionMode.Compress) { using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(dest_mem, type_deflate)) { source_mem.CopyTo(deflate); deflate.Flush(); } } else { using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(source_mem, type_deflate)) { deflate.CopyTo(dest_mem); deflate.Flush(); } } source_mem.Flush(); } if (dest_mem.CanSeek) { dest_mem.Seek(0, System.IO.SeekOrigin.Begin); } dest_byte = dest_mem.ToArray(); dest_mem.Flush(); } return(dest_byte); }
/// <summary> /// Compresses and returns the <paramref name="source"/> byte array using the <see cref="System.IO.Compression.DeflateStream"/> algorithm. /// </summary> /// <param name="source">The byte array to compress.</param> /// <returns>The compressed <paramref name="source"/> byte array.</returns> public byte[] Compress(byte[] source) { if (source == null) { return(source); } byte[] result = null; using (var ms = new System.IO.MemoryStream()) { using (var ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress, true)) { ds.Write(source, 0, source.Length); ds.Flush(); ds.Close(); } result = ms.ToArray(); ms.Close(); } return(result); }
public static object DecompressAndDecode(byte[] input) { MemoryStream compressedStream = new MemoryStream(input); compressedStream.Seek(2, SeekOrigin.Begin); //skip zlib header, we're only interested in the deflated bytes byte[] outputBytes = new byte[4096]; DeflateStream stream = new DeflateStream(compressedStream, CompressionMode.Decompress); try { stream.Read(outputBytes, 0, (int) input.Length); stream.Flush(); stream.Close(); return Rencode.Decode(outputBytes); } catch (Exception e) { //Console.WriteLine("Exception reading stream"); } return null; }
public async void RemoteManageLoop(HttpListenerContext context) { try { LineLog.Debug("serving: " + context.Request.RawUrl); var req = context.Request; var resp = context.Response; var msg = getResponse(req); resp.StatusCode = (int)msg.StatusCode; LineLog.Debug("returning: " + resp.StatusCode); foreach (var h in msg.Headers) { foreach (var v in h.Value) { resp.Headers.Add(h.Key, v); } } if (resp.StatusCode != 303) { using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes("Critical Failure"); bool expires = false; if (msg.Content is StringContent) { var qw = msg.Content as StringContent; string qq = await qw.ReadAsStringAsync(); resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "text/html"; // Default to HTML } outBytes = Encoding.UTF8.GetBytes(qq); } else if (msg.Content is FileContent) { var qw = msg.Content as FileContent; resp.ContentType = qw.Type; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Should always be set, but bin just incase } //resp.Headers.Add("Content-Disposition", $"attachment; filename=\"{qw.FileName}\""); outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is ByteArrayContent) { var qw = msg.Content as ByteArrayContent; resp.ContentType = mimeTypes.Where(t => req.Url.AbsolutePath.EndsWith(t.Key)).FirstOrDefault().Value; if (resp.ContentType == null) { resp.ContentType = "application/octet-stream"; // Default to binary } outBytes = await qw.ReadAsByteArrayAsync(); } else if (msg.Content is StreamContent) { var qw = msg.Content as StreamContent; using (var ms = new MemoryStream()) { var stream = await qw.ReadAsStreamAsync(); stream.CopyTo(ms); //resp.ContentType = "application/octet-stream" outBytes = ms.ToArray(); } } //resp.Headers.Add("Content-language", "en-au"); resp.Headers.Add("Access-Control-Allow-Origin: *"); resp.Headers.Add("Access-Control-Allow-Methods: *"); if (expires) { resp.Headers.Add("Cache-Control", "no-cache, no-store, must-revalidate"); resp.Headers.Add("Pragma", "no-cache"); resp.Headers.Add("Expires", "Wed, 16 Jul 1969 13:32:00 UTC"); } if ((outBytes.Length > 0) && (resp.ContentType != "image/png")) // Don't compress empty responses or compressed file types { var enc = req.Headers.GetValues("Accept-Encoding"); if (enc?.Contains("gzip") ?? false) { using (MemoryStream ms = new MemoryStream()) using (System.IO.Compression.GZipStream gs = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress)) { gs.Write(outBytes, 0, outBytes.Length); gs.Flush(); gs.Close(); // https://stackoverflow.com/questions/3722192/how-do-i-use-gzipstream-with-system-io-memorystream#comment3929538_3722263 ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "gzip"); } else if (enc?.Any(a => a.ToLowerInvariant() == "deflate") ?? false) { using (MemoryStream ms = new MemoryStream()) { using (System.IO.Compression.DeflateStream ds = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress)) { ds.Write(outBytes, 0, outBytes.Length); ds.Flush(); } ms.Flush(); outBytes = ms.ToArray(); } resp.Headers.Add("Content-Encoding", "deflate"); } } resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } else { resp.OutputStream.Close(); } } catch (Exception e) { LineLog.Error("Something went wrong.", e); var resp = context.Response; resp.StatusCode = 500; using (var output = resp.OutputStream) { byte[] outBytes = Encoding.UTF8.GetBytes(e.GetType() + ": " + e.Message); resp.ContentLength64 = outBytes.Length; try { output.Write(outBytes, 0, outBytes.Length); } catch (Exception ex) { Program.LineLog.Error("Failed to write " + ex.GetType().ToString() + " " + ex.Message); } } } }
private void WriteDataChunks() { byte[] pixels = _image.Pixels; byte[] data = new byte[_image.PixelWidth * _image.PixelHeight * 4 + _image.PixelHeight]; int rowLength = _image.PixelWidth * 4 + 1; for (int y = 0; y < _image.PixelHeight; y++) { byte compression = 0; if (y > 0) { compression = 2; } data[y * rowLength] = compression; for (int x = 0; x < _image.PixelWidth; x++) { // Calculate the offset for the new array. int dataOffset = y * rowLength + x * 4 + 1; // Calculate the offset for the original pixel array. int pixelOffset = (y * _image.PixelWidth + x) * 4; data[dataOffset + 0] = pixels[pixelOffset + 0]; data[dataOffset + 1] = pixels[pixelOffset + 1]; data[dataOffset + 2] = pixels[pixelOffset + 2]; data[dataOffset + 3] = pixels[pixelOffset + 3]; if (y > 0) { int lastOffset = ((y - 1) * _image.PixelWidth + x) * 4; data[dataOffset + 0] -= pixels[lastOffset + 0]; data[dataOffset + 1] -= pixels[lastOffset + 1]; data[dataOffset + 2] -= pixels[lastOffset + 2]; data[dataOffset + 3] -= pixels[lastOffset + 3]; } } } byte[] buffer = null; int bufferLength = 0; MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(); //using (Ionic.Zlib.DeflateStream zStream = new Ionic.Zlib.DeflateStream(memoryStream, Ionic.Zlib.CompressionMode.Compress)) using (System.IO.Compression.DeflateStream zStream = new System.IO.Compression.DeflateStream(memoryStream, System.IO.Compression.CompressionMode.Compress)) { zStream.Write(data, 0, data.Length); zStream.Flush(); bufferLength = (int)memoryStream.Length; buffer = memoryStream.GetBuffer(); zStream.Close(); } //using (DeflaterOutputStream zStream = new DeflaterOutputStream(memoryStream)) //{ // zStream.Write(data, 0, data.Length); // zStream.Flush(); // zStream.Finish(); // bufferLength = (int)memoryStream.Length; // buffer = memoryStream.GetBuffer(); //} } finally { if (memoryStream != null) { memoryStream.Dispose(); } } int numChunks = bufferLength / MaxBlockSize; if (bufferLength % MaxBlockSize != 0) { numChunks++; } for (int i = 0; i < numChunks; i++) { int length = bufferLength - i * MaxBlockSize; if (length > MaxBlockSize) { length = MaxBlockSize; } WriteChunk(PngChunkTypes.Data, buffer, i * MaxBlockSize, length); } }
internal static byte[] Deflate(byte[] source_byte, System.IO.Compression.CompressionMode type_deflate) { byte[] dest_byte = null; using (System.IO.MemoryStream dest_mem = new System.IO.MemoryStream()) { using (System.IO.MemoryStream source_mem = new System.IO.MemoryStream(source_byte)) { if (source_mem.CanSeek) { source_mem.Seek(0, System.IO.SeekOrigin.Begin); } if (type_deflate == System.IO.Compression.CompressionMode.Compress) { using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(dest_mem, type_deflate)) { source_mem.CopyTo(deflate); deflate.Flush(); } } else { using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(source_mem, type_deflate)) { deflate.CopyTo(dest_mem); deflate.Flush(); } } source_mem.Flush(); } if (dest_mem.CanSeek) { dest_mem.Seek(0, System.IO.SeekOrigin.Begin); } dest_byte = dest_mem.ToArray(); dest_mem.Flush(); } return dest_byte; }
public static void Close(this System.IO.Compression.DeflateStream strm) { strm.Flush(); }
public void CreateFromStream(Stream _stream, DateTime _cacheTime, EventHandler <CacheWriteProgressChangedEventArgs> progressCallback) { Microsoft.VisualBasic.FileIO.FileSystem.CreateDirectory(this.innerfi.DirectoryName); bool cancel = false; using (BufferedStream bufferStream = new BufferedStream(_stream, 1024)) using (FileStream fs = this.innerfi.Create()) { byte[] laiwhg; if (this.IsCompressedStream(bufferStream, out laiwhg)) { BinaryWriter bw = new BinaryWriter(fs); bw.Write(false); bw.Flush(); long totalread = 0; if (laiwhg != null && laiwhg.Length > 0) { fs.Write(laiwhg, 0, laiwhg.Length); totalread = laiwhg.Length; } byte[] arr = new byte[1024]; int readbyte = bufferStream.Read(arr, 0, arr.Length); while (readbyte > 0) { if (cancel) { fs.Flush(); fs.Close(); this.innerfi.Delete(); return; } fs.Write(arr, 0, readbyte); totalread += readbyte; if (progressCallback != null) { var myEvent = new CacheWriteProgressChangedEventArgs(totalread); progressCallback.Invoke(this, myEvent); cancel = myEvent.Cancel; } readbyte = bufferStream.Read(arr, 0, arr.Length); } fs.Flush(); } else { BinaryWriter bw = new BinaryWriter(fs); bw.Write(true); bw.Flush(); using (System.IO.Compression.DeflateStream localfile = new System.IO.Compression.DeflateStream(fs, System.IO.Compression.CompressionMode.Compress)) { long totalread = 0; if (laiwhg != null && laiwhg.Length > 0) { localfile.Write(laiwhg, 0, laiwhg.Length); totalread = laiwhg.Length; } byte[] arr = new byte[1024]; int readbyte = bufferStream.Read(arr, 0, arr.Length); while (readbyte > 0) { if (cancel) { localfile.Flush(); localfile.Close(); this.innerfi.Delete(); return; } localfile.Write(arr, 0, readbyte); totalread += readbyte; if (progressCallback != null) { var myEvent = new CacheWriteProgressChangedEventArgs(totalread); progressCallback.Invoke(this, myEvent); cancel = myEvent.Cancel; } readbyte = bufferStream.Read(arr, 0, arr.Length); } localfile.Flush(); } } } this.innerfi.CreationTimeUtc = _cacheTime; this.innerfi.LastWriteTimeUtc = _cacheTime; if (!this._disposed) { this.CacheCreateCompleted?.Invoke(this, System.EventArgs.Empty); } }
private void WriteDataChunks() { byte[] pixels = _image.Pixels; byte[] data = new byte[_image.PixelWidth * _image.PixelHeight * 4 + _image.PixelHeight]; int rowLength = _image.PixelWidth * 4 + 1; for (int y = 0; y < _image.PixelHeight; y++) { byte compression = 0; if (y > 0) { compression = 2; } data[y * rowLength] = compression; for (int x = 0; x < _image.PixelWidth; x++) { // Calculate the offset for the new array. int dataOffset = y * rowLength + x * 4 + 1; // Calculate the offset for the original pixel array. int pixelOffset = (y * _image.PixelWidth + x) * 4; data[dataOffset + 0] = pixels[pixelOffset + 0]; data[dataOffset + 1] = pixels[pixelOffset + 1]; data[dataOffset + 2] = pixels[pixelOffset + 2]; data[dataOffset + 3] = pixels[pixelOffset + 3]; if (y > 0) { int lastOffset = ((y - 1) * _image.PixelWidth + x) * 4; data[dataOffset + 0] -= pixels[lastOffset + 0]; data[dataOffset + 1] -= pixels[lastOffset + 1]; data[dataOffset + 2] -= pixels[lastOffset + 2]; data[dataOffset + 3] -= pixels[lastOffset + 3]; } } } byte[] buffer = null; int bufferLength = 0; MemoryStream memoryStream = null; try { memoryStream = new MemoryStream(); //using (Ionic.Zlib.DeflateStream zStream = new Ionic.Zlib.DeflateStream(memoryStream, Ionic.Zlib.CompressionMode.Compress)) using (System.IO.Compression.DeflateStream zStream = new System.IO.Compression.DeflateStream(memoryStream, System.IO.Compression.CompressionMode.Compress)) { zStream.Write(data, 0, data.Length); zStream.Flush(); bufferLength = (int)memoryStream.Length; buffer = memoryStream.GetBuffer(); zStream.Close(); } //using (DeflaterOutputStream zStream = new DeflaterOutputStream(memoryStream)) //{ // zStream.Write(data, 0, data.Length); // zStream.Flush(); // zStream.Finish(); // bufferLength = (int)memoryStream.Length; // buffer = memoryStream.GetBuffer(); //} } finally { if (memoryStream != null) { memoryStream.Dispose(); } } int numChunks = bufferLength / MaxBlockSize; if (bufferLength % MaxBlockSize != 0) { numChunks++; } for (int i = 0; i < numChunks; i++) { int length = bufferLength - i * MaxBlockSize; if (length > MaxBlockSize) { length = MaxBlockSize; } WriteChunk(PngChunkTypes.Data, buffer, i * MaxBlockSize, length); } }
/// <summary> /// Clears all buffers for this stream and causes any buffered data to be written to the underlying device. /// </summary> /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception> public override void Flush() { #if !PORTABLE _deflateStream.Flush(); #endif }