Created
March 12, 2015 09:02
-
-
Save SimonCropp/8e496a25dc08a5337ab0 to your computer and use it in GitHub Desktop.
StreamSplit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using Newtonsoft.Json; | |
| using NUnit.Framework; | |
| [TestFixture] | |
| public class Class1 | |
| { | |
| [Test] | |
| public void Foo() | |
| { | |
| File.Delete("target.json"); | |
| var source = File.OpenRead("file.json"); | |
| var target = File.Create("target.json"); | |
| var splitStream = new SplitStream(source, target); | |
| var streamReader = new JsonTextReader(new StreamReader(splitStream)); | |
| var jsonSerializer = JsonSerializer.Create(); | |
| var product = jsonSerializer.Deserialize<Product>(streamReader); | |
| Debug.WriteLine(product); | |
| } | |
| } | |
| public class SplitStream:Stream | |
| { | |
| Stream source; | |
| Stream targetToDuplicate; | |
| public SplitStream(Stream source, Stream targetToDuplicate) | |
| { | |
| this.source = source; | |
| this.targetToDuplicate = targetToDuplicate; | |
| } | |
| public override void Flush() | |
| { | |
| source.Flush(); | |
| targetToDuplicate.Flush(); | |
| } | |
| public override long Seek(long offset, SeekOrigin origin) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public override void SetLength(long value) | |
| { | |
| throw new NotImplementedException(); | |
| } | |
| public override int Read(byte[] buffer, int offset, int count) | |
| { | |
| var read = source.Read(buffer, offset, count); | |
| targetToDuplicate.Write(buffer, offset, read); | |
| return read; | |
| } | |
| public override void Write(byte[] buffer, int offset, int count) | |
| { | |
| throw new NotSupportedException(); | |
| } | |
| public override bool CanRead | |
| { | |
| get { return true; } | |
| } | |
| public override bool CanSeek | |
| { | |
| get { return false; } | |
| } | |
| public override bool CanWrite | |
| { | |
| get { return false; } | |
| } | |
| public override long Length | |
| { | |
| get { throw new NotSupportedException(); } | |
| } | |
| public override long Position | |
| { | |
| get { throw new NotSupportedException(); } | |
| set { throw new NotSupportedException(); } | |
| } | |
| } | |
| public class Product | |
| { | |
| public string Name { get; set; } | |
| public DateTime Expiry { get; set; } | |
| public string[] Sizes { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment