本文整理汇总了C#中System.IO.MemoryStream.ReadAllBytes方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.ReadAllBytes方法的具体用法?C# MemoryStream.ReadAllBytes怎么用?C# MemoryStream.ReadAllBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.ReadAllBytes方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestReadAllBytes
public void TestReadAllBytes()
{
var fromBytes = Enumerable.Repeat<byte>(1, 1024 * 1024)
.Concat(Enumerable.Repeat<byte>(2, 1024 * 1024))
.Concat(Enumerable.Repeat<byte>(3, 1024 * 1024)).ToArray();
var stream = new MemoryStream(fromBytes);
var toBytes = stream.ReadAllBytes();
Assert.IsTrue(fromBytes.SequenceEqual(toBytes));
}
示例2: Enqueue
public void Enqueue(EnvelopeToken envelope)
{
using (var stream = new MemoryStream())
{
_formatter.Serialize(stream, envelope);
stream.Position = 0;
var bytes = stream.ReadAllBytes();
_queue.Add(bytes);
}
}
示例3: read_all_bytes
public void read_all_bytes()
{
var helloWorld = "Hello world.";
var bytes = Encoding.Unicode.GetBytes(helloWorld);
var stream = new MemoryStream();
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
var actual = stream.ReadAllBytes();
actual.ShouldBe(bytes);
}
示例4: Extract_PackageContainsFiles_ContentOfEachFileIsWrittenToDisc
public void Extract_PackageContainsFiles_ContentOfEachFileIsWrittenToDisc()
{
// Arrange
string targetFolder = Environment.CurrentDirectory;
string file1Path = "tools\\file1.txt";
string file1Content = Guid.NewGuid().ToString();
Stream file1Stream = TestUtilities.GetStreamReaderForText(file1Content).BaseStream;
var file1 = new Mock<IPackageFile>();
file1.Setup(f => f.Path).Returns(file1Path);
file1.Setup(f => f.GetStream()).Returns(file1Stream);
var files = new List<IPackageFile> { file1.Object };
string packageId = "Package.A";
var version = new SemanticVersion(1, 0, 0, 0);
var packageDirectory = Path.Combine(targetFolder, string.Format("{0}.{1}", packageId, version));
var package = new Mock<IPackage>();
package.Setup(p => p.Id).Returns(packageId);
package.Setup(p => p.Version).Returns(version);
package.Setup(p => p.GetFiles()).Returns(files);
var filesystemAccessor = new Mock<IFilesystemAccessor>();
filesystemAccessor.Setup(f => f.DirectoryExists(packageDirectory)).Returns(true);
filesystemAccessor.Setup(f => f.DeleteDirectory(packageDirectory)).Returns(true);
var writeStream = new MemoryStream();
var streamWriter = new StreamWriter(writeStream);
filesystemAccessor.Setup(f => f.GetWriteStream(It.Is<string>(s => s.EndsWith(file1Path)))).Returns(streamWriter.BaseStream);
var nugetPackageExtractor = new NugetPackageExtractor(filesystemAccessor.Object);
// Act
var result = nugetPackageExtractor.Extract(package.Object, targetFolder);
// Assert
var bytes = writeStream.ReadAllBytes();
string newContentOfOldFile = Encoding.UTF8.GetString(bytes);
Assert.IsNotNull(result);
Assert.AreEqual(file1Content, newContentOfOldFile);
file1.Verify(f => f.GetStream(), Times.Once());
filesystemAccessor.Verify(f => f.GetWriteStream(It.Is<string>(s => s.StartsWith(packageDirectory))), Times.Once());
}
示例5: Serialize
public void Serialize(Envelope envelope)
{
if (envelope.Message == null) throw new InvalidOperationException("No message on this envelope to serialize");
if (envelope.ContentType.IsEmpty())
{
envelope.ContentType = _graph.DefaultContentType;
}
var serializer = selectSerializer(envelope);
using (var stream = new MemoryStream())
{
serializer.Serialize(envelope.Message, stream);
stream.Position = 0;
envelope.Data = stream.ReadAllBytes();
}
}
示例6: StreamTest
public void StreamTest()
{
MemoryStream stream = new MemoryStream();
stream.WriteAllBytes(CommonHelper.StringToByte("Hello"));
stream.Position = 0;
var bytes = stream.ReadAllBytes();
Assert.AreEqual(CommonHelper.ByteToString(bytes), "Hello");
}
示例7: Encode
public static object Encode(SQLInfo.ColDef col, object data)
{
if (data == null) return null;
//Logger.ReportVerbose("Encoding " + col.ColName + " as " + col.ColType.Name.ToLower());
switch (col.ColType.Name.ToLower())
{
case "guid":
return data.ToString();
case "string":
return data;
case "datetime":
return ((DateTime)data).Ticks;
case "mediatype":
return ((MediaType)data).ToString();
case "videoformat":
return ((VideoFormat)data).ToString();
case "downloadpolicy":
return ((MediaBrowser.Library.Network.DownloadPolicy)data).ToString();
case "int":
case "int16":
case "int32":
case "int64":
case "long":
case "double":
case "nullable`1":
return data;
default:
var ms = new MemoryStream();
Serializer.Serialize<object>(ms,data);
ms.Seek(0,0);
return ms.ReadAllBytes();
}
}
示例8: PublishPackage
private void PublishPackage(PublishState state)
{
var url = new Uri(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", _baseGalleryServerUrl, PublishPackageService));
using (Stream requestStream = new MemoryStream()) {
var data = new PublishData {
Key = state.PublishKey,
Id = state.PackageMetadata.Id,
Version = state.PackageMetadata.Version.ToString()
};
var jsonSerializer = new DataContractJsonSerializer(typeof(PublishData));
jsonSerializer.WriteObject(requestStream, data);
requestStream.Seek(0, SeekOrigin.Begin);
WebClient client = new WebClient();
client.Proxy = _cachedProxy;
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.UserAgent] = _userAgent;
client.UploadProgressChanged += OnUploadProgressChanged;
client.UploadDataCompleted += OnPublishPackageCompleted;
client.UploadDataAsync(url, "POST", requestStream.ReadAllBytes(), state);
}
}
示例9: Version
public void SelfUpdate_PackageIsFound_VersionIsNewerThanTheCurrentVersion_PackageContainsExeWhichNameMatchesTheApplication_PackageContentIsWrittenToExecutable()
{
// Arrange
string exePath = "NuDeploy.exe";
var version = new Version(1, 0, 0, 0);
var userInterface = new Mock<IUserInterface>();
var applicationInformation = new ApplicationInformation { NameOfExecutable = exePath };
var packageRepositoryBrowser = new Mock<IPackageRepositoryBrowser>();
var filesystemAccessor = new Mock<IFilesystemAccessor>();
var packageContent = "Content of the update package";
var packageContentStream = TestUtilities.GetStreamReaderForText(packageContent).BaseStream;
var packageFileMock = new Mock<IPackageFile>();
packageFileMock.Setup(f => f.Path).Returns("tools\\" + exePath);
packageFileMock.Setup(f => f.GetStream()).Returns(packageContentStream);
var packageMock = new Mock<IPackage>();
packageMock.Setup(p => p.Version).Returns(new SemanticVersion(2, 0, 0, 0));
packageMock.Setup(p => p.GetFiles()).Returns(new List<IPackageFile> { packageFileMock.Object });
var targetStream = new MemoryStream();
filesystemAccessor.Setup(f => f.GetWriteStream(exePath)).Returns(targetStream);
packageRepositoryBrowser.Setup(p => p.FindPackage(It.IsAny<string>())).Returns(packageMock.Object);
var selfUpdateService = new SelfUpdateService(
userInterface.Object, applicationInformation, packageRepositoryBrowser.Object, filesystemAccessor.Object);
// Act
bool result = selfUpdateService.SelfUpdate(exePath, version);
// Assert
var bytes = targetStream.ReadAllBytes();
string newContentOfOldFile = Encoding.UTF8.GetString(bytes);
filesystemAccessor.Verify(f => f.GetWriteStream(exePath), Times.Once());
Assert.AreEqual(packageContent, newContentOfOldFile);
Assert.IsTrue(result);
}
示例10: ExtractAssembly
private Assembly ExtractAssembly(ZipEntry entry)
{
var stream = new MemoryStream();
entry.Extract(stream);
return Assembly.Load(stream.ReadAllBytes());
}
示例11: Should_Read_To_Array
public void Should_Read_To_Array()
{
var bytes = new byte[] { 1, 2, 3 };
var stream = new MemoryStream(bytes);
Assert.AreEqual(bytes, stream.ReadAllBytes());
}
示例12: Serialize
public byte[] Serialize()
{
using (var stream = new MemoryStream())
{
new BinarySerializer().Serialize(this, stream);
stream.Position = 0;
return stream.ReadAllBytes();
}
}