diff options
author | JamesNK <james@newtonking.com> | 2011-03-22 08:48:52 +0300 |
---|---|---|
committer | JamesNK <james@newtonking.com> | 2011-03-22 08:48:52 +0300 |
commit | 6a4942526704f2d60783cfce130ac6f46591d7de (patch) | |
tree | 65a7d200358daef2eafe6a2197b1e07455a8a57a | |
parent | 6f0c382ba7954b75489e1a7b5e803318d4ba21af (diff) |
-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
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs | 18 | ||||
-rw-r--r-- | Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs | 18 | ||||
-rw-r--r-- | Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs | 18 | ||||
-rw-r--r-- | Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs | 18 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs | 5 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Bson/BsonReader.cs | 8 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/Bson/BsonWriter.cs | 8 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/JsonReader.cs | 13 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/JsonTextReader.cs | 2 | ||||
-rw-r--r-- | Src/Newtonsoft.Json/JsonTextWriter.cs | 3 | ||||
-rw-r--r-- | 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 3f457ad..a5c3c1d 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderTests.cs @@ -41,6 +41,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()
{
byte[] data = MiscellaneousUtils.HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00");
diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs index d8e38ca..49c531d 100644 --- a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterTests.cs @@ -39,6 +39,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()
{
MemoryStream ms = new MemoryStream();
diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs index f80566f..e0156da 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTest.cs @@ -36,6 +36,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()
{
string input = @"{
diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index e94ef7a..0793cb6 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -35,6 +35,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()
{
StringBuilder sb = new StringBuilder();
diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs index 7c0d476..c9ee4bd 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 b187337..fa18d61 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 d3a380e..4260c90 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 cf77c5d..d282a2a 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -118,6 +118,16 @@ namespace Newtonsoft.Json private readonly List<JTokenType> _stack;
/// <summary>
+ /// Gets or sets a value indicating whether the underlying stream or
+ /// <see cref="TextReader"/> should be closed when the reader is closed.
+ /// </summary>
+ /// <value>
+ /// true to close the underlying stream or <see cref="TextReader"/> when
+ /// the reader is closed; otherwise false. The default is true.
+ /// </value>
+ public bool CloseInput { get; set; }
+
+ /// <summary>
/// Gets the quotation mark character used to enclose the value of a string.
/// </summary>
public virtual char QuoteChar
@@ -173,6 +183,9 @@ namespace Newtonsoft.Json {
_currentState = State.Start;
_stack = new List<JTokenType>();
+
+ CloseInput = true;
+
Push(JTokenType.None);
}
diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 416a3c7..def46e1 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 0a083e0..02aa35a 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();
}
/// <summary>
diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 374a9c9..864d99a 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -127,6 +127,16 @@ namespace Newtonsoft.Json private Formatting _formatting;
/// <summary>
+ /// Gets or sets a value indicating whether the underlying stream or
+ /// <see cref="TextReader"/> should be closed when the writer is closed.
+ /// </summary>
+ /// <value>
+ /// true to close the underlying stream or <see cref="TextReader"/> when
+ /// the writer is closed; otherwise false. The default is true.
+ /// </value>
+ public bool CloseOutput { get; set; }
+
+ /// <summary>
/// Gets the top.
/// </summary>
/// <value>The top.</value>
@@ -179,12 +189,14 @@ namespace Newtonsoft.Json /// <summary>
/// Creates an instance of the <c>JsonWriter</c> class.
/// </summary>
- public JsonWriter()
+ protected JsonWriter()
{
_stack = new List<JTokenType>(8);
_stack.Add(JTokenType.None);
_currentState = State.Start;
_formatting = Formatting.None;
+
+ CloseOutput = true;
}
private void Push(JTokenType value)
|