本文整理汇总了C#中System.IO.Stream.FlushAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.FlushAsync方法的具体用法?C# Stream.FlushAsync怎么用?C# Stream.FlushAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Stream
的用法示例。
在下文中一共展示了Stream.FlushAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyStreamsAsyncParallel
public static async Task<long> CopyStreamsAsyncParallel(Stream src, Stream dst)
{
long numCopied = 0;
byte[][] buffer = new byte[2][];
buffer[0] = new byte[0x1000];
buffer[1] = new byte[0x1000];
var index = 0;
int numRead = await src.ReadAsync(buffer[index], 0, buffer[index].Length);
while (numRead > 0)
{
var writeAsync = dst.WriteAsync(buffer[index], 0, numRead);
index = index ^= 1;
var readAsync = src.ReadAsync(buffer[index], 0, buffer[index].Length);
Task.WaitAll(writeAsync, readAsync);
numRead = readAsync.Result;
numCopied += numRead;
}
await dst.FlushAsync();
src.Dispose();
dst.Dispose();
return numCopied;
}
示例2: SaveFile
public async Task SaveFile(string name, Stream stream)
{
var fullPath = GetFullPath(name);
using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write))
{
await stream.CopyToAsync(fs).ConfigureAwait(false);
await stream.FlushAsync().ConfigureAwait(false);
await fs.FlushAsync().ConfigureAwait(false);
}
}
示例3: CopyWithProgressAsync
public static async Task<long> CopyWithProgressAsync(this Stream source, Stream destination,
ProgressReportDelegate progressReport = null, long sourceLength = 0, int bufferSize = 64 * 1024)
{
long bytesWritten = 0;
long totalBytesToWrite = sourceLength;
byte[] copy_buffer = new byte[bufferSize];
int read;
while ((read = await source.ReadAsync(copy_buffer, 0, copy_buffer.Length)) > 0)
{
await destination.WriteAsync(copy_buffer, 0, read);
bytesWritten += read;
System.Diagnostics.Debug.WriteLine("CopyWithProgress: {0} / {1}", bytesWritten, totalBytesToWrite);
if (null != progressReport)
{
int percentComplete = 0;
if (sourceLength > 0)
percentComplete = (int)((bytesWritten/(double)totalBytesToWrite) * 100);
progressReport(percentComplete, bytesWritten, totalBytesToWrite);
}
}
await destination.FlushAsync();
return bytesWritten;
}
示例4: WriteRequest
private static async Task WriteRequest(byte[] message, Stream tcpStream)
{
var lengthBigEndian = BitConverter.GetBytes(message.Length);
Array.Reverse(lengthBigEndian);
tcpStream.Write(lengthBigEndian, 0, lengthBigEndian.Length);
tcpStream.Write(message, 0, message.Length);
await tcpStream.FlushAsync();
}
示例5: SendOverStream
public void SendOverStream(Stream stream, Notification value)
{
byte[] data = ServiceManager.Serializer.Serialize(value);
byte[] dataLength = BitConverter.GetBytes(data.Length);
stream.Write(dataLength, 0, dataLength.Length);
stream.Write(data, 0, data.Length);
stream.FlushAsync();
}
示例6: CopyToAsync
public static async Task CopyToAsync(this Stream source, Stream destination, IProgress<long> progress, CancellationToken cancellationToken) {
byte[] buffer = new byte[81920];
int bytesRead = 0;
long bytesTotal = 0;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken)) > 0) {
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken);
bytesTotal += bytesRead;
progress?.Report(bytesTotal);
}
await destination.FlushAsync(cancellationToken);
}
示例7: ZipCatrobatPackage
public async Task ZipCatrobatPackage(Stream zipStream, string localStoragePath)
{
using (var storage = StorageSystem.GetStorage())
{
using(var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
{
await WriteFilesRecursiveToZip(archive, storage, localStoragePath, "");
await zipStream.FlushAsync();
}
}
//ZipFile.CreateFromDirectory(localStoragePath, zipPath, CompressionLevel.Fastest, true);
}
示例8: DownloadAsync
public async Task DownloadAsync(Uri uri, Stream destination, CancellationToken cancelToken)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
using (Stream stream = await m_client.GetStreamAsync(uri))
{
await stream.CopyToAsync(destination, 4096, cancelToken);
await destination.FlushAsync();
}
}
示例9: ToStreamMaxJPEGAsync
public async static Task ToStreamMaxJPEGAsync(this Image iImage,
Stream iStream)
{
using (EncoderParameters pEPsParams = new EncoderParameters(1))
{
EncoderParameter pEPrParam = new EncoderParameter(Encoder.Quality, 100L);
pEPsParams.Param[0] = pEPrParam;
using (Bitmap pBmpCopy = new Bitmap(iImage))
{
pBmpCopy.Save(iStream, GetEncoder(ImageFormat.Jpeg), pEPsParams);
}
await iStream.FlushAsync();
}
}
示例10: ConnectAsync
public async Task ConnectAsync()
{
_tcpClient.NoDelay = true;
await _tcpClient.ConnectAsync(_host, _port);
_stream = _tcpClient.GetStream();
await _stream.WriteAsync(TcpConstants.MagicCookie, 0, TcpConstants.MagicCookie.Length);
await _stream.FlushAsync();
ModData = await _stream.ReadObjectAsync<ModData>();
_isConnected = true;
var spawnedTask = Task.Run(() => RunChannel());
}
示例11: SendHeaders
private async Task SendHeaders(Uri url)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("GET / HTTP/1.1");
sb.AppendLine("Connection: keep-alive");
sb.AppendLine("Host: " + url.Host);
sb.AppendLine("icy-metadata: 0");
sb.AppendLine();
outStream = underlayingSocket.OutputStream.AsStreamForWrite();
var bytes = System.Text.UTF8Encoding.UTF8.GetBytes(sb.ToString());
await outStream.WriteAsync(bytes, 0, bytes.Length);
await outStream.FlushAsync();
}
示例12: WriteAsync
/// <summary>
/// Writes Packet to a stream Asynchronously
/// </summary>
/// <param name="stream">The stream to write to</param>
/// <param name="flush">Flush the streams buffer</param>
/// <param name="token">Cancellation token</param>
/// <remarks>Known to freeze randomly so using the synchronously version is currently reccomended</remarks>
public async Task WriteAsync(Stream stream, bool flush = true, CancellationToken token = default(CancellationToken))
{
if (!stream.CanWrite)
throw new XpressNetProtocolViolationException("Unable to write to stream");
var buffer = new List<byte>();
buffer.Add(HeaderByte);
buffer.AddRange(Payload);
buffer.Add(GetXoredByte());
await stream.WriteAsync(buffer.ToArray(), 0, buffer.Count, token);
if (flush)
{
await stream.FlushAsync(token);
LogPacketWrite();
}
}
示例13: WriteToStream
public async Task WriteToStream(Stream stream)
{
var bodyArray = Encoding.UTF8.GetBytes(Content);
var contentStream = new MemoryStream(bodyArray);
var headerBuilder = new StringBuilder();
headerBuilder.AppendLine($"HTTP/1.1 {(int)StatusCode} {StatusCode}");
headerBuilder.AppendLine("Server: catnap-srv/1.0.0");
foreach (var header in Headers)
{
headerBuilder.AppendLine($"{header.Key}: {header.Value}");
}
headerBuilder.AppendLine($"Content-Length: {contentStream.Length}");
headerBuilder.AppendLine("Connection: close");
headerBuilder.AppendLine();
var headerArray = Encoding.UTF8.GetBytes(headerBuilder.ToString());
await stream.WriteAsync(headerArray, 0, headerArray.Length);
await contentStream.CopyToAsync(stream);
await stream.FlushAsync();
}
示例14: SaveBinary
public async Task SaveBinary(string fName, Stream stream)
{
try
{
IFolder rootFolder = FileSystem.Current.LocalStorage;//;
IFolder folder = await rootFolder.CreateFolderAsync("Settings", CreationCollisionOption.OpenIfExists, CancellationToken.None);
IFile file = await folder.CreateFileAsync(fName, CreationCollisionOption.ReplaceExisting, CancellationToken.None);
var t = file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite, CancellationToken.None);
using (Stream s = await t)
{
await stream.CopyToAsync(s);
await stream.FlushAsync();
s.Dispose();
stream.Dispose();
}
//stream.Dispose ();
//s.Dispose ();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
示例15: WriteAsync
private static async Task WriteAsync(AdminProject project, Stream stream, CancellationToken cancellationToken)
{
var stringWriter = new StringWriter();
await stringWriter.WriteLineAsync(string.Format("{0},{1},{2},{3},{4},{5},{6}",
project.Id.ToCsvValue(),
project.Name.ToCsvValue(),
project.Created.ToCsvValue(),
project.UserId.ToCsvValue(),
project.UserName.ToCsvValue(),
project.ProductType.ToCsvValue(),
project.Product.ToCsvValue()));
byte[] bytes = Encoding.UTF8.GetBytes(stringWriter.ToString());
await stream.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
await stream.FlushAsync(cancellationToken);
}