本文整理汇总了C#中System.IO.MemoryStream.ToByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# MemoryStream.ToByteArray方法的具体用法?C# MemoryStream.ToByteArray怎么用?C# MemoryStream.ToByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.MemoryStream
的用法示例。
在下文中一共展示了MemoryStream.ToByteArray方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtensionMethods_ToByteArray_WhenValidString_ExpectValidBytes
public void ExtensionMethods_ToByteArray_WhenValidString_ExpectValidBytes()
{
//------------Setup for test--------------------------
const string input = "test message";
var bytes = Encoding.UTF8.GetBytes(input);
//------------Execute Test---------------------------
using(Stream s = new MemoryStream(bytes))
{
var result = s.ToByteArray();
//------------Assert Results-------------------------
Assert.AreEqual(result.ToString(), bytes.ToString());
}
}
示例2: DecryptToStream_ReturnsExpectedDecryptedStream_WhenFileParamIsValid
public void DecryptToStream_ReturnsExpectedDecryptedStream_WhenFileParamIsValid()
{
//Arrange
byte[] expected = TestConstants.UnencryptedData;
byte[] actual;
//Act
using (var memStream = new MemoryStream())
{
_streamEncryptor.DecryptToStream(_encryptedFile, memStream);
actual = memStream.ToByteArray();
}
//Assert
Assert.IsTrue(actual.SequenceEqual(expected));
}
示例3: Export
public byte[] Export()
{
byte[] output;
using (MemoryStream mem = new MemoryStream())
using (BinaryWriter b = new BinaryWriter(mem))
{
b.Write(Rotation.X);
b.Write(Rotation.Y);
b.Write(Rotation.Z);
b.Write(Zoom1);
b.Write(Zoom2);
b.Write(Position.X);
b.Write(Position.Y);
b.Write(Position.Z);
output = mem.ToByteArray();
}
return output;
}
示例4: Zip
public static byte[] Zip( string fileName, byte[] fileData )
{
var packageStream = new MemoryStream();
using (Package package = Package.Open(packageStream, FileMode.Create))
{
string destFilename = ".\\" + fileName;
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
PackagePart part = package.CreatePart(uri, "", CompressionOption.Maximum);
using (var appendFile = new MemoryStream(fileData))
{
using (Stream destinationPartStream = part.GetStream())
{
CopyStream(appendFile, destinationPartStream);
}
}
}
return packageStream.ToByteArray();
}
示例5: WriteObjectContent
/// <summary>
/// Writes only the content of the object to the XML document or stream using the specified <see cref="T:System.Xml.XmlDictionaryWriter" />.
/// </summary>
/// <param name="writer">An <see cref="T:System.Xml.XmlDictionaryWriter" /> used to write the XML document or stream.</param>
/// <param name="graph">The object that contains the content to write.</param>
public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
{
Argument.IsNotNull("writer", writer);
var memoryStream = new MemoryStream();
BinarySerializerHelper.DiscoverAndSerialize(memoryStream, graph, _type);
var bytes = memoryStream.ToByteArray();
writer.WriteBase64(bytes, 0, bytes.Length);
}
示例6: WriteBody
private void WriteBody(Stream stream, SmtpMessage message)
{
var mg = message;
if (mg.IsSigned == false)
{
WriteBodyData(stream, mg);
}
else
{
Stream bodyStream = new MemoryStream();
WriteBodyData(bodyStream, mg);
var boundary = mg.ContentType.CharsetEncoding.GetBytes("--" + mg.ContentType.Boundary);
if (mg.IsEncrypted == true)
{
var signedStream = new MemoryStream();
this.WriteSignedData(signedStream, mg, bodyStream.ToByteArray(), boundary);
this.WriteEncryptedData(stream, mg, signedStream.ToByteArray());
}
else
{
this.WriteSignedData(stream, mg, bodyStream.ToByteArray(), boundary);
}
}
}
示例7: CanReadZeroByteStream
public void CanReadZeroByteStream()
{
var stream = new MemoryStream();
var bytes = stream.ToByteArray(1000);
bytes.Length.Should().Be(0);
}
示例8: CanReadTwoByteStream
public void CanReadTwoByteStream()
{
var stream = new MemoryStream(new byte[] { 0x30, 143 });
var bytes = stream.ToByteArray(1000);
bytes.Should().Have.SameSequenceAs<byte>(0x30, 143);
}
示例9: CanReadMoreBytesThanTheBuffer
public void CanReadMoreBytesThanTheBuffer()
{
var stream = new MemoryStream(Enumerable.Range(0, 256).Select(x => Convert.ToByte(x)).ToArray());
var bytes = stream.ToByteArray(2);
bytes.Should().Have.UniqueValues().And.Have.Count.EqualTo(256);
}
示例10: CreateBackup
/// <summary>
/// Creates a backup of the object property values.
/// </summary>
private void CreateBackup()
{
using (var stream = new MemoryStream())
{
var propertiesToIgnore = (from propertyData in PropertyDataManager.GetProperties(_object.GetType())
where !propertyData.Value.IncludeInBackup
select propertyData.Value.Name).ToArray();
List<PropertyValue> objectsToSerialize;
lock (_object._propertyValuesLock)
{
objectsToSerialize = _object.ConvertDictionaryToListAndExcludeNonSerializableObjects(_object._propertyBag.GetAllProperties(), propertiesToIgnore);
}
#if NET
var serializer = SerializationHelper.GetBinarySerializer(false);
serializer.Serialize(stream, objectsToSerialize);
#else
// Xml backup, create serializer without using the cache since the dictionary is used for every object, and
// we need a "this" object specific dictionary.
var serializer = SerializationHelper.GetDataContractSerializer(GetType(), objectsToSerialize.GetType(),
"backup", objectsToSerialize, false);
serializer.WriteObject(stream, objectsToSerialize);
_knownTypesForDeserialization = new List<Type>();
foreach (var objectToSerialize in objectsToSerialize)
{
if (objectToSerialize.Value != null)
{
_knownTypesForDeserialization.Add(objectToSerialize.Value.GetType());
}
}
#endif
_propertyValuesBackup = stream.ToByteArray();
}
_objectValuesBackup = new Dictionary<string, object>();
_objectValuesBackup.Add(IsDirty, _object.IsDirty);
}
示例11: CreateBackup
/// <summary>
/// Creates a backup of the object property values.
/// </summary>
private void CreateBackup()
{
using (var stream = new MemoryStream())
{
var catelTypeInfo = PropertyDataManager.GetCatelTypeInfo(_object.GetType());
var propertiesToIgnore = (from propertyData in catelTypeInfo.GetCatelProperties()
where !propertyData.Value.IncludeInBackup
select propertyData.Value.Name).ToArray();
var serializer = SerializationFactory.GetXmlSerializer();
serializer.SerializeMembers(_object, stream, propertiesToIgnore);
_propertyValuesBackup = stream.ToByteArray();
}
_objectValuesBackup = new Dictionary<string, object>();
_objectValuesBackup.Add(IsDirty, _object.IsDirty);
}
示例12: SendHttpResponse
public static void SendHttpResponse(HttpContext context, string contentToSend)
{
CompressionSupport compressionSupport = context.GetCompressionSupport();
string acceptEncoding = context.Request.Headers["Accept-Encoding"];
if (compressionSupport != CompressionSupport.None)
{
if (context.Response.Headers.AllKeys.FirstOrDefault(key=>key.Equals(HttpExtensions.ResponseCompressionHeaderKey)) == null)
context.AddCompressionHeaders(compressionSupport);
System.IO.MemoryStream stream = null;
using (stream = new System.IO.MemoryStream())
{
byte[] content = System.Text.Encoding.UTF8.GetBytes(contentToSend);
if (compressionSupport == CompressionSupport.Deflate)
{
using (System.IO.Compression.DeflateStream zip = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Compress, false))
{
zip.Write(content, 0, content.Length);
}
}
else if (compressionSupport == CompressionSupport.GZip)
{
using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Compress, false))
{
zip.Write(content, 0, content.Length);
}
}
}
context.Response.BinaryWrite(stream.ToByteArray());
}
else
{
context.Response.Write(contentToSend);
}
context.Response.Flush();
}