2

So I haven't written the code I'm dealing right now and I'm looking how to best handle this.

Right now I have this.

public static void WriteSymbol(Stream stream, Symbol symbol)
{
    using (var streamWriter = new StreamWriter(stream))
    {
        JsonSerializer.Create(SerializerSettings).Serialize(streamWriter, symbol);                
    }
}

I'd like to be able to read the content of the stream after this is done in my test so that I can check the integration. The problem is that right now after Serialize the stream is closed and I can't read anymore from it.

I see that JsonWriter public bool CloseOutput { get; set; } but I don't see something for this static JsonSerializer.

What would be the best way to go about this? How do I prevent the Serializer from closing the stream? Is there some way I should check the content of the Stream?

NtFreX
  • 10,379
  • 2
  • 43
  • 63
ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • 2
    Are you sure that it's the serializer that's closing it? – Jeff Jul 13 '17 at 11:07
  • 3
    Possible duplicate of [Is there any way to close a StreamWriter without closing its BaseStream?](https://stackoverflow.com/questions/2666888/is-there-any-way-to-close-a-streamwriter-without-closing-its-basestream) – Jeff Jul 13 '17 at 11:09
  • 3
    I strongly suspect that `StreamWriter` is the thing closing it here... see the question that @JeffE has linked to - there's a very good chance that it will get you sorted – Marc Gravell Jul 13 '17 at 11:09
  • @MarcGravell There is constructor argument on `StreamWriter`. `LeaveOpen`. – NtFreX Jul 13 '17 at 11:13
  • @NtFreX yes, I edited that - apparently my `ConsoleApp48` that I used to test is targeting 4.0, and it didn't exist in 4.0 :) – Marc Gravell Jul 13 '17 at 11:14
  • My bad guys. Thanks for pointing it out. I forgot to check whether the StreamWriter closes it when it's being disposed but it does. I see that I can set the leaveOpen to true but I have to set the encoding and bufferSize. I see in the docs that default encoding is UTF8 so I'll set it to that. What would be the default buffer size? – ditoslav Jul 13 '17 at 11:14
  • 1
    @ditoslav according to the IL: 1024 – Marc Gravell Jul 13 '17 at 11:15

2 Answers2

5

From .net 4.5 upwards you can use the LeaveOpen constructor argument of StreamWriter.

The default buffer size used by the StreamWriter is 1024 as visible when decompiling the type.

So you can do the following.

using (var streamWriter = new StreamWriter(stream, Encoding.UTF8, 1024, true))
{
    // TODO: do something 
}
NtFreX
  • 10,379
  • 2
  • 43
  • 63
0

Try something like this:

public static void WriteSymbol(Stream stream, Symbol symbol)
{
    using (var streamWriter = new StreamWriter(stream))
    {
        JsonSerializer.Create(SerializerSettings).Serialize(streamWriter, symbol); 
        // test stream here               
    }
}

Or don't surround this call with a using statement and then close the stream outside of this method.

jlavallet
  • 1,267
  • 1
  • 12
  • 33
  • The problem with not using a `using` here is that many stream decorators / etc buffer things - if you don't fully those them down, they may not flush (even `Flush()` doesn't always actually do a full flush) – Marc Gravell Jul 13 '17 at 11:12
  • I wasn't suggesting that he not close the stream, only that he do so after testing for whatever it is he wants to test for. – jlavallet Jul 13 '17 at 11:15
  • I suppose using the `StreamWriter` only in the using statement is against the idea of the author. – NtFreX Jul 13 '17 at 11:16
  • "Or don't surround this call with a using statement and close the stream outside of this method." - you *must* also close down the writer, *at the correct time* (which will be : now) – Marc Gravell Jul 13 '17 at 11:16
  • Good point on the StreamWriter. He could create and manage that outside of the method as well. Obviously this is not optimal but the point is that the stream is being closed and he says he wants to keep it open long enough to test something. – jlavallet Jul 13 '17 at 11:21