From 6a4942526704f2d60783cfce130ac6f46591d7de Mon Sep 17 00:00:00 2001 From: JamesNK Date: Tue, 22 Mar 2011 05:48:52 +0000 Subject: [PATCH] -Changed BsonReader/BsonWriter to close the underlying stream by default when closed -Added CloseInput/CloseOutput to JsonReader/JsonWriter to control whether the underlying stream is closed. Default to true --- .../Bson/BsonReaderTests.cs | 18 ++++++++++++++++++ .../Bson/BsonWriterTests.cs | 18 ++++++++++++++++++ .../JsonTextReaderTest.cs | 18 ++++++++++++++++++ .../JsonTextWriterTest.cs | 18 ++++++++++++++++++ Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs | 5 +++++ Src/Newtonsoft.Json/Bson/BsonReader.cs | 8 ++++++++ Src/Newtonsoft.Json/Bson/BsonWriter.cs | 8 ++++++++ Src/Newtonsoft.Json/JsonReader.cs | 13 +++++++++++++ Src/Newtonsoft.Json/JsonTextReader.cs | 2 +- Src/Newtonsoft.Json/JsonTextWriter.cs | 3 ++- Src/Newtonsoft.Json/JsonWriter.cs | 14 +++++++++++++- 11 files changed, 122 insertions(+), 3 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs index 3f457ad0..a5c3c1dd 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs @@ -40,6 +40,24 @@ namespace Newtonsoft.Json.Tests.Bson { private const char Euro = '\u20ac'; + [Test] + public void CloseInput() + { + MemoryStream ms = new MemoryStream(); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(ms.CanRead); + reader.Close(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + reader = new BsonReader(ms) { CloseInput = false }; + + Assert.IsTrue(ms.CanRead); + reader.Close(); + Assert.IsTrue(ms.CanRead); + } + [Test] public void ReadSingleObject() { diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs index d8e38cae..49c531d7 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs @@ -38,6 +38,24 @@ namespace Newtonsoft.Json.Tests.Bson { public class BsonWriterTests : TestFixtureBase { + [Test] + public void CloseOutput() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + Assert.IsTrue(ms.CanRead); + writer.Close(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + writer = new BsonWriter(ms) { CloseOutput = false }; + + Assert.IsTrue(ms.CanRead); + writer.Close(); + Assert.IsTrue(ms.CanRead); + } + [Test] public void WriteSingleObject() { diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index f80566fc..e0156daa 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -35,6 +35,24 @@ namespace Newtonsoft.Json.Tests { public class JsonTextReaderTest : TestFixtureBase { + [Test] + public void CloseInput() + { + MemoryStream ms = new MemoryStream(); + JsonTextReader reader = new JsonTextReader(new StreamReader(ms)); + + Assert.IsTrue(ms.CanRead); + reader.Close(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + reader = new JsonTextReader(new StreamReader(ms)) { CloseInput = false }; + + Assert.IsTrue(ms.CanRead); + reader.Close(); + Assert.IsTrue(ms.CanRead); + } + [Test] public void YahooFinance() { diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index e94ef7a9..0793cb61 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -34,6 +34,24 @@ namespace Newtonsoft.Json.Tests { public class JsonTextWriterTest : TestFixtureBase { + [Test] + public void CloseOutput() + { + MemoryStream ms = new MemoryStream(); + JsonTextWriter writer = new JsonTextWriter(new StreamWriter(ms)); + + Assert.IsTrue(ms.CanRead); + writer.Close(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + writer = new JsonTextWriter(new StreamWriter(ms)) { CloseOutput = false }; + + Assert.IsTrue(ms.CanRead); + writer.Close(); + Assert.IsTrue(ms.CanRead); + } + [Test] public void ValueFormatting() { diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs index 7c0d476e..c9ee4bdd 100644 --- a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs @@ -30,6 +30,11 @@ namespace Newtonsoft.Json.Bson _writer.Flush(); } + public void Close() + { + _writer.Close(); + } + public void WriteToken(BsonToken t) { CalculateSize(t); diff --git a/Src/Newtonsoft.Json/Bson/BsonReader.cs b/Src/Newtonsoft.Json/Bson/BsonReader.cs index b1873377..fa18d617 100644 --- a/Src/Newtonsoft.Json/Bson/BsonReader.cs +++ b/Src/Newtonsoft.Json/Bson/BsonReader.cs @@ -242,6 +242,14 @@ namespace Newtonsoft.Json.Bson } } + public override void Close() + { + base.Close(); + + if (CloseInput && _reader != null) + _reader.Close(); + } + private bool ReadCodeWScope() { switch (_bsonReaderState) diff --git a/Src/Newtonsoft.Json/Bson/BsonWriter.cs b/Src/Newtonsoft.Json/Bson/BsonWriter.cs index d3a380e8..4260c90c 100644 --- a/Src/Newtonsoft.Json/Bson/BsonWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonWriter.cs @@ -156,6 +156,14 @@ namespace Newtonsoft.Json.Bson _propertyName = name; } + public override void Close() + { + base.Close(); + + if (CloseOutput && _writer != null) + _writer.Close(); + } + private void AddParent(BsonToken container) { AddToken(container); diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index cf77c5d5..d282a2a6 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -117,6 +117,16 @@ namespace Newtonsoft.Json private readonly List _stack; + /// + /// Gets or sets a value indicating whether the underlying stream or + /// should be closed when the reader is closed. + /// + /// + /// true to close the underlying stream or when + /// the reader is closed; otherwise false. The default is true. + /// + public bool CloseInput { get; set; } + /// /// Gets the quotation mark character used to enclose the value of a string. /// @@ -173,6 +183,9 @@ namespace Newtonsoft.Json { _currentState = State.Start; _stack = new List(); + + CloseInput = true; + Push(JTokenType.None); } diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 416a3c7e..def46e15 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -991,7 +991,7 @@ namespace Newtonsoft.Json { base.Close(); - if (_reader != null) + if (CloseInput && _reader != null) _reader.Close(); if (_buffer != null) diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 0a083e0b..02aa35a9 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -134,7 +134,8 @@ namespace Newtonsoft.Json { base.Close(); - _writer.Close(); + if (CloseOutput && _writer != null) + _writer.Close(); } /// diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 374a9c9b..864d99af 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -126,6 +126,16 @@ namespace Newtonsoft.Json private State _currentState; private Formatting _formatting; + /// + /// Gets or sets a value indicating whether the underlying stream or + /// should be closed when the writer is closed. + /// + /// + /// true to close the underlying stream or when + /// the writer is closed; otherwise false. The default is true. + /// + public bool CloseOutput { get; set; } + /// /// Gets the top. /// @@ -179,12 +189,14 @@ namespace Newtonsoft.Json /// /// Creates an instance of the JsonWriter class. /// - public JsonWriter() + protected JsonWriter() { _stack = new List(8); _stack.Add(JTokenType.None); _currentState = State.Start; _formatting = Formatting.None; + + CloseOutput = true; } private void Push(JTokenType value)