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()); } }
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)); }
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; }
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(); }
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(); }
/// <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); }
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); } } }
public void CanReadZeroByteStream() { var stream = new MemoryStream(); var bytes = stream.ToByteArray(1000); bytes.Length.Should().Be(0); }
public void CanReadTwoByteStream() { var stream = new MemoryStream(new byte[] { 0x30, 143 }); var bytes = stream.ToByteArray(1000); bytes.Should().Have.SameSequenceAs<byte>(0x30, 143); }
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); }
/// <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); }
/// <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); }
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(); }