本文整理汇总了C#中System.IO.MemoryStream.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.MemoryStream.CopyTo方法的具体用法?C# System.IO.MemoryStream.CopyTo怎么用?C# System.IO.MemoryStream.CopyTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了System.IO.MemoryStream.CopyTo方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToByteArray
public static byte[] ToByteArray(this System.IO.Stream strm)
{
if(strm is System.IO.MemoryStream)
{
return (strm as System.IO.MemoryStream).ToArray();
}
long originalPosition = -1;
if(strm.CanSeek)
{
originalPosition = strm.Position;
}
try
{
using (var ms = new System.IO.MemoryStream())
{
ms.CopyTo(ms);
return ms.ToArray();
}
}
catch(Exception ex)
{
throw ex;
}
finally
{
if (originalPosition >= 0) strm.Position = originalPosition;
}
}
示例2: CreateUpdatePackage
public static void CreateUpdatePackage(System.Security.Cryptography.RSACryptoServiceProvider key, string inputfolder, string outputfolder, string manifest = null)
{
// Read the existing manifest
UpdateInfo remoteManifest;
var manifestpath = manifest ?? System.IO.Path.Combine(inputfolder, UPDATE_MANIFEST_FILENAME);
using(var s = System.IO.File.OpenRead(manifestpath))
using(var sr = new System.IO.StreamReader(s))
using(var jr = new Newtonsoft.Json.JsonTextReader(sr))
remoteManifest = new Newtonsoft.Json.JsonSerializer().Deserialize<UpdateInfo>(jr);
if (remoteManifest.Files == null)
remoteManifest.Files = new FileEntry[0];
if (remoteManifest.ReleaseTime.Ticks == 0)
remoteManifest.ReleaseTime = DateTime.UtcNow;
var ignoreFiles = (from n in remoteManifest.Files
where n.Ignore
select n).ToArray();
var ignoreMap = ignoreFiles.ToDictionary(k => k.Path, k => "", Duplicati.Library.Utility.Utility.ClientFilenameStringComparer);
remoteManifest.MD5 = null;
remoteManifest.SHA256 = null;
remoteManifest.Files = null;
remoteManifest.UncompressedSize = 0;
var localManifest = remoteManifest.Clone();
localManifest.RemoteURLS = null;
inputfolder = Duplicati.Library.Utility.Utility.AppendDirSeparator(inputfolder);
var baselen = inputfolder.Length;
var dirsep = System.IO.Path.DirectorySeparatorChar.ToString();
ignoreMap.Add(UPDATE_MANIFEST_FILENAME, "");
var md5 = System.Security.Cryptography.MD5.Create();
var sha256 = System.Security.Cryptography.SHA256.Create();
Func<string, string> computeMD5 = (path) =>
{
md5.Initialize();
using(var fs = System.IO.File.OpenRead(path))
return Convert.ToBase64String(md5.ComputeHash(fs));
};
Func<string, string> computeSHA256 = (path) =>
{
sha256.Initialize();
using(var fs = System.IO.File.OpenRead(path))
return Convert.ToBase64String(sha256.ComputeHash(fs));
};
// Build a zip
using (var archive_temp = new Duplicati.Library.Utility.TempFile())
{
using (var zipfile = new Duplicati.Library.Compression.FileArchiveZip(archive_temp, new Dictionary<string, string>()))
{
Func<string, string, bool> addToArchive = (path, relpath) =>
{
if (ignoreMap.ContainsKey(relpath))
return false;
if (path.EndsWith(dirsep))
return true;
using (var source = System.IO.File.OpenRead(path))
using (var target = zipfile.CreateFile(relpath,
Duplicati.Library.Interface.CompressionHint.Compressible,
System.IO.File.GetLastAccessTimeUtc(path)))
{
source.CopyTo(target);
remoteManifest.UncompressedSize += source.Length;
}
return true;
};
// Build the update manifest
localManifest.Files =
(from fse in Duplicati.Library.Utility.Utility.EnumerateFileSystemEntries(inputfolder)
let relpath = fse.Substring(baselen)
where addToArchive(fse, relpath)
select new FileEntry() {
Path = relpath,
LastWriteTime = System.IO.File.GetLastAccessTimeUtc(fse),
MD5 = fse.EndsWith(dirsep) ? null : computeMD5(fse),
SHA256 = fse.EndsWith(dirsep) ? null : computeSHA256(fse)
})
.Union(ignoreFiles).ToArray();
// Write a signed manifest with the files
using (var ms = new System.IO.MemoryStream())
using (var sw = new System.IO.StreamWriter(ms))
{
new Newtonsoft.Json.JsonSerializer().Serialize(sw, localManifest);
//.........这里部分代码省略.........
示例3: Deflate
internal static byte[] Deflate(byte[] source_byte, System.IO.Compression.CompressionMode type_deflate)
{
byte[] dest_byte = null;
using (System.IO.MemoryStream dest_mem = new System.IO.MemoryStream())
{
using (System.IO.MemoryStream source_mem = new System.IO.MemoryStream(source_byte))
{
if (source_mem.CanSeek)
{
source_mem.Seek(0, System.IO.SeekOrigin.Begin);
}
if (type_deflate == System.IO.Compression.CompressionMode.Compress)
{
using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(dest_mem, type_deflate))
{
source_mem.CopyTo(deflate);
deflate.Flush();
}
}
else
{
using (System.IO.Compression.DeflateStream deflate = new System.IO.Compression.DeflateStream(source_mem, type_deflate))
{
deflate.CopyTo(dest_mem);
deflate.Flush();
}
}
source_mem.Flush();
}
if (dest_mem.CanSeek)
{
dest_mem.Seek(0, System.IO.SeekOrigin.Begin);
}
dest_byte = dest_mem.ToArray();
dest_mem.Flush();
}
return dest_byte;
}
示例4: Forge
public ActionResult Forge(ViewModels.UploadViewModel model)
{
Response.TrySkipIisCustomErrors = true;
if (ModelState.IsValid)
{
var currentUser = UserManager.FindById(User.Identity.GetUserId<int>());
Models.GameMapVariant variant = new Models.GameMapVariant();
variant.Title = model.Title.Replace("\0", "");
variant.ShortDescription = model.Description;
variant.Description = model.Content;
variant.CreatedOn = DateTime.UtcNow;
variant.AuthorId = User.Identity.GetUserId<int>();
variant.File = new Models.File()
{
FileSize = model.File.ContentLength,
FileName = Guid.NewGuid().ToString() + ".vrt",
UploadedOn = variant.CreatedOn,
MD5 = model.File.InputStream.ToMD5()
};
// Validate the variant to see if it's not a duplicate.
var validateMap = GameMapService.ValidateHash(variant.File.MD5);
if (validateMap != null)
{
Response.StatusCode = 400;
return Content(string.Format(
"<b>Keep it Clean!</b> The forge variant has already been uploaded: <a target=\"_blank\" href=\"{0}\">{1}</a>.",
Url.Action("Details", "Forge", new { slug = validateMap.Slug }),
validateMap.Title));
}
/* Read the map type from the uploaded file.
* This is also a validation message to make sure
* that the file uploaded is an actual map.
*/
var detectType = VariantDetector.Detect(model.File.InputStream);
if (detectType == VariantType.Invalid)
{
// The given map doesn't exist.
model.File.InputStream.Close();
Response.StatusCode = 400;
return Content("<b>Keep it Clean!</b> The file uploaded is invalid. Please make sure the map is in the new format.");
}
else if (detectType == VariantType.GameVariant)
{
// The given map doesn't exist.
model.File.InputStream.Close();
Response.StatusCode = 400;
return Content("<strong>PARDON OUR DUST!</strong> Can't upload game variant as forge variant.");
}
string path = System.IO.Path.Combine(Server.MapPath("~/Content/Files/Forge/"), variant.File.FileName);
using (var stream = new System.IO.MemoryStream())
{
model.File.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
model.File.InputStream.CopyTo(stream);
ForgeVariant variantItem = new ForgeVariant(stream);
var map = GameMapService.GetMapByInternalId(variantItem.MapId);
if (map != null)
{
variant.GameMapId = map.Id;
variantItem.VariantName = model.Title;
//variantItem.VariantAuthor = currentUser.UploaderName;
variantItem.VariantDescription = variant.ShortDescription;
variantItem.Save();
// Save the file.
using (var fileStream = System.IO.File.Create(path))
{
stream.Seek(0, System.IO.SeekOrigin.Begin);
stream.CopyTo(fileStream);
}
}
else
{
Response.StatusCode = 400;
return Content("<strong>PARDON OUR DUST!</strong> We currently do not support the uploaded map.");
}
}
GameMapService.AddVariant(variant);
GameMapService.Save();
return Content(Url.Action("Details", "Forge", new { slug = variant.Slug }));
}
Response.StatusCode = 400;
return View("~/Views/Shared/_ModelState.cshtml");
}
示例5: EmitELFStream
/// <summary>
/// Produces an ELF compatible binary output stream with the compiled methods
/// </summary>
/// <param name="outstream">The output stream</param>
/// <param name="assemblyOutput">The assembly text output, can be null</param>
/// <param name="methods">The compiled methods</param>
public Dictionary<int, Mono.Cecil.MethodReference> EmitELFStream(System.IO.Stream outstream, System.IO.TextWriter assemblyOutput, IEnumerable<ICompiledMethod> methods)
{
Dictionary<int, Mono.Cecil.MethodReference> callpoints;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
uint bootloader_offset;
callpoints = EmitInstructionStream(ms, assemblyOutput, methods, out bootloader_offset);
SPEEmulator.ELFReader.EmitELFHeader((uint)ms.Length, bootloader_offset, outstream);
ms.Position = 0;
ms.CopyTo(outstream);
}
return callpoints;
}