本文整理汇总了C#中System.IO.BufferedStream类的典型用法代码示例。如果您正苦于以下问题:C# BufferedStream类的具体用法?C# BufferedStream怎么用?C# BufferedStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BufferedStream类属于System.IO命名空间,在下文中一共展示了BufferedStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
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();
}
}
示例2: ShowUsage
public override void ShowUsage()
{
//BufferedStream类主要也是用来处理流数据的,但是该类主要的功能是用来封装其他流类。
//为什么要封装其他流类,这么做的意义是什么?按照微软的话说主要是减少某些流直接操作存储设备的时间。
//对于一些流来说直接向磁盘中存储数据这种做法的效率并不高,用BufferedStream包装过的流,先在内存中进行统一的处理再向磁盘中写入数据,也会提高写入的效率。
Console.WriteLine("BufferedStream类主要也是用来处理流数据的,但是该类主要的功能是用来封装其他流类。");
FileStream fileStream1 = File.Open(@"C:\NewText.txt", FileMode.OpenOrCreate, FileAccess.Read); //读取文件流
FileStream fileStream2 = File.Open(@"C:\Text2.txt", FileMode.OpenOrCreate, FileAccess.Write); //写入文件流
byte[] array4 = new byte[4096];
BufferedStream bufferedInput = new BufferedStream(fileStream1); //封装文件流
BufferedStream bufferedOutput = new BufferedStream(fileStream2); //封装文件流
int byteRead = bufferedInput.Read(array4, 0, array4.Length);
bufferedOutput.Write(array4, 0, array4.Length);
//= bufferedInput.Read(array4, 0, 4096);
while (byteRead > 0) //读取到了数据
{
bufferedOutput.Write(array4, 0, byteRead);
Console.WriteLine(byteRead);
break;
};
bufferedInput.Close();
bufferedOutput.Close();
fileStream1.Close();
fileStream2.Close();
Console.ReadKey();
}
示例3: PutMappedResult
public void PutMappedResult(string view, string docId, string reduceKey, RavenJObject data)
{
Etag etag = uuidGenerator.CreateSequentialUuid(UuidType.MappedResults);
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["hashed_reduce_key"], HashReduceKey(reduceKey));
var mapBucket = IndexingUtil.MapBucket(docId);
Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["bucket"], mapBucket);
using (Stream stream = new BufferedStream(new ColumnStream(session, MappedResults, tableColumnsCache.MappedResultsColumns["data"])))
{
using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
{
data.WriteTo(dataStream);
dataStream.Flush();
}
}
Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["etag"], etag.TransformToValueForEsentSorting());
Api.SetColumn(session, MappedResults, tableColumnsCache.MappedResultsColumns["timestamp"], SystemTime.UtcNow.ToBinary());
update.Save();
}
}
示例4: SerializeToStreamAsync
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
using (var uncloseableStream = new UndisposableStream(stream))
using (var bufferedStream = new BufferedStream(uncloseableStream))
{
Stream compressedStream = null;
if (encodingType == "gzip")
{
compressedStream = new GZipStream(bufferedStream, CompressionMode.Compress, leaveOpen: true);
}
else if (encodingType == "deflate")
{
compressedStream = new DeflateStream(bufferedStream, CompressionMode.Compress, leaveOpen: true);
}
else throw new InvalidOperationException("This shouldn't happen, ever.");
await originalContent.CopyToAsync(compressedStream);
if (compressedStream != null)
{
compressedStream.Dispose();
}
}
}
示例5: GetData
private static IList<DateTime> GetData()
{
IList<DateTime> dates = new List<DateTime>();
var cultureInfo = new CultureInfo("en-US");
using (var fs = File.Open(DataFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var bs = new BufferedStream(fs, 262144))
using (var sr = new StreamReader(bs))
{
while (true)
{
string line = sr.ReadLine();
if (line == null) break;
try
{
dates.Add(DateTime.Parse(line, cultureInfo));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
return dates;
}
示例6: Set
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();
}
}
示例7: PooledSocket
public PooledSocket(IPEndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout)
{
var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
// all operations are "atomic", we do not send small chunks of data
socket.NoDelay = true;
var mre = new ManualResetEvent(false);
var timeout = connectionTimeout == TimeSpan.MaxValue
? Timeout.Infinite
: (int)connectionTimeout.TotalMilliseconds;
socket.ReceiveTimeout = (int)receiveTimeout.TotalMilliseconds;
socket.SendTimeout = (int)receiveTimeout.TotalMilliseconds;
socket.BeginConnect(endpoint, iar =>
{
try { using (iar.AsyncWaitHandle) socket.EndConnect(iar); }
catch { }
mre.Set();
}, null);
if (!mre.WaitOne(timeout) || !socket.Connected)
{
using (socket)
throw new TimeoutException("Could not connect to " + endpoint);
}
this.socket = socket;
this.endpoint = endpoint;
this.inputStream = new BufferedStream(new BasicNetworkStream(socket));
}
示例8: Build
public void Build(string filename) {
positions = new List<long>();
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
using (BufferedStream bs = new BufferedStream(fs)) {
bool detectFirstLine = true;
int b = 0;
while ((b = bs.ReadByte()) != -1) {
if (b == '\n') {
positions.Add(bs.Position);
detectFirstLine = false;
}
if (detectFirstLine) {
if (bs.Position == 1 && b != 0xEF) {
positions.Add(0);
detectFirstLine = false;
}
if (bs.Position == 2 && b != 0xBB) {
positions.Add(0);
detectFirstLine = false;
}
if (bs.Position == 3 && b != 0xBF) {
positions.Add(0);
detectFirstLine = false;
}
if (bs.Position == 4 && detectFirstLine) {
positions.Add(3);
detectFirstLine = false;
}
}
}
}
}
}
示例9: PutReducedResult
public void PutReducedResult(string view, string reduceKey, int level, int sourceBucket, int bucket, RavenJObject data)
{
Guid etag = uuidGenerator.CreateSequentialUuid();
using (var update = new Update(session, ReducedResults, JET_prep.Insert))
{
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["view"], view, Encoding.Unicode);
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["level"], level);
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["reduce_key"], reduceKey, Encoding.Unicode);
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["hashed_reduce_key"], HashReduceKey(reduceKey));
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["bucket"], bucket);
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["source_bucket"], sourceBucket);
using (Stream stream = new BufferedStream(new ColumnStream(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["data"])))
{
using (var dataStream = documentCodecs.Aggregate(stream, (ds, codec) => codec.Value.Encode(reduceKey, data, null, ds)))
{
data.WriteTo(dataStream);
dataStream.Flush();
}
}
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["etag"], etag.TransformToValueForEsentSorting());
Api.SetColumn(session, ReducedResults, tableColumnsCache.ReduceResultsColumns["timestamp"], SystemTime.UtcNow);
update.Save();
}
}
示例10: AddAttachment
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;
}
示例11: connect
public bool connect()
{
client = new TcpClient(APIConstants.DEFAULT_SERVER,APIConstants.DEFAULT_TCP_PORT);
client.NoDelay = true;
start = DateTime.Now;
st = new BufferedStream(client.GetStream(),8000);
Envelope ping = new Envelope() { ping = new PingMsg() { nonce = new Random().Next(int.MaxValue) } , type = Envelope.MsgCode.PingMsgCode };
DateTime deadline = DateTime.Now.AddMilliseconds(3000);
while (DateTime.Now < deadline)
{
send(ping);
System.Threading.Thread.Sleep(500);
var pingresp = receive(0);
if (pingresp != null && pingresp.pingResponse != null)
return true;
}
throw new IOException("No Responce");
}
示例12: EbcdicTestsTestReader
public void EbcdicTestsTestReader()
{
FileFormat fileFormat = CopybookLoader.LoadCopybook(Copybook + "/Test.fileformat");
using (BufferedStream inputStream = new BufferedStream(new MemoryStream(Bytes)))
{
EbcdicReader reader = new EbcdicReader(inputStream, fileFormat, false);
List<object> record = reader.NextRecord();
Assert.AreEqual(Objects.Length, record.ToArray().Length);
Assert.AreEqual(Objects.GetType(), record.ToArray().GetType());
//NOTE : CollectionAssert does not support nested collection handling ...
int index = 0;
foreach (var rec in record.ToArray())
{
var array = rec as Array;
if (array != null)
{
CollectionAssert.AreEqual((Array)Objects[index], array);
}
else
{
Assert.AreEqual(Objects[index], rec);
}
index++;
}
}
}
示例13: GetXmlFromStream
public static XDocument GetXmlFromStream(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.ServicePoint.Expect100Continue = false;
request.Proxy = null;
var response = request.GetResponse();
var xml = string.Empty;
using (var stream = response.GetResponseStream())
{
using (var buffer = new BufferedStream(stream))
{
using (var reader = new StreamReader(buffer, Encoding.UTF8))
{
xml = reader.ReadToEnd();
}
}
}
//The Star-Wars-Beatles-PeerGynt-bug
//Aleph XML-repsonses somtimes returns Unicode SOH instead of SP
//a few places in the document.
//This causes the parsing to fail..
const char soh ='\u0001';
const char sp = '\u0020';
var xmlEscaped = xml.Replace(soh, sp);
return XDocument.Parse(xmlEscaped);
}
示例14: init
public bool init(TcpClient client)
{
if (client == null) {
return false;
}
_tcpClient = client;
try {
Stream stream = _tcpClient.GetStream();
// !! DO NOT instantiate the LineReaderInputStream with a BufferedStream
// this can cause very odd behaviour
_inputReader = new LineReaderInputStream(stream);
BufferedStream buffStream = new BufferedStream(stream);
_outputWriter = new StreamWriter(buffStream);
return true;
}
catch (SocketException ex) {
// close the socket if an exception occurs getting its stream
closeConnection();
if (_debug) {
Console.WriteLine(ex.ToString());
}
}
return false;
}
示例15: BitReader
public BitReader(byte[] bytes)
{
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
s = new BufferedStream(ms);
}