本文整理汇总了C#中System.IO.MemoryStream.AsInputStream方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.AsInputStream方法的具体用法?C# MemoryStream.AsInputStream怎么用?C# MemoryStream.AsInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.AsInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BlobSeekTestAsync
public async Task BlobSeekTestAsync()
{
byte[] buffer = GetRandomBuffer(2 * 1024);
CloudBlobContainer container = GetRandomContainerReference();
try
{
await container.CreateAsync();
CloudPageBlob blob = container.GetPageBlobReference("blob1");
using (MemoryStream srcStream = new MemoryStream(buffer))
{
await blob.UploadFromStreamAsync(srcStream.AsInputStream(), null, null, null);
using (IInputStream blobStream = await blob.OpenReadAsync())
{
Stream blobStreamForRead = blobStream.AsStreamForRead();
blobStreamForRead.Seek(2048, 0);
byte[] buff = new byte[100];
int numRead = await blobStreamForRead.ReadAsync(buff, 0, 100);
Assert.AreEqual(numRead, 0);
}
}
}
finally
{
container.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例2: FileReadStreamBasicTestAsync
public async Task FileReadStreamBasicTestAsync()
{
byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
CloudFileShare share = GetRandomShareReference();
try
{
await share.CreateAsync();
CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
using (MemoryStream wholeFile = new MemoryStream(buffer))
{
await file.UploadFromStreamAsync(wholeFile.AsInputStream());
}
using (MemoryStream wholeFile = new MemoryStream(buffer))
{
IRandomAccessStreamWithContentType readStream = await file.OpenReadAsync();
using (Stream fileStream = readStream.AsStreamForRead())
{
TestHelper.AssertStreamsAreEqual(wholeFile, fileStream);
}
}
}
finally
{
share.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例3: PageBlobDownloadToStreamRangeTestAsync
public async Task PageBlobDownloadToStreamRangeTestAsync()
{
byte[] buffer = GetRandomBuffer(2 * 1024);
CloudPageBlob blob = this.testContainer.GetPageBlobReference("blob1");
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
byte[] testBuffer = new byte[1024];
MemoryStream blobStream = new MemoryStream(testBuffer);
Exception ex = await TestHelper.ExpectedExceptionAsync<Exception>(
async () => await blob.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 0, 0),
"Requesting 0 bytes when downloading range should not work");
Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
await blob.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 0, 1024);
Assert.AreEqual(blobStream.Position, 1024);
TestHelper.AssertStreamsAreEqualAtIndex(blobStream, wholeBlob, 0, 0, 1024);
CloudPageBlob blob2 = this.testContainer.GetPageBlobReference("blob1");
MemoryStream blobStream2 = new MemoryStream(testBuffer);
ex = await TestHelper.ExpectedExceptionAsync<Exception>(
async () => await blob2.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 1024, 0),
"Requesting 0 bytes when downloading range should not work");
Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
await blob2.DownloadRangeToStreamAsync(blobStream2.AsOutputStream(), 1024, 1024);
TestHelper.AssertStreamsAreEqualAtIndex(blobStream2, wholeBlob, 0, 1024, 1024);
AssertAreEqual(blob, blob2);
}
}
示例4: FileSeekTestAsync
public async Task FileSeekTestAsync()
{
byte[] buffer = GetRandomBuffer(2 * 1024);
CloudFileShare share = GetRandomShareReference();
try
{
await share.CreateAsync();
CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
using (MemoryStream srcStream = new MemoryStream(buffer))
{
await file.UploadFromStreamAsync(srcStream.AsInputStream(), null, null, null);
using (var fileStream = await file.OpenReadAsync())
{
Stream fileStreamForRead = fileStream.AsStreamForRead();
fileStreamForRead.Seek(2048, 0);
byte[] buff = new byte[100];
int numRead = await fileStreamForRead.ReadAsync(buff, 0, 100);
Assert.AreEqual(numRead, 0);
}
}
}
finally
{
share.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例5: BlockBlobReadStreamBasicTestAsync
public async Task BlockBlobReadStreamBasicTestAsync()
{
byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
CloudBlobContainer container = GetRandomContainerReference();
try
{
await container.CreateAsync();
CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
}
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
using (IInputStream blobStream = await blob.OpenReadAsync())
{
await TestHelper.AssertStreamsAreEqualAsync(wholeBlob.AsInputStream(), blobStream);
}
}
}
finally
{
container.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例6: PageBlobReadStreamBasicTestAsync
public async Task PageBlobReadStreamBasicTestAsync()
{
byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
CloudBlobContainer container = GetRandomContainerReference();
try
{
await container.CreateAsync();
CloudPageBlob blob = container.GetPageBlobReference("blob1");
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
}
using (MemoryStream wholeBlob = new MemoryStream(buffer))
{
wholeBlob.Seek(0, SeekOrigin.End);
IRandomAccessStreamWithContentType readStream = await blob.OpenReadAsync();
using (Stream blobStream = readStream.AsStreamForRead())
{
blobStream.Seek(0, SeekOrigin.End);
TestHelper.AssertStreamsAreEqual(wholeBlob, blobStream);
}
}
}
finally
{
container.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例7: FileReadLockToETagTestAsync
public async Task FileReadLockToETagTestAsync()
{
byte[] outBuffer = new byte[1 * 1024 * 1024];
byte[] buffer = GetRandomBuffer(2 * outBuffer.Length);
CloudFileShare share = GetRandomShareReference();
try
{
await share.CreateAsync();
CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
file.StreamMinimumReadSizeInBytes = outBuffer.Length;
using (MemoryStream wholeFile = new MemoryStream(buffer))
{
await file.UploadFromStreamAsync(wholeFile.AsInputStream());
}
OperationContext opContext = new OperationContext();
using (IRandomAccessStreamWithContentType fileStream = await file.OpenReadAsync(null, null, opContext))
{
Stream fileStreamForRead = fileStream.AsStreamForRead();
await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length);
await file.SetMetadataAsync();
await ExpectedExceptionAsync(
async () => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
opContext,
"File read stream should fail if file is modified during read",
HttpStatusCode.PreconditionFailed);
}
opContext = new OperationContext();
using (IRandomAccessStreamWithContentType fileStream = await file.OpenReadAsync(null, null, opContext))
{
Stream fileStreamForRead = fileStream.AsStreamForRead();
long length = fileStreamForRead.Length;
await file.SetMetadataAsync();
await ExpectedExceptionAsync(
async () => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
opContext,
"File read stream should fail if file is modified during read",
HttpStatusCode.PreconditionFailed);
}
/*
opContext = new OperationContext();
AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1)));
await file.SetMetadataAsync();
await TestHelper.ExpectedExceptionAsync(
async () => await file.OpenReadAsync(accessCondition, null, opContext),
opContext,
"File read stream should fail if file is modified during read",
HttpStatusCode.PreconditionFailed);
*/
}
finally
{
share.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例8: UploadTextAsync
public static async Task UploadTextAsync(CloudFile file, string text, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
{
byte[] textAsBytes = encoding.GetBytes(text);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(textAsBytes, 0, textAsBytes.Length);
stream.Seek(0, SeekOrigin.Begin);
file.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;
await file.UploadFromStreamAsync(stream.AsInputStream(), accessCondition, options, operationContext);
}
}
示例9: SaveAsync
public async Task SaveAsync()
{
this.SaveApplicationState();
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(SessionStateFileName, CreationCollisionOption.ReplaceExisting);
var bytes = this.SerializeState(this.states);
using (var sessionData = new MemoryStream(bytes))
{
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
sessionData.Seek(0, SeekOrigin.Begin);
var provider = new DataProtectionProvider("LOCAL=user");
await provider.ProtectStreamAsync(sessionData.AsInputStream(), fileStream);
await fileStream.FlushAsync();
}
}
}
示例10: FileStoreContentMD5TestAsync
public async Task FileStoreContentMD5TestAsync()
{
FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
{
StoreFileContentMD5 = false,
};
FileRequestOptions optionsWithMD5 = new FileRequestOptions()
{
StoreFileContentMD5 = true,
};
CloudFileShare share = GetRandomShareReference();
try
{
await share.CreateAsync();
CloudFile file = share.GetRootDirectoryReference().GetFileReference("file4");
using (Stream stream = new MemoryStream())
{
await file.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
}
await file.FetchAttributesAsync();
Assert.IsNotNull(file.Properties.ContentMD5);
file = share.GetRootDirectoryReference().GetFileReference("file5");
using (Stream stream = new MemoryStream())
{
await file.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null);
}
await file.FetchAttributesAsync();
Assert.IsNull(file.Properties.ContentMD5);
file = share.GetRootDirectoryReference().GetFileReference("file6");
using (Stream stream = new MemoryStream())
{
await file.UploadFromStreamAsync(stream.AsInputStream());
}
await file.FetchAttributesAsync();
Assert.IsNull(file.Properties.ContentMD5);
}
finally
{
share.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例11: CreateForTestAsync
private static async Task CreateForTestAsync(CloudBlockBlob blob, int blockCount, int blockSize, bool commit = true)
{
byte[] buffer = GetRandomBuffer(blockSize);
List<string> blocks = GetBlockIdList(blockCount);
foreach (string block in blocks)
{
using (MemoryStream stream = new MemoryStream(buffer))
{
await blob.PutBlockAsync(block, stream.AsInputStream(), null);
}
}
if (commit)
{
await blob.PutBlockListAsync(blocks);
}
}
示例12: 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);
}
}
示例13: FileDownloadToStreamRangeTestAsync
public async Task FileDownloadToStreamRangeTestAsync()
{
byte[] buffer = GetRandomBuffer(2 * 1024);
CloudFileShare share = GetRandomShareReference();
try
{
await share.CreateAsync();
CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
using (MemoryStream wholeFile = new MemoryStream(buffer))
{
await file.UploadFromStreamAsync(wholeFile.AsInputStream());
byte[] testBuffer = new byte[1024];
MemoryStream fileStream = new MemoryStream(testBuffer);
Exception ex = await TestHelper.ExpectedExceptionAsync<Exception>(
async () => await file.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 0, 0),
"Requesting 0 bytes when downloading range should not work");
Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
await file.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 0, 1024);
Assert.AreEqual(fileStream.Position, 1024);
TestHelper.AssertStreamsAreEqualAtIndex(fileStream, wholeFile, 0, 0, 1024);
CloudFile file2 = share.GetRootDirectoryReference().GetFileReference("file1");
MemoryStream fileStream2 = new MemoryStream(testBuffer);
ex = await TestHelper.ExpectedExceptionAsync<Exception>(
async () => await file2.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 1024, 0),
"Requesting 0 bytes when downloading range should not work");
Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
await file2.DownloadRangeToStreamAsync(fileStream2.AsOutputStream(), 1024, 1024);
TestHelper.AssertStreamsAreEqualAtIndex(fileStream2, wholeFile, 0, 1024, 1024);
AssertAreEqual(file, file2);
}
}
finally
{
share.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例14: CloudBlockBlobDownloadToStreamCancelAsync
public async Task CloudBlockBlobDownloadToStreamCancelAsync()
{
byte[] buffer = GetRandomBuffer(1 * 1024 * 1024);
CloudBlobContainer container = GetRandomContainerReference();
try
{
await container.CreateAsync();
CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
using (MemoryStream originalBlob = new MemoryStream(buffer))
{
await blob.UploadFromStreamAsync(originalBlob.AsInputStream());
using (MemoryStream downloadedBlob = new MemoryStream())
{
OperationContext operationContext = new OperationContext();
IAsyncAction action = blob.DownloadToStreamAsync(downloadedBlob.AsOutputStream(), null, null, operationContext);
await Task.Delay(100);
action.Cancel();
try
{
await action;
}
catch (Exception)
{
Assert.AreEqual(operationContext.LastResult.Exception.Message, "A task was canceled.");
Assert.AreEqual(operationContext.LastResult.HttpStatusCode, 306);
//Assert.AreEqual(operationContext.LastResult.HttpStatusMessage, "Unused");
}
TestHelper.AssertNAttempts(operationContext, 1);
}
}
}
finally
{
container.DeleteIfExistsAsync().AsTask().Wait();
}
}
示例15: SaveAsync
/// <summary>
/// Save the current <see cref="SessionState"/>. Any <see cref="Frame"/> instances
/// registered with <see cref="RegisterFrame"/> will also preserve their current
/// navigation stack, which in turn gives their active <see cref="Page"/> an opportunity
/// to save its state.
/// </summary>
/// <returns>An asynchronous task that reflects when session state has been saved.</returns>
public async Task SaveAsync()
{
try
{
// Save the navigation state for all registered frames
foreach (var weakFrameReference in _registeredFrames)
{
IFrameFacade frame;
if (weakFrameReference.TryGetTarget(out frame))
{
SaveFrameNavigationState(frame);
}
}
// Serialize the session state synchronously to avoid asynchronous access to shared
// state
MemoryStream sessionData = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
serializer.WriteObject(sessionData, _sessionState);
// Get an output stream for the SessionState file and write the state asynchronously
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Constants.SessionStateFileName, CreationCollisionOption.ReplaceExisting);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
sessionData.Seek(0, SeekOrigin.Begin);
var provider = new DataProtectionProvider("LOCAL=user");
// Encrypt the session data and write it to disk.
await provider.ProtectStreamAsync(sessionData.AsInputStream(), fileStream);
await fileStream.FlushAsync();
}
}
catch (Exception e)
{
throw new SessionStateServiceException(e);
}
}