/// <summary> /// Removes the specified characters from the message. /// </summary> /// <param name="pipelineContext">Pipeline context</param> /// <param name="inputMessage">Input message</param> /// <returns>Original input message</returns> protected override IBaseMessage Execute(IPipelineContext pipelineContext, IBaseMessage inputMessage) { if (inputMessage.BodyPart != null) { if (this.SourceEncoding != this.TargetEncoding) { TraceProvider.Logger.TraceInfo("Transcoding message stream from '{0}' to '{1}'", this.SourceEncoding.WebName, this.TargetEncoding.WebName); // assign a new TranscodingStream to the incoming message Stream originalStream = inputMessage.BodyPart.GetOriginalDataStream(); Stream transcodeStream = new TranscodingStream(originalStream, this.SourceEncoding, this.TargetEncoding, this.BufferSize); // return the message for downstream pipeline components (further down in the pipeline) inputMessage.BodyPart.Data = transcodeStream; pipelineContext.ResourceTracker.AddResource(transcodeStream); } else { TraceProvider.Logger.TraceInfo("Source and target encoding are the same. Skip transcoding: Souce = '{0}', Target = '{1}'", this.SourceEncoding.WebName, this.TargetEncoding.WebName); } } else { TraceProvider.Logger.TraceInfo("Message has no body part, exiting"); } // return original message return(inputMessage); }
public void Decode_And_Encode_Same_Encoding_UTF8() { const string value = "ABCDEFGHØþ"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); var output = new byte[bytesUtf8.Length]; Stream after = new TranscodingStream(new MemoryStream(bytesUtf8), Encoding.UTF8, Encoding.UTF8); var count = after.Read(output, 0, 40); Assert.IsTrue(ByteArrayCompare(output, bytesUtf8)); }
public void MultiByteCharactersInSourceStreamDoNotGetLostStreaming() { const string value = "Øþ"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); var expected = new byte[] { 0xD8, 0xFE }; Stream after = new TranscodingStream(new MemoryStream(bytesUtf8), Encoding.GetEncoding(1252), Encoding.UTF8); Assert.IsTrue(ByteArrayCompare(StreamToArray(after), expected)); }
public void Decode_1252_And_Encode_UTF32() { const string value = "AØþ"; var bytesUtf32 = Encoding.UTF32.GetBytes(value); var bytes1252 = Encoding.GetEncoding(1252).GetBytes(value); var output = new byte[bytesUtf32.Length]; Stream after = new TranscodingStream(new MemoryStream(bytes1252), Encoding.UTF32, Encoding.GetEncoding(1252)); Assert.IsTrue(ByteArrayCompare(StreamToArray(after), bytesUtf32)); }
public void NoFullRead2() { const string value = "BAA"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); var expected = new byte[] { 0x00, 0x42 }; var output = new byte[2]; Stream after = new TranscodingStream(new MemoryStream(bytesUtf8), Encoding.UTF8, Encoding.UTF8); var count = after.Read(output, 1, 1); Assert.IsTrue(ByteArrayCompare(output, expected)); }
public void EmptyWrite() { const string value = ""; var bytesUtf8 = Encoding.UTF8.GetBytes(value); var expected = new byte[] { 0x41 }; var output = new byte[9]; Stream after = new TranscodingStream(new MemoryStream(bytesUtf8), Encoding.GetEncoding(1252), Encoding.UTF8); var count = after.Read(output, 0, 1); Assert.AreEqual(0, after.Read(output, 0, 1)); }
public void IdisposableTest2() { const string value = "BAA"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); Stream inStream = new MemoryStream(bytesUtf8); Stream after = new TranscodingStream(inStream, Encoding.UTF8, Encoding.UTF8); after.Close(); var length = inStream.Length; }
public void SourceStreamIsShorterThanDestinationStream() { const string value = "A"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); var expected = new byte[100]; expected[0] = 0x41; var output = new byte[100]; Stream after = new TranscodingStream(new MemoryStream(bytesUtf8), Encoding.GetEncoding(1252), Encoding.UTF8); var count = after.Read(output, 0, 100); Assert.IsTrue(ByteArrayCompare(output, expected)); }
public void IdisposableTest() { const string value = "BAA"; var bytesUtf8 = Encoding.UTF8.GetBytes(value); Stream inStream = new MemoryStream(bytesUtf8); using (Stream after = new TranscodingStream(inStream, Encoding.UTF8, Encoding.UTF8)) { while (after.ReadByte() != -1) { } } var length = inStream.Length; }