本文整理汇总了C#中MemoryStream.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.CopyTo方法的具体用法?C# MemoryStream.CopyTo怎么用?C# MemoryStream.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.CopyTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Save
private void Save()
{
using (var newZip = new MemoryStream()) {
zipFile.SaveTo(newZip, CompressionType.Deflate, entryNameEncoding ?? Encoding.UTF8);
stream.SetLength(0);
stream.Position = 0;
newZip.Position = 0;
newZip.CopyTo(stream);
}
}
示例2: ExportFileToDirectory
private static void ExportFileToDirectory(UploadedFile file, DirectoryInfo destination, IEnumerable<string> cultures)
{
foreach (var culture in cultures)
{
var cultureDirectory = new DirectoryInfo(Path.Combine(destination.FullName, culture));
if (!cultureDirectory.Exists)
{
cultureDirectory.Create();
}
using (var memoryStream = new MemoryStream())
{
var exportFile = new FileInfo(Path.Combine(cultureDirectory.FullName, file.Filename));
var exportTranslationState = file.ExportTranslation(culture, memoryStream).Result;
if (exportTranslationState == UploadedFile.ExportTranslationState.Success)
{
memoryStream.Position = 0;
using (Stream fileStream = File.OpenWrite(exportFile.FullName))
{
memoryStream.CopyTo(fileStream);
Console.WriteLine("[SUCCESS] Exporting: " + exportFile.FullName + " Locale: " + culture);
}
}
else if (exportTranslationState == UploadedFile.ExportTranslationState.NoContent)
{
Console.WriteLine("[WARNING] Exporting: " + exportFile.FullName + " Locale: " + culture + " has no translations!");
}
else
{
Console.WriteLine("[FAILED] Exporting: " + exportFile.FullName + " Locale: " + culture);
}
}
}
}
示例3: WriteLocalFileHeaderAndDataIfNeeded
private void WriteLocalFileHeaderAndDataIfNeeded()
{
//_storedUncompressedData gets frozen here, and is what gets written to the file
if (_storedUncompressedData != null || _compressedBytes != null)
{
if (_storedUncompressedData != null)
{
_uncompressedSize = _storedUncompressedData.Length;
//The compressor fills in CRC and sizes
//The DirectToArchiveWriterStream writes headers and such
using (Stream entryWriter = new DirectToArchiveWriterStream(
GetDataCompressor(_archive.ArchiveStream, true, null),
this))
{
_storedUncompressedData.Seek(0, SeekOrigin.Begin);
_storedUncompressedData.CopyTo(entryWriter);
_storedUncompressedData.Dispose();
_storedUncompressedData = null;
}
}
else
{
// we know the sizes at this point, so just go ahead and write the headers
if (_uncompressedSize == 0)
CompressionMethod = CompressionMethodValues.Stored;
WriteLocalFileHeader(false);
// we just want to copy the compressed bytes straight over, but the stream apis
// don't handle larger than 32 bit values, so we just wrap it in a stream
using (MemoryStream compressedStream = new MemoryStream(_compressedBytes))
{
compressedStream.CopyTo(_archive.ArchiveStream);
}
}
}
else //there is no data in the file, but if we are in update mode, we still need to write a header
{
if (_archive.Mode == ZipArchiveMode.Update || !_everOpenedForWrite)
{
_everOpenedForWrite = true;
WriteLocalFileHeader(true);
}
}
}
示例4: MemoryStream_CopyTo_Invalid
public static void MemoryStream_CopyTo_Invalid()
{
MemoryStream memoryStream;
using (memoryStream = new MemoryStream())
{
Assert.Throws<ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null));
// Validate the destination parameter first.
Assert.Throws<ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null, bufferSize: 0));
Assert.Throws<ArgumentNullException>("destination", () => memoryStream.CopyTo(destination: null, bufferSize: -1));
// Then bufferSize.
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // 0-length buffer doesn't make sense.
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));
}
// After the Stream is disposed, we should fail on all CopyTos.
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: 0)); // Not before bufferSize is validated.
Assert.Throws<ArgumentOutOfRangeException>("bufferSize", () => memoryStream.CopyTo(Stream.Null, bufferSize: -1));
MemoryStream disposedStream = memoryStream;
// We should throw first for the source being disposed...
Assert.Throws<ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1));
// Then for the destination being disposed.
memoryStream = new MemoryStream();
Assert.Throws<ObjectDisposedException>(() => memoryStream.CopyTo(disposedStream, 1));
// Then we should check whether we can't read but can write, which isn't possible for non-subclassed MemoryStreams.
// THen we should check whether the destination can read but can't write.
var readOnlyStream = new DelegateStream(
canReadFunc: () => true,
canWriteFunc: () => false
);
Assert.Throws<NotSupportedException>(() => memoryStream.CopyTo(readOnlyStream, 1));
}