private static Task <BString> ReadStringAsync(PeekableReader reader) { var lengthBuilder = new StringBuilder(); while (reader.PeekByte() != ':') { lengthBuilder.Append((char)reader.ReadByte()); } Expect(reader, ':'); var length = Convert.ToInt32(lengthBuilder.ToString()); var buffer = new byte[length]; for (var i = 0; i < length; i++) { var b = reader.ReadByte(); if (b < 0) { throw new Exception("Unexpected end of stream"); } buffer[i] = (byte)b; } return(Task.FromResult(new BString(buffer))); }
private static void Expect(PeekableReader reader, char expectedChar) { var foundChar = reader.ReadByte(); if (foundChar < 0) { throw new Exception("Unexpected end of stream"); } if (foundChar != expectedChar) { throw new Exception("Unexpected character: " + foundChar); } }
private static Task <BNumber> ReadNumberAsync(PeekableReader reader) { Expect(reader, 'i'); var builder = new StringBuilder(); while (reader.PeekByte() != 'e') { builder.Append((char)reader.ReadByte()); } Expect(reader, 'e'); var num = Convert.ToInt64(builder.ToString()); return(Task.FromResult(new BNumber(num))); }