本文整理汇总了C#中System.IO.MemoryStream.WriteAsync方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.WriteAsync方法的具体用法?C# MemoryStream.WriteAsync怎么用?C# MemoryStream.WriteAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.WriteAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReceieveMessageAsync
public async Task<byte[]> ReceieveMessageAsync()
{
if (IsDisposed) throw new ObjectDisposedException(nameof(Connection));
if (!ConnectionStream.CanRead) throw new AccessViolationException(nameof(ConnectionStream));
int size;
using (var stream = new MemoryStream())
{
var message = new byte[4];
while (stream.Length < 4 && ConnectionStream.CanRead)
{
int receieved = await ConnectionStream.ReadAsync(message, 0, 4 - Convert.ToInt32(stream.Length));
await stream.WriteAsync(message, 0, receieved);
}
size = BitConverter.ToInt32(stream.ToArray(), 0);
}
if (!ConnectionStream.CanRead)
throw new AccessViolationException(nameof(ConnectionStream));
if (size > MaxSize)
return null;
using (var stream = new MemoryStream())
{
var buffer = new byte[512];
while (stream.Length < size)
{
if (!ConnectionStream.CanRead) throw new AccessViolationException(nameof(ConnectionStream));
var receieved = await ConnectionStream.ReadAsync(buffer, 0, size - Convert.ToInt32(stream.Length));
await stream.WriteAsync(buffer, 0, receieved);
}
return stream.ToArray();
}
}
示例2: LoadFirstLinesFromDiskAsync
public async Task<string> LoadFirstLinesFromDiskAsync(string fileName, int lineCount, SecureString passphrase)
{
using (var inputStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
{
using (var outputStream = new MemoryStream())
{
var password = SecureStringCredentialStore.SecureStringToString(passphrase);
using (var cryptoStream = CipherStream.Open(inputStream, password))
{
while (cryptoStream.CanRead)
{
var buffer = new byte[4096];
await cryptoStream.ReadAsync(buffer, 0, 4096);
await outputStream.WriteAsync(buffer, 0, 4096);
char[] chunk = Encoding.UTF8.GetChars(buffer);
var occurances = chunk.Count(c => c == '\n');
if (occurances >= lineCount) break;
}
}
outputStream.Position = 0;
using (var reader = new StreamReader(outputStream))
{
return await reader.ReadToEndAsync();
}
}
}
}
示例3: PackAsync
public async Task<byte[]> PackAsync(ICollection<ClientFile> files)
{
if (files == null) throw new ArgumentNullException("files");
if (files.Count == 0) throw new ArgumentOutOfRangeException("files");
var header = new StringBuilder();
var totalBuffer = new byte[files.Select(f => f.Data.Length).Sum()];
using (var output = new MemoryStream(totalBuffer))
{
foreach (var f in files)
{
var data = f.Data;
await output.WriteAsync(data, 0, data.Length);
if (header.Length > 0)
{
header.Append(FileSeparator);
}
header.Append(f.Name);
header.Append(SizeSeparator);
header.Append(data.Length);
}
var headerData = Encoding.Unicode.GetBytes(header.ToString());
var headerSize = BitConverter.GetBytes(headerData.Length);
return Utilities.Concat(headerSize, headerData, totalBuffer);
}
}
示例4: AliceSendsData
private async static Task<byte[]> AliceSendsData(string message)
{
Console.WriteLine("Alice send message {0}", message);
byte[] rawData = Encoding.UTF8.GetBytes(message);
byte[] encryptedData = null;
using(var aliceAlgo = new ECDiffieHellmanCng(aliceKey))
{
using(CngKey bobPubKey = CngKey.Import(bobPubKeyBlob, CngKeyBlobFormat.GenericPublicBlob))
{
byte[] symmKey = aliceAlgo.DeriveKeyMaterial(bobPubKey);
Console.WriteLine("Alice create this symm key with Bobs public key information : {0}", Convert.ToBase64String(symmKey));
using(var aes = new AesCryptoServiceProvider())
{
aes.Key = symmKey;
aes.GenerateIV();
using(ICryptoTransform encryptor = aes.CreateEncryptor())
{
using(MemoryStream ms = new MemoryStream())
{
var cs = new CryptoStream(ms, encryptor,CryptoStreamMode.Write);
await ms.WriteAsync(aes.IV, 0, aes.IV.Length);
cs.Write(rawData, 0, rawData.Length);
cs.Close();
encryptedData = ms.ToArray();
}
}
aes.Clear();
}
}
}
Console.WriteLine("Alice message is encrypted : {0}", Convert.ToBase64String(encryptedData));
return encryptedData;
}
示例5: Get
public async Task<HttpResponseMessage> Get(string key)
{
var cachedItem = MemoryCache.Default.Get(key) as byte[];
if(cachedItem == null)
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
Stream stream = null;
if (cachedItem != null)
{
stream = new MemoryStream();
await stream.WriteAsync(cachedItem, 0, cachedItem.Length);
stream.Position = 0;
}
//var content = new PushStreamContent(async (stream, httpContent, transportContext) =>
//{
// await stream.WriteAsync(cachedItem, 0, cachedItem.Length);
// stream.Close();
//});
var content = new StreamContent(stream);
return new HttpResponseMessage() { Content = content };
}
示例6: ReadThumbnailAsync
/// <summary>
/// Read a thumbnail in metadata from byte array.
/// </summary>
/// <param name="bytes">Byte array</param>
/// <returns>BitmapImage of thumbnail</returns>
internal static async Task<BitmapImage> ReadThumbnailAsync(byte[] bytes)
{
if ((bytes == null) || !bytes.Any())
throw new ArgumentNullException("bytes");
try
{
using (var ms = new MemoryStream())
{
await ms.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
// If continued by ContinueWith, an exception will not be caught by try-catch block in debug mode.
return ReadThumbnailFromExifByImaging(ms);
}
}
catch (Exception ex)
{
Debug.WriteLine("Failed to read a thumbnail. {0}", ex);
if (IsImageNotSupported(ex))
throw new ImageNotSupportedException();
throw;
}
}
示例7: AssembleFile
private static async Task<byte[]> AssembleFile(string filePath) {
byte[] buffer;
using (var resultStream = new MemoryStream()) {
var content = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}",
Guid.NewGuid(),
Path.GetFileName(filePath),
Environment.NewLine);
buffer = Encoding.UTF8.GetBytes(content);
await resultStream.WriteAsync(buffer, 0, buffer.Length);
content = "Content-Type: application/octet-stream" + Environment.NewLine + Environment.NewLine;
buffer = Encoding.UTF8.GetBytes(content);
await resultStream.WriteAsync(buffer, 0, buffer.Length);
buffer = IOFile.ReadAllBytes(filePath);
await resultStream.WriteAsync(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(Environment.NewLine);
await resultStream.WriteAsync(buffer, 0, buffer.Length);
await resultStream.FlushAsync();
buffer = resultStream.ToArray();
}
return buffer;
}
示例8: ReadStateAsync
public async Task ReadStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
{
var tableResult = await _table.ExecuteAsync(TableOperation.Retrieve<DynamicTableEntity>(grainReference.ToKeyString(), grainType));
if (tableResult.Result == null)
{
return;
}
var entity = tableResult.Result as DynamicTableEntity;
var serializer = new JsonSerializer();
using (var memoryStream = new MemoryStream())
{
foreach (var propertyName in entity.Properties.Keys.Where(p => p.StartsWith("d")).OrderBy(p => p))
{
var dataPart = entity.Properties[propertyName];
await memoryStream.WriteAsync(dataPart.BinaryValue, 0, dataPart.BinaryValue.Length);
}
memoryStream.Position = 0;
using (var bsonReader = new BsonReader(memoryStream))
{
var data = serializer.Deserialize<Dictionary<string, object>>(bsonReader);
grainState.SetAll(data);
}
}
}
示例9: ToByteArrayAsync
public static async Task<byte[]> ToByteArrayAsync(this ArraySegment<byte> source) {
byte[] data = null;
using (MemoryStream ms = new MemoryStream()) {
await ms.WriteAsync(source.Array, source.Offset, source.Count);
await ms.FlushAsync();
data = ms.ToArray();
}
return data;
}
示例10: GetDataHeader
private async Task<DataHeader> GetDataHeader(byte[] btarrHeader) {
BinaryFormatter objFormatter = new BinaryFormatter();
DataHeader objHeader;
using(MemoryStream objStream = new MemoryStream()) {
await objStream.WriteAsync(btarrHeader, 0, btarrHeader.Length);
objStream.Position = 0;
objHeader = (DataHeader)objFormatter.Deserialize(objStream);
}
return objHeader;
}
示例11: UploadTextAsync
public static async Task UploadTextAsync(ICloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
{
byte[] textAsBytes = encoding.GetBytes(text);
using (MemoryStream stream = new MemoryStream())
{
await stream.WriteAsync(textAsBytes, 0, textAsBytes.Length);
if (blob.BlobType == BlobType.PageBlob)
{
int lastPageSize = (int)(stream.Length % 512);
if (lastPageSize != 0)
{
byte[] padding = new byte[512 - lastPageSize];
await stream.WriteAsync(padding, 0, padding.Length);
}
}
stream.Seek(0, SeekOrigin.Begin);
await blob.UploadFromStreamAsync(stream.AsInputStream(), accessCondition, options, operationContext);
}
}
示例12: CreateTempCopyStream
public static async Task<Stream> CreateTempCopyStream(String path)
{
var bytes = File.ReadAllBytes(path);
var ms = new MemoryStream();
await ms.WriteAsync(bytes, 0, bytes.Length);
ms.Position = 0;
return ms;
}
示例13: ProcessingImage
private async Task ProcessingImage(byte[] btarrResponse) {
Bitmap objBitMap;
BinaryFormatter objFormatter = new BinaryFormatter();
using(MemoryStream objStream = new MemoryStream()) {
await objStream.WriteAsync(btarrResponse, 0, btarrResponse.Length);
objStream.Position = 0;
objBitMap = (Bitmap)objFormatter.Deserialize(objStream);
objBitMap.Save(@"D:\screenshot.jpg");
}
}
示例14: ProcessingStreets
private async Task ProcessingStreets(byte[] btarrResponse) {
BinaryFormatter objFormatter = new BinaryFormatter();
using(MemoryStream objStream = new MemoryStream()) {
await objStream.WriteAsync(btarrResponse, 0, btarrResponse.Length);
objStream.Position = 0;
List<Street> objStreets = (List<Street>)objFormatter.Deserialize(objStream);
ShowStreets(objStreets);
}
}
示例15: ReadToEndAsByteAsync
/// <summary>
///
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static async Task<byte[]> ReadToEndAsByteAsync(this Stream stream) {
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream()) {
int read;
while ((read = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) {
await ms.WriteAsync(buffer, 0, read);
}
return ms.ToArray();
}
}