public void putFile() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("这个请求是一个普通文件"); Console.ResetColor(); outputStream.WriteLine("HTTP/1.0 200 OK"); outputStream.WriteLine("Content-Type: application/octet-stream"); outputStream.WriteLine("Content-Disposition: attachment"); outputStream.WriteLine("Connection: close"); outputStream.WriteLine(""); outputStream.Flush(); FileStream stream = new FileStream(http_url_ondisk, FileMode.Open, FileAccess.Read); try { byte[] buff2 = new byte[1024]; int count = 0; while ((count = stream.Read(buff2, 0, 1024)) != 0) { outputByteStream.Write(buff2, 0, count); } outputByteStream.Flush(); outputByteStream.Close(); stream.Close(); } catch (Exception) { Console.WriteLine("停止传输文件"); stream.Close(); outputByteStream.Close(); } }
public void BadXmlTests() { try { EncodeHelper.Deserialize("<garbage />", typeof(System.Object)); Assert.Fail("You should not have been able to deserialize this preceding xml"); } catch { } MemoryStream ms = new MemoryStream(); using (StreamWriter sw = new StreamWriter(ms)) { sw.Write("<garbage />"); sw.Flush(); object test = EncodeHelper.Deserialize(ms); Assert.AreEqual(null, test); } try { //Test the Deserialize method that builds an error message. ms = new MemoryStream(); using (BufferedStream sw = new BufferedStream(ms)) { sw.Write(Encoding.UTF8.GetPreamble(), 0, 3); byte[] msg = System.Text.Encoding.UTF8.GetBytes("<garbage>"); sw.Write(msg, 0, msg.Length); sw.Flush(); ms.Position = 0; object test = EncodeHelper.Deserialize(ms); Assert.AreEqual(null, test); } } catch (Exception ex) { if (ex.Message.IndexOf("Couldn't parse XML") == -1) Assert.Fail("We should have failed for a bad xml file."); } }
public static void GetObjectPartly() { const string localFilePath = "<your localFilePath>"; const string bucketName = "<your bucketName>"; const string fileKey = "<your fileKey>"; const string accessId = "<your access id>"; const string accessKey = "<your access key>"; _client = new OssClient(accessId, accessKey); using (var fileStream = new FileStream(localFilePath, FileMode.OpenOrCreate)) { var bufferedStream = new BufferedStream(fileStream); var objectMetadata = _client.GetObjectMetadata(bucketName, fileKey); var fileLength = objectMetadata.ContentLength; const int partSize = 1024 * 1024 * 10; var partCount = CalPartCount(fileLength, partSize); for (var i = 0; i < partCount; i++) { var startPos = partSize * i; var endPos = partSize * i + (partSize < (fileLength - startPos) ? partSize : (fileLength - startPos)) - 1; Download(bufferedStream, startPos, endPos, localFilePath, bucketName, fileKey); } bufferedStream.Flush(); } }
static void Main(string[] args) { FileStream fs1 = new FileStream("out.bin",FileMode.Create,FileAccess.Write,FileShare.Read); FileStream fs2 = new FileStream("out2.bin",FileMode.Create,FileAccess.Write,FileShare.Read); bf1 = new BufferedStream(fs1); bf2 = new BufferedStream(fs2); try { Server server = new Server(10, 4096* 100 * 2); server.Sequential = false; server.Start(new IPEndPoint(IPAddress.Any, 40004)); server.MessageReceived += OnMessageReceived; server.ClientConnected += OnClientConnected; server.ClientDisconnected += OnClientDisconnected; Console.ReadKey(); } finally { bf1.Flush(); bf2.Flush(); bf1.Close(); bf2.Close(); fs1.Close(); fs2.Close(); } }
public override void Authenticate(NpgsqlConnector context, byte[] password) { NpgsqlPasswordPacket pwpck = new NpgsqlPasswordPacket(password); BufferedStream stream = new BufferedStream(context.Stream); pwpck.WriteToStream(stream); stream.Flush(); }
public byte[] Serialize(object o) { if (o == null) { throw new ArgumentNullException("o"); } var type = o.GetType(); if (!type.IsDefined(typeof(SerializableAttribute), true)) { throw new ArgumentException( string.Format("The provided object cannot be serialized; [{0}] is not serializable", type)); } using (var stream = new MemoryStream()) { var buffer = new BufferedStream(stream); var formatter = new BinaryFormatter(); formatter.Serialize(buffer, o); buffer.Flush(); return stream.GetBuffer(); } }
private void button3_Click(object sender, EventArgs e) { try { string str1 = textBox1.Text; string str2 = textBox2.Text + "\\" + textBox1.Text.Substring(textBox1.Text.LastIndexOf("\\") + 1, textBox1.Text.Length - textBox1.Text.LastIndexOf("\\") - 1); Stream myStream1, myStream2; BufferedStream myBStream1, myBStream2; byte[] myByte = new byte[1024]; int i; myStream1 = File.OpenRead(str1); myStream2 = File.OpenWrite(str2); myBStream1 = new BufferedStream(myStream1); myBStream2 = new BufferedStream(myStream2); i = myBStream1.Read(myByte, 0, 1024); while (i > 0) { myBStream2.Write(myByte, 0, i); i = myBStream1.Read(myByte, 0, 1024); } myBStream2.Flush(); myStream1.Close(); myBStream2.Close(); MessageBox.Show("文件复制完成"); } catch(Exception ex) { MessageBox.Show(ex.Message); } }
public void Set(string name, string key, RavenJObject data, UuidType uuidType) { Api.JetSetCurrentIndex(session, Lists, "by_name_and_key"); Api.MakeKey(session, Lists, name, Encoding.Unicode, MakeKeyGrbit.NewKey); Api.MakeKey(session, Lists, key, Encoding.Unicode, MakeKeyGrbit.None); var exists = Api.TrySeek(session, Lists, SeekGrbit.SeekEQ); using (var update = new Update(session, Lists, exists ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["name"], name, Encoding.Unicode); Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["key"], key, Encoding.Unicode); Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["etag"], uuidGenerator.CreateSequentialUuid(uuidType).TransformToValueForEsentSorting()); Api.SetColumn(session, Lists, tableColumnsCache.ListsColumns["created_at"], SystemTime.UtcNow); using (var columnStream = new ColumnStream(session, Lists, tableColumnsCache.ListsColumns["data"])) { if (exists) columnStream.SetLength(0); using (Stream stream = new BufferedStream(columnStream)) { data.WriteTo(stream); stream.Flush(); } } update.Save(); } }
public Etag AddAttachment(string key, Etag etag, Stream data, RavenJObject headers) { Api.JetSetCurrentIndex(session, Files, "by_name"); Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ); if (isUpdate) { var existingEtag = Etag.Parse(Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"])); if (existingEtag != etag && etag != null) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag }; } } else { if (data == null) throw new InvalidOperationException("When adding new attachment, the attachment data must be specified"); if (Api.TryMoveFirst(session, Details)) Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1); } Etag newETag = uuidGenerator.CreateSequentialUuid(UuidType.Attachments); using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode); if (data != null) { long written; using (var columnStream = new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"])) { if (isUpdate) columnStream.SetLength(0); using (var stream = new BufferedStream(columnStream)) { data.CopyTo(stream); written = stream.Position; stream.Flush(); } } if (written == 0) // empty attachment { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["data"], new byte[0]); } } Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting()); Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode); update.Save(); } logger.Debug("Adding attachment {0}", key); return newETag; }
public override void Authenticate(NpgsqlConnector context, string password) { NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Authenticate"); NpgsqlPasswordPacket pwpck = new NpgsqlPasswordPacket(password, context.BackendProtocolVersion); BufferedStream stream = new BufferedStream(context.Stream); pwpck.WriteToStream(stream); stream.Flush(); }
public void Stream2Test() { var ms = new BufferedStream(new MemoryStream()); Console.WriteLine(ms.CanWrite); Console.WriteLine(ms.CanRead); ms.Write(new byte[3] { 1, 2, 3 }, 0, 3); ms.Flush(); var bys = new byte[10]; var len = ms.Read(bys, 0, 10); Console.WriteLine(len + "->" + bys[0]); }
public void Execute() { MemoryStream buffer = new MemoryStream(); formatter.Serialize(buffer, message); byte[] bytes = buffer.ToArray(); byte[] size = BitConverter.GetBytes((int)buffer.Length); Stream stream = new BufferedStream(client.GetStream()); stream.Write(size, 0, size.Length); stream.Write(bytes, 0, bytes.Length); stream.Flush(); }
public static void Decompress(Stream instream, Stream outstream) { BufferedStream stream = new BufferedStream(outstream); BufferedStream zStream = new BufferedStream(instream); if ((zStream.ReadByte() == 0x42) && (zStream.ReadByte() == 90)) { BZip2InputStream stream3 = new BZip2InputStream(zStream); for (int i = stream3.ReadByte(); i != -1; i = stream3.ReadByte()) { stream.WriteByte((byte) i); } stream.Flush(); } }
/// <summary> /// Work around a 64Mb limit when writing streams to files. /// <seealso cref="http://groups.google.co.uk/group/microsoft.public.dotnet.framework/browse_thread/thread/ba3582b0a6e45517/03e8d3c8718a9c44"/> /// </summary> public static void WriteMemoryStreamToFile(string filename, MemoryStream memory) { //// old code: //using (Stream // file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite), // fileBuffer = new BufferedStream(file) //) //{ // byte[] memoryBuffer = memory.GetBuffer(); // int memoryLength = (int)memory.Length; // fileBuffer.Write(memoryBuffer, 0, memoryLength); //##jpl: drawback: works only up to 2 gigabyte! // fileBuffer.Close(); // file.Close(); //} using (System.IO.Stream file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { using (Stream buffer = new System.IO.BufferedStream(file)) { byte[] memoryBuffer = memory.GetBuffer(); long memoryLength = memory.Length; int index = 0; // writing everything at once fails for writing memory streams larger than 64 megabyte // to a file stream on the network //buffer.Write(memoryBuffer, 0, memoryLength); // so we introduce a temporary buffer const int copyBufferSize = 65536; byte[] copyBuffer = new byte[copyBufferSize]; while (memoryLength > 0) { int actualLength; if (memoryLength > copyBufferSize) { actualLength = copyBufferSize; } else { actualLength = (int)memoryLength; //jpl: this cast is valid, as now memoryLength <= copyBufferSize } Array.Copy(memoryBuffer, index, copyBuffer, 0, actualLength); buffer.Write(copyBuffer, 0, actualLength); memoryLength = memoryLength - actualLength; index = index + actualLength; } buffer.Flush(); buffer.Close(); } } }
public void Buffered_Stream_buffers_and_forwards_writes_and_flushes_before_close() { var mock = new Mock<Stream>(); Stream stream = mock.Object; mock.SetupGet(d => d.CanRead).Returns(true); mock.SetupGet(d => d.CanWrite).Returns(true); BufferedStream bs = new BufferedStream(stream); bs.WriteByte((byte)'a'); bs.Flush(); bs.Close(); mock.Verify(d => d.Write(It.Is<byte[]>(array => array.Length > 0 && array[0] == 'a'), 0, 1)); mock.Verify(d => d.Flush()); mock.Verify(d => d.Close()); }
/// <summary> /// BufferedStream使用装饰模式 /// 继承Stream基类,提供同样的接口 /// 可以封装其他继承自Stream的类,提供缓存的功能 /// </summary> static void Case1() { string filePath = @"test.txt"; if (File.Exists(filePath)) File.Delete(filePath); using(FileStream fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) { using (BufferedStream bs = new BufferedStream(fs)) { Output(bs, "I am Simon.\r\n"); Output(bs, "How are you?"); bs.Flush(); } } }
/// <summary> /// 테스트용 임시 파일을 생성한다. /// </summary> public FileInfo CreateTestFile(string filepath, long size) { var fi = new FileInfo(filepath); var buffer = new byte[BufferSize]; ArrayTool.GetRandomBytes(buffer); using(var bs = new BufferedStream(fi.OpenWrite())) { long writeCount = 0; do { bs.Write(buffer, 0, BufferSize); writeCount += BufferSize; } while(size > writeCount); bs.Flush(); } return fi; }
public Guid AddAttachment(string key, Guid? etag, Stream data, RavenJObject headers) { Api.JetSetCurrentIndex(session, Files, "by_name"); Api.MakeKey(session, Files, key, Encoding.Unicode, MakeKeyGrbit.NewKey); var isUpdate = Api.TrySeek(session, Files, SeekGrbit.SeekEQ); if (isUpdate) { var existingEtag = Api.RetrieveColumn(session, Files, tableColumnsCache.FilesColumns["etag"]).TransfromToGuidWithProperSorting(); if (existingEtag != etag && etag != null) { throw new ConcurrencyException("PUT attempted on attachment '" + key + "' using a non current etag") { ActualETag = existingEtag, ExpectedETag = etag.Value }; } } else { if (Api.TryMoveFirst(session, Details)) Api.EscrowUpdate(session, Details, tableColumnsCache.DetailsColumns["attachment_count"], 1); } Guid newETag = uuidGenerator.CreateSequentialUuid(); using (var update = new Update(session, Files, isUpdate ? JET_prep.Replace : JET_prep.Insert)) { Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["name"], key, Encoding.Unicode); using (var stream = new BufferedStream(new ColumnStream(session, Files, tableColumnsCache.FilesColumns["data"]))) { data.CopyTo(stream); stream.Flush(); } Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["etag"], newETag.TransformToValueForEsentSorting()); Api.SetColumn(session, Files, tableColumnsCache.FilesColumns["metadata"], headers.ToString(Formatting.None), Encoding.Unicode); update.Save(); } logger.Debug("Adding attachment {0}", key); return newETag; }
public override void Query( NpgsqlConnector context, NpgsqlCommand command ) { NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Query"); //String commandText = command.GetCommandText(); //NpgsqlEventLog.LogMsg(resman, "Log_QuerySent", LogLevel.Debug, commandText); // Send the query request to backend. NpgsqlQuery query = new NpgsqlQuery(command, context.BackendProtocolVersion); BufferedStream stream = new BufferedStream(context.Stream); query.WriteToStream(stream, context.Encoding); stream.Flush(); ProcessBackendResponses(context); }
static void FileStreamAndBufferedStream() { System.IO.Stream fileStream = System.IO.File.Open("./file.txt", System.IO.FileMode.Open); System.IO.BufferedStream bufferedStream = new System.IO.BufferedStream(fileStream); System.Console.WriteLine("CanRead: " + bufferedStream.CanRead); System.Console.WriteLine("CanWrite: " + bufferedStream.CanWrite); System.Console.WriteLine("CanSeek: " + bufferedStream.CanSeek); System.Console.WriteLine("Length: " + bufferedStream.Length); System.Console.WriteLine("Position: " + bufferedStream.Position); int firstByte = bufferedStream.ReadByte(); System.Console.WriteLine("Byte:" + firstByte); bufferedStream.Position = 0; System.Console.WriteLine("Position: " + bufferedStream.Position); bufferedStream.WriteByte((byte)(firstByte + 1)); bufferedStream.Flush(); //bufferedStream.Position = 0; //firstByte = bufferedStream.ReadByte(); //bufferedStream.Dispose(); System.Console.WriteLine("Byte:" + firstByte); }
public static void WriteDataToRequest(HttpWebRequest req, string data, bool disableCompression) { req.SendChunked = true; // we want to make sure that we use a buffer properly here so we won't send the data // in many different TCP packets using (var requestStream = new BufferedStream(req.GetRequestStream())) using (var dataStream = new GZipStream(requestStream, CompressionMode.Compress)) using (var writer = disableCompression == false ? new StreamWriter(dataStream, Encoding.UTF8) : new StreamWriter(requestStream, Encoding.UTF8)) { writer.Write(data); writer.Flush(); if (disableCompression == false) dataStream.Flush(); requestStream.Flush(); } }
public static PTable FromMemtable(IMemTable table, string filename, int cacheDepth = 16) { Ensure.NotNull(table, "table"); Ensure.NotNullOrEmpty(filename, "filename"); Ensure.Nonnegative(cacheDepth, "cacheDepth"); //Log.Trace("Started dumping MemTable [{0}] into PTable...", table.Id); var sw = Stopwatch.StartNew(); using (var fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None, DefaultSequentialBufferSize, FileOptions.SequentialScan)) { fs.SetLength(PTableHeader.Size + IndexEntrySize * (long)table.Count + MD5Size); // EXACT SIZE fs.Seek(0, SeekOrigin.Begin); using (var md5 = MD5.Create()) using (var cs = new CryptoStream(fs, md5, CryptoStreamMode.Write)) using (var bs = new BufferedStream(cs, DefaultSequentialBufferSize)) { // WRITE HEADER var headerBytes = new PTableHeader(Version).AsByteArray(); cs.Write(headerBytes, 0, headerBytes.Length); // WRITE INDEX ENTRIES var buffer = new byte[IndexEntrySize]; foreach (var record in table.IterateAllInOrder()) { var rec = record; AppendRecordTo(bs, rec.Bytes, buffer); } bs.Flush(); cs.FlushFinalBlock(); // WRITE MD5 var hash = md5.Hash; fs.Write(hash, 0, hash.Length); } } Log.Trace("Dumped MemTable [{0}, {1} entries] in {2}.", table.Id, table.Count, sw.Elapsed); return new PTable(filename, table.Id, depth: cacheDepth); }
private void StreamToClient(Stream stream, ExportOptions options, Lazy<NameValueCollection> headers, IPrincipal user) { var old = CurrentOperationContext.Headers.Value; var oldUser = CurrentOperationContext.User.Value; try { CurrentOperationContext.Headers.Value = headers; CurrentOperationContext.User.Value = user; Database.TransactionalStorage.Batch(accessor => { var bufferStream = new BufferedStream(stream, 1024 * 64); using (var cts = new CancellationTokenSource()) using (var timeout = cts.TimeoutAfter(DatabasesLandlord.SystemConfiguration.DatabaseOperationTimeout)) using (var streamWriter = new StreamWriter(bufferStream)) using (var writer = new JsonTextWriter(streamWriter)) { writer.WriteStartObject(); writer.WritePropertyName("Results"); writer.WriteStartArray(); var exporter = new SmugglerExporter(Database, options); exporter.Export(item => WriteToStream(writer, item, timeout), cts.Token); writer.WriteEndArray(); writer.WriteEndObject(); writer.Flush(); bufferStream.Flush(); } }); } finally { CurrentOperationContext.Headers.Value = old; CurrentOperationContext.User.Value = oldUser; } }
public static void CreatePTableFile(string filename, long ptableSize, int indexEntrySize, int cacheDepth = 16) { Ensure.NotNullOrEmpty(filename, "filename"); Ensure.Nonnegative(cacheDepth, "cacheDepth"); var sw = Stopwatch.StartNew(); var tableId = Guid.NewGuid(); using (var fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None, DefaultSequentialBufferSize, FileOptions.SequentialScan)) { fs.SetLength((long)ptableSize); fs.Seek(0, SeekOrigin.Begin); var recordCount = (long)((ptableSize - PTableHeader.Size - PTable.MD5Size) / (long)indexEntrySize); using (var md5 = MD5.Create()) using (var cs = new CryptoStream(fs, md5, CryptoStreamMode.Write)) using (var bs = new BufferedStream(cs, DefaultSequentialBufferSize)) { // WRITE HEADER var headerBytes = new PTableHeader(Version).AsByteArray(); cs.Write(headerBytes, 0, headerBytes.Length); // WRITE INDEX ENTRIES var buffer = new byte[indexEntrySize]; for (long i = 0; i < recordCount; i++) { bs.Write(buffer, 0, indexEntrySize); } bs.Flush(); cs.FlushFinalBlock(); // WRITE MD5 var hash = md5.Hash; fs.Write(hash, 0, hash.Length); } } Console.WriteLine("Created PTable File[{0}, size of {1}] in {2}.", tableId, ptableSize, sw.Elapsed); }
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data, byte[] viewAndReduceKeyHashed) { Guid etag = uuidGenerator.CreateSequentialUuid(); using (var update = new Update(session, MappedResults, JET_prep.Insert)) { Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["view"], view, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["document_key"], docId, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key"], reduceKey, Encoding.Unicode); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["reduce_key_and_view_hashed"], viewAndReduceKeyHashed); using (var stream = new BufferedStream(new ColumnStream(session, MappedResults, tableColumnsCache.MappedResultsColumns["data"]))) { data.WriteTo(stream); stream.Flush(); } Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["etag"], etag.TransformToValueForEsentSorting()); Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["timestamp"], SystemTime.Now); update.Save(); } }
public static void GetObjectPartly(string bucketName) { client.PutObject(bucketName, key, Config.BigFileToUpload); string localFilePath = dirToDownload + "/sample.3.data"; using (var fileStream = new FileStream(localFilePath, FileMode.OpenOrCreate)) { var bufferedStream = new BufferedStream(fileStream); var objectMetadata = client.GetObjectMetadata(bucketName, key); var fileLength = objectMetadata.ContentLength; const int partSize = 1024 * 1024 * 10; var partCount = CalPartCount(fileLength, partSize); for (var i = 0; i < partCount; i++) { var startPos = partSize * i; var endPos = partSize * i + (partSize < (fileLength - startPos) ? partSize : (fileLength - startPos)) - 1; Download(bufferedStream, startPos, endPos, localFilePath, bucketName, key); } bufferedStream.Flush(); } }
/// <summary> /// Reads period terminated data from source stream. Reads data while gets single period on line, /// what is data terminator. /// </summary> /// <param name="storeStream">Stream where to store readed data.</param> /// <param name="maxSize">Maximum number of bytes to read.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum size exceeded.</param> /// <returns>Returns number of bytes written to <b>storeStream</b>.</returns> /// <exception cref="ArgumentNullException">Raised when <b>storeStream</b> is null.</exception> /// <exception cref="ArgumentException">Raised when <b>maxSize</b> less than 1.</exception> /// <exception cref="InvalidOperationException">Raised when there already is pending read operation.</exception> /// <exception cref="LineSizeExceededException">Raised when maximum allowed line size has exceeded.</exception> /// <exception cref="DataSizeExceededException">Raised when maximum allowed data size has exceeded.</exception> /// <exception cref="IncompleteDataException">Raised when source stream was reached end of stream and data is not period terminated.</exception> public int ReadPeriodTerminated(Stream storeStream,int maxSize,SizeExceededAction exceededAction) { if(storeStream == null){ throw new ArgumentNullException("storeStream"); } lock(this){ if(m_IsReadActive){ throw new InvalidOperationException("There is pending read operation, multiple read operations not allowed !"); } else{ m_IsReadActive = true; } } try{ BufferedStream bufferedStoreStream = new BufferedStream(storeStream,32000); bool lineSizeExceeded = false; bool isPeriodTerminated = false; int totalReadedCount = 0; int readedCount = 0; int rawReadedCount = 0; // Just break reading at once if maximum allowed line or data size exceeded. if(exceededAction == SizeExceededAction.ThrowException){ try{ // Read first line. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.JunkAndThrowException,out rawReadedCount,false); } catch(LineSizeExceededException x){ string dummy = x.Message; lineSizeExceeded = true; } while(rawReadedCount != 0){ totalReadedCount += rawReadedCount; // We have data terminator "<CRLF>.<CRLF>". if(readedCount == 1 && m_pLineBuffer[0] == '.'){ isPeriodTerminated = true; break; } // If line starts with period(.), first period is removed. else if(m_pLineBuffer[0] == '.'){ // Maximum allowed line or data size exceeded. if(lineSizeExceeded || totalReadedCount > maxSize){ // Junk data } // Write readed line to store stream. else{ bufferedStoreStream.Write(m_pLineBuffer,1,readedCount - 1); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); } } // Normal line. else{ // Maximum allowed line or data size exceeded. if(lineSizeExceeded || totalReadedCount > maxSize){ // Junk data } // Write readed line to store stream. else{ bufferedStoreStream.Write(m_pLineBuffer,0,readedCount); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); } } try{ // Read next line. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.JunkAndThrowException,out rawReadedCount,false); } catch(LineSizeExceededException x){ string dummy = x.Message; lineSizeExceeded = true; } } } // Read and junk all data if maximum allowed line or data size exceeded. else{ // Read first line. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.JunkAndThrowException,out rawReadedCount,false); while(rawReadedCount != 0){ totalReadedCount += rawReadedCount; // We have data terminator "<CRLF>.<CRLF>". if(readedCount == 1 && m_pLineBuffer[0] == '.'){ isPeriodTerminated = true; break; } // If line starts with period(.), first period is removed. else if(m_pLineBuffer[0] == '.'){ // Maximum allowed size exceeded. if(totalReadedCount > maxSize){ throw new DataSizeExceededException(); } // Write readed line to store stream. bufferedStoreStream.Write(m_pLineBuffer,1,readedCount - 1); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); } // Normal line. else{ // Maximum allowed size exceeded. if(totalReadedCount > maxSize){ throw new DataSizeExceededException(); } // Write readed line to store stream. bufferedStoreStream.Write(m_pLineBuffer,0,readedCount); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); } // Read next line. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.JunkAndThrowException,out rawReadedCount,false); } } bufferedStoreStream.Flush(); // Log if(this.Logger != null){ this.Logger.AddRead(totalReadedCount,null); } if(lineSizeExceeded){ throw new LineSizeExceededException(); } if(!isPeriodTerminated){ throw new IncompleteDataException("Source stream was reached end of stream and data is not period terminated !"); } if(totalReadedCount > maxSize){ throw new DataSizeExceededException(); } return totalReadedCount; } finally{ m_IsReadActive = false; } }
/// <summary> /// Reads header from source stream and stores to the specified stream. Reads header data while /// gets blank line, what is header terminator. For example this method can be used for reading /// mail,http,sip, ... headers. /// </summary> /// <param name="storeStream">Stream where to store readed data.</param> /// <param name="maxSize">Maximum number of bytes to read.</param> /// <param name="exceededAction">Specifies how this method behaves when maximum line or data size exceeded.</param> /// <returns>Returns number of bytes written to <b>storeStream</b>.</returns> /// <exception cref="ArgumentNullException">Raised when <b>storeStream</b> is null.</exception> /// <exception cref="ArgumentException">Raised when <b>maxSize</b> less than 1.</exception> /// <exception cref="InvalidOperationException">Raised when there already is pending read operation.</exception> /// <exception cref="LineSizeExceededException">Raised when maximum allowed line size has exceeded.</exception> /// <exception cref="DataSizeExceededException">Raised when maximum allowed data size has exceeded.</exception> public int ReadHeader(Stream storeStream,int maxSize,SizeExceededAction exceededAction) { if(storeStream == null){ throw new ArgumentNullException("storeStream"); } lock(this){ if(m_IsReadActive){ throw new InvalidOperationException("There is pending read operation, multiple read operations not allowed !"); } else{ m_IsReadActive = true; } } try{ BufferedStream bufferedStoreStream = new BufferedStream(storeStream,32000); bool lineSizeExceeded = false; int totalReadedCount = 0; int readedCount = 0; int rawReadedCount = 0; while(true){ // Read line. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.ThrowException,out rawReadedCount,false); // We have reached end of stream, no more data. if(rawReadedCount == 0){ break; } totalReadedCount += rawReadedCount; // We got header terminator. if(readedCount == 0){ break; } else{ // Maximum allowed data size exceeded. if(totalReadedCount > maxSize){ if(exceededAction == SizeExceededAction.ThrowException){ throw new DataSizeExceededException(); } } // Write readed bytes to store stream. else{ bufferedStoreStream.Write(m_pLineBuffer,0,readedCount); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); } } } bufferedStoreStream.Flush(); // Maximum allowed line size exceeded, some data is junked. if(lineSizeExceeded){ throw new LineSizeExceededException(); } // Maximum allowed data size exceeded, some data is junked. if(totalReadedCount > maxSize){ throw new DataSizeExceededException(); } // Log if(this.Logger != null){ this.Logger.AddRead(totalReadedCount,null); } return totalReadedCount; } finally{ m_IsReadActive = false; } }
/// <summary> /// Reades all data from the specified stream and writes it to source stream. Period handlign and period terminator is added as required. /// </summary> /// <param name="stream">Stream which data to write to source stream.</param> /// <param name="maxSize">Maximum muber of bytes to read from <b>stream</b> and write source stream.</param> /// <returns>Returns number of bytes written to source stream. Note this value differs from /// <b>stream</b> readed bytes count because of period handling and period terminator. /// </returns> /// <exception cref="ArgumentNullException">Raised when <b>stream</b> is null.</exception> /// <exception cref="InvalidOperationException">Raised when there already is pending write operation.</exception> /// <exception cref="LineSizeExceededException">Raised when <b>stream</b> contains line with bigger line size than allowed.</exception> /// <exception cref="DataSizeExceededException">Raised when <b>stream</b> has more data than <b>maxSize</b> allows..</exception> public int WritePeriodTerminated(Stream stream,int maxSize) { if(stream == null){ throw new ArgumentNullException("stream"); } lock(this){ if(m_IsWriteActive){ throw new InvalidOperationException("There is pending write operation, multiple write operations not allowed !"); } m_IsWriteActive = true; } try{ BufferedStream bufferedStoreStream = new BufferedStream(m_pStream,32000); StreamHelper reader = new StreamHelper(stream); int totalWrittenCount = 0; int readedCount = 0; int rawReadedCount = 0; while(true){ // Read data block. readedCount = this.ReadLineInternal(m_pLineBuffer,SizeExceededAction.ThrowException,out rawReadedCount,false); // We reached end of stream, no more data. if(readedCount == 0){ break; } // Maximum allowed data size exceeded. if((totalWrittenCount + rawReadedCount) > maxSize){ throw new DataSizeExceededException(); } // If line starts with period(.), additional period is added. if(m_pLineBuffer[0] == '.'){ bufferedStoreStream.WriteByte((byte)'.'); totalWrittenCount++; } // Write readed line to buffered stream. bufferedStoreStream.Write(m_pLineBuffer,0,readedCount); bufferedStoreStream.Write(m_LineBreak,0,m_LineBreak.Length); totalWrittenCount += (readedCount + m_LineBreak.Length); } // Write terminator ".<CRLF>". We have start <CRLF> already in stream. bufferedStoreStream.Write(new byte[]{(byte)'.',(byte)'\r',(byte)'\n'},0,3); bufferedStoreStream.Flush(); m_pStream.Flush(); // Log if(this.Logger != null){ this.Logger.AddWrite(totalWrittenCount,null); } return totalWrittenCount; } finally{ m_IsWriteActive = false; } }
private Response TcpRequest(Request request) { //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //sw.Start(); byte[] responseMessage = new byte[512]; for (int intAttempts = 0; intAttempts < m_Retries; intAttempts++) { for (int intDnsServer = 0; intDnsServer < m_DnsServers.Count; intDnsServer++) { using( TcpClient tcpClient = new TcpClient() ) { tcpClient.ReceiveTimeout = m_Timeout * 1000; try { IAsyncResult result = tcpClient.BeginConnect(m_DnsServers[intDnsServer].Address, m_DnsServers[intDnsServer].Port, null, null); bool success = result.AsyncWaitHandle.WaitOne(m_Timeout*1000, true); if (!success || !tcpClient.Connected) { Verbose(string.Format(";; Connection to nameserver {0} failed", (intDnsServer + 1))); continue; } BufferedStream bs = new BufferedStream(tcpClient.GetStream()); byte[] data = request.Data; bs.WriteByte((byte)((data.Length >> 8) & 0xff)); bs.WriteByte((byte)(data.Length & 0xff)); bs.Write(data, 0, data.Length); bs.Flush(); Response TransferResponse = new Response(); int intSoa = 0; int intMessageSize = 0; //Debug.WriteLine("Sending "+ (request.Length+2) + " bytes in "+ sw.ElapsedMilliseconds+" mS"); while (true) { int intLength = bs.ReadByte() << 8 | bs.ReadByte(); if (intLength <= 0) { Verbose(string.Format(";; Connection to nameserver {0} failed", (intDnsServer + 1))); throw new SocketException(); // next try } intMessageSize += intLength; data = new byte[intLength]; bs.Read(data, 0, intLength); Response response = new Response(m_DnsServers[intDnsServer], data); //Debug.WriteLine("Received "+ (intLength+2)+" bytes in "+sw.ElapsedMilliseconds +" mS"); if (response.header.RCODE != RCode.NoError) return response; if (response.Questions[0].QType != QType.AXFR) { AddToCache(response); return response; } // Zone transfer!! if(TransferResponse.Questions.Count==0) TransferResponse.Questions.AddRange(response.Questions); TransferResponse.Answers.AddRange(response.Answers); TransferResponse.Authorities.AddRange(response.Authorities); TransferResponse.Additionals.AddRange(response.Additionals); if (response.Answers[0].Type == Type.SOA) intSoa++; if (intSoa == 2) { TransferResponse.header.QDCOUNT = (ushort)TransferResponse.Questions.Count; TransferResponse.header.ANCOUNT = (ushort)TransferResponse.Answers.Count; TransferResponse.header.NSCOUNT = (ushort)TransferResponse.Authorities.Count; TransferResponse.header.ARCOUNT = (ushort)TransferResponse.Additionals.Count; TransferResponse.MessageSize = intMessageSize; return TransferResponse; } } } // try catch (SocketException) { continue; // next try } finally { m_Unique++; } } } } Response responseTimeout = new Response(); responseTimeout.Error = "Timeout Error"; return responseTimeout; }
public void Open() { try { mySerialPort.Open(); baseStream = new BufferedStream(mySerialPort.BaseStream, 1024); Encoding enc = new ASCIIEncoding(); writer = new StreamWriter(baseStream, enc); reader = new BinaryReader(baseStream); baseStream.WriteByte((byte)0x19); //ctrl-Y --> perform reset of Patriot baseStream.WriteByte((byte)0x0D); baseStream.WriteByte((byte)0x0A); baseStream.Flush(); // Thread.Sleep(12000); //wait for completion Head h = frames[0]; frames.Remove(h); h = IssueCommand("F1"); //set binary mode if (h != null && h.ErrorIndicator != (byte)0) throw new Exception("Unable to set binary mode: " + h.ExtractErrorMessage()); h = IssueCommand("U1"); //set metric scale (cm) if (h != null && h.ErrorIndicator != (byte)0) throw new Exception("Unable to set metric units: " + h.ExtractErrorMessage()); h = IssueCommand("O*,2"); //position data only reader.ReadBytes(h.ResponseSize); //skip returned h = IssueCommand("L1,1"); //set button on stylus to work if (h != null && h.ErrorIndicator != (byte)0) throw new Exception("Unable to set stylus button mode: " + h.ExtractErrorMessage()); } catch (Exception e) { throw new Exception("Patriot.Open: " + e.Message); } }
/// <summary> /// Write an entry to the archive. This method will call the putNextEntry /// and then write the contents of the entry, and finally call closeEntry()() /// for entries that are files. For directories, it will call putNextEntry(), /// and then, if the recurse flag is true, process each entry that is a /// child of the directory. /// </summary> /// <param name="entry"> /// The TarEntry representing the entry to write to the archive. /// </param> /// <param name="recurse"> /// If true, process the children of directory entries. /// </param> public void WriteEntry(TarEntry entry, bool recurse) { bool asciiTrans = false; string tempFileName = null; string eFile = entry.File; // Work on a copy of the entry so we can manipulate it. // Note that we must distinguish how the entry was constructed. // if (eFile == null || eFile.Length == 0) { entry = TarEntry.CreateTarEntry(entry.Name); } else { // // The user may have explicitly set the entry's name to // something other than the file's path, so we must save // and restore it. This should work even when the name // was set from the File's name. // string saveName = entry.Name; entry = TarEntry.CreateEntryFromFile(eFile); entry.Name = saveName; } if (this.verbose) { OnProgressMessageEvent(entry, null); } if (this.asciiTranslate && !entry.IsDirectory) { asciiTrans = !IsBinary(eFile); // original java source : // MimeType mime = null; // string contentType = null; // // try { // contentType = FileTypeMap.getDefaultFileTypeMap(). getContentType( eFile ); // // mime = new MimeType( contentType ); // // if ( mime.getPrimaryType(). // equalsIgnoreCase( "text" ) ) // { // asciiTrans = true; // } // else if ( this.transTyper != null ) // { // if ( this.transTyper.isAsciiFile( eFile ) ) // { // asciiTrans = true; // } // } // } catch ( MimeTypeParseException ex ) // { // // IGNORE THIS ERROR... // } // // if (this.debug) { // Console.Error.WriteLine("CREATE TRANS? '" + asciiTrans + "' ContentType='" + contentType + "' PrimaryType='" + mime.getPrimaryType()+ "'" ); // } if (asciiTrans) { tempFileName = Path.GetTempFileName(); StreamReader inStream = File.OpenText(eFile); Stream outStream = new BufferedStream(File.Create(tempFileName)); while (true) { string line = inStream.ReadLine(); if (line == null) { break; } byte[] data = Encoding.ASCII.GetBytes(line); outStream.Write(data, 0, data.Length); outStream.WriteByte((byte)'\n'); } inStream.Close(); outStream.Flush(); outStream.Close(); entry.Size = new FileInfo(tempFileName).Length; eFile = tempFileName; } } string newName = null; if (this.rootPath != null) { if (entry.Name.StartsWith(this.rootPath)) { newName = entry.Name.Substring(this.rootPath.Length + 1 ); } } if (this.pathPrefix != null) { newName = (newName == null) ? this.pathPrefix + "/" + entry.Name : this.pathPrefix + "/" + newName; } if (newName != null) { entry.Name = newName; } this.tarOut.PutNextEntry(entry); if (entry.IsDirectory) { if (recurse) { TarEntry[] list = entry.GetDirectoryEntries(); for (int i = 0; i < list.Length; ++i) { this.WriteEntry(list[i], recurse); } } } else { Stream inputStream = File.OpenRead(eFile); int numWritten = 0; byte[] eBuf = new byte[32 * 1024]; while (true) { int numRead = inputStream.Read(eBuf, 0, eBuf.Length); if (numRead <=0) { break; } this.tarOut.Write(eBuf, 0, numRead); numWritten += numRead; } // Console.WriteLine("written " + numWritten + " bytes"); inputStream.Close(); if (tempFileName != null && tempFileName.Length > 0) { File.Delete(tempFileName); } this.tarOut.CloseEntry(); } }
public void With_WebSocket_FailsWithDoubleMessageAwait() { var handshake = GenerateSimpleHandshake(); using (var ms = new BufferedStream(new MemoryStream())) using (WebSocket ws = new WebSocketRfc6455(ms, new WebSocketListenerOptions() { PingTimeout = Timeout.InfiniteTimeSpan }, new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2), handshake.Request, handshake.Response, handshake.NegotiatedMessageExtensions)) { ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8); ms.Write(new Byte[] { 129, 130, 75, 91, 80, 26, 3, 50 }, 0, 8); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); ws.ReadMessage(); ws.ReadMessage(); } }
public void flush() { _stream.Flush(); }