public void Save() { List <CompressedFileRecord> objSavedRecords = new List <CompressedFileRecord>(); string strTempFilePath = Path.GetTempFileName(); using (FileStream objTempFileStream = new FileStream(strTempFilePath, FileMode.Create, FileAccess.ReadWrite)) { CompressedFileRecord[] objInsertedRecords = CompressedFileRecordManager.InsertedRecords; foreach (CompressedFileRecord objInsertedRecord in objInsertedRecords) { using (FileStream objInputFile = new FileStream(objInsertedRecord.OriginalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { int intStartingPosition = (int)objTempFileStream.Position; CompressionManager.Compress(objInputFile, objTempFileStream); int intCompressedSize = (int)objTempFileStream.Position - intStartingPosition; CompressedFileRecord objSavedRecord = new CompressedFileRecord(objInsertedRecord.OriginalFilePath, intCompressedSize, objInsertedRecord.RelativePath); objSavedRecord.Keys.AddRange(objInsertedRecord.Keys); objSavedRecords.Add(objSavedRecord); } objTempFileStream.Flush(); } CompressedFileRecord[] objUpdatedRecords = CompressedFileRecordManager.UpdatedRecords; foreach (CompressedFileRecord objUpdatedRecord in objUpdatedRecords) { int intFileStartIndex = CompressedFileRecordManager.FindFileStartIndex(objUpdatedRecord); if (intFileStartIndex != -1) { CompressedStream.Position = intFileStartIndex; CompressionManager.CopyToStream(CompressedStream, objTempFileStream, objUpdatedRecord.CompressedSize); objSavedRecords.Add(objUpdatedRecord); } objTempFileStream.Flush(); } objTempFileStream.Position = 0; CompressedStream.SetLength(0); CompressedStream.Position = 0; CompressedStream.Write(CompressedStreamSignature, 0, CompressedStreamSignature.Length); string strSavedRecords = CompressedFileRecordManager.ToString(objSavedRecords.ToArray()); byte[] bytSavedRecords = ASCIIEncoding.ASCII.GetBytes(strSavedRecords); byte[] bytCompressedRecords = CompressionManager.Compress(bytSavedRecords); int intRecordLength = bytCompressedRecords.Length; byte[] bytHeaderRecordLength = BitConverter.GetBytes(intRecordLength); CompressedStream.Write(bytHeaderRecordLength, 0, bytHeaderRecordLength.Length); CompressedStream.Write(bytCompressedRecords, 0, bytCompressedRecords.Length); CompressionManager.CopyToStream(objTempFileStream, CompressedStream); CompressedStream.Flush(); } File.Delete(strTempFilePath); Refresh(); }
public void TestSetLength() { using (var stream = new CompressedStream(new DummyNetworkStream())) { Assert.Throws <NotSupportedException> (() => { var x = stream.Length; }); Assert.Throws <NotSupportedException> (() => stream.SetLength(500)); } }
internal async Task ListenAsync() { await Task.Yield(); var buff = new byte[BUFFER_SIZE]; var buffseg = new ArraySegment <byte>(buff); byte[] resultbuff = null; WebSocketReceiveResult result = null; SocketCloseEventArgs close = null; var token = Token; try { while (!token.IsCancellationRequested && Socket.State == WebSocketState.Open) { using (var ms = new MemoryStream()) { do { result = await Socket.ReceiveAsync(buffseg, token).ConfigureAwait(false); if (result.MessageType == WebSocketMessageType.Close) { var cc = result.CloseStatus != null ? (int)result.CloseStatus.Value : -1; close = new SocketCloseEventArgs(null) { CloseCode = cc, CloseMessage = result.CloseStatusDescription }; } else { ms.Write(buff, 0, result.Count); } }while (!result.EndOfMessage); resultbuff = ms.ToArray(); } if (close != null) { break; } var resultstr = ""; if (result.MessageType == WebSocketMessageType.Binary) { if (resultbuff[0] == 0x78) { await CompressedStream.WriteAsync(resultbuff, 2, resultbuff.Length - 2).ConfigureAwait(false); } else { await CompressedStream.WriteAsync(resultbuff, 0, resultbuff.Length).ConfigureAwait(false); } await CompressedStream.FlushAsync().ConfigureAwait(false); CompressedStream.Position = 0; // partial credit to FiniteReality // overall idea is their // I tuned the finer details // -Emzi var sfix = BitConverter.ToUInt16(resultbuff, resultbuff.Length - 2); if (sfix != ZLIB_STREAM_SUFFIX) { using (var zlib = new DeflateStream(CompressedStream, CompressionMode.Decompress, true)) { await zlib.CopyToAsync(DecompressedStream).ConfigureAwait(false); } } else { await StreamDecompressor.CopyToAsync(DecompressedStream).ConfigureAwait(false); } resultbuff = DecompressedStream.ToArray(); DecompressedStream.Position = 0; DecompressedStream.SetLength(0); CompressedStream.Position = 0; CompressedStream.SetLength(0); } resultstr = UTF8.GetString(resultbuff, 0, resultbuff.Length); await CallOnMessageAsync(resultstr).ConfigureAwait(false); } } catch (Exception e) { close = new SocketCloseEventArgs(null) { CloseCode = -1, CloseMessage = e.Message }; } await DisconnectAsync(close).ConfigureAwait(false); }