Skip to content

Commit

Permalink
Closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
andreashuber-lawo committed Apr 4, 2016
1 parent e6e5eea commit 3c86535
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Lawo.EmberPlusSharp/Ember/EmberWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public void WriteEndContainer()
WriteLength(this.writeBuffer, 0, 0, 1);
}

/// <summary>Flushes the internal buffer into the stream passed to the constructor.</summary>
public void Flush()
{
this.AssertNotDisposed();
this.writeBuffer.Flush();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

private void AssertNotDisposed()
Expand Down
5 changes: 0 additions & 5 deletions Lawo.EmberPlusSharp/Model/EmberReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace Lawo.EmberPlusSharp.Model

internal static class EmberReaderExtensions
{
internal static void AssertRead(this EmberReader reader)
{
AssertRead(reader, null);
}

internal static bool AssertAndReadContentsAsBoolean(this EmberReader reader)
{
return AssertAndReadContents(reader, r => r.ReadContentsAsBoolean());
Expand Down
40 changes: 27 additions & 13 deletions Lawo.EmberPlusSharp/Model/NodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Lawo.EmberPlusSharp.Model
{
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -319,24 +320,37 @@ private void ReadChildContents(
}
else
{
reader.AssertRead();

if (reader.CanReadContents && (reader.OuterId == GlowNodeContents.Identifier.OuterId))
using (var stream = new MemoryStream())
using (var writer = new EmberWriter(stream))
{
var context = new Context(this, number, reader.AssertAndReadContentsAsString());
child = this.ReadNewChildContents(reader, actualType, context, out childRequestState);
// Since EmberReader checks that every end of a container is matched by a start, we need to write
// this dummy here.
writer.WriteStartSet(GlowNode.Contents.OuterId);
var identifier = reader.CopyToEndContainer(writer, GlowNodeContents.Identifier.OuterId) as string;

if (identifier != null)
{
writer.Flush();
stream.Position = 0;

if (child != null)
using (var contentsReader = new EmberReader(stream))
{
contentsReader.Read(); // Read what we have written with WriteStartSet above
var context = new Context(this, number, identifier);
child = this.ReadNewChildContents(contentsReader, actualType, context, out childRequestState);

if (child != null)
{
this.children.Add(number, child);
}
}
}
else
{
this.children.Add(number, child);
childRequestState = RequestState.Complete;
child = null;
}
}
else
{
reader.SkipToEndContainer();
childRequestState = RequestState.Complete;
child = null;
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions Lawo/IO/WriteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ public WriteBuffer(WriteAsyncCallback writeAsync, int bufferSize) : base(bufferS
public bool Flush()
{
var count = this.Count;
this.Count = 0; // Make sure that the buffer is empty even if write throws
this.write(this.GetBuffer(), 0, count);

// Prevent call to this.write when buffer is empty so that there is no error when the underlying stream has
// already been disposed.
if (count > 0)
{
this.Count = 0; // Make sure that the buffer is empty even if write throws
this.write(this.GetBuffer(), 0, count);
}

return true;
}

Expand Down
9 changes: 6 additions & 3 deletions LawoTest/IO/BufferTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,14 @@ await AssertThrowAsync<InvalidOperationException>(
() => writeBuffer.WriteAsync(new byte[3], 0, 3, CancellationToken.None));

var asyncWriteBuffer = new WriteBuffer((WriteAsyncCallback)stream.WriteAsync, 1);
asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
AssertThrow<InvalidOperationException>(() => asyncWriteBuffer.Flush());
asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
AssertThrow<InvalidOperationException>(
() => asyncWriteBuffer.Reserve(2), () => asyncWriteBuffer.Write(new byte[3], 0, 3));
asyncWriteBuffer[asyncWriteBuffer.Count++] = 42;
var str = "Hello";
AssertThrow<InvalidOperationException>(
() => asyncWriteBuffer.Flush(),
() => asyncWriteBuffer.Reserve(2),
() => asyncWriteBuffer.Write(new byte[3], 0, 3),
() => asyncWriteBuffer.WriteAsUtf8(str, Encoding.UTF8.GetByteCount(str)));
}
});
Expand Down

0 comments on commit 3c86535

Please sign in to comment.