/// <summary> /// Read a line from the byte stream /// </summary> /// <returns></returns> public async Task <string> ReadLineAsync() { byte lastChar = default(byte); int bufferDataLength = 0; // try to use the thread static buffer, usually it is enough var buffer = Buffer; while (stream.DataAvailable || await stream.FillBufferAsync()) { byte newChar = stream.ReadByteFromBuffer(); buffer[bufferDataLength] = newChar; //if new line if (lastChar == '\r' && newChar == '\n') { return(encoding.GetString(buffer, 0, bufferDataLength - 1)); } //end of stream if (newChar == '\0') { return(encoding.GetString(buffer, 0, bufferDataLength)); } bufferDataLength++; //store last char for new line comparison lastChar = newChar; if (bufferDataLength == buffer.Length) { ResizeBuffer(ref buffer, bufferDataLength * 2); } } if (bufferDataLength == 0) { return(null); } return(encoding.GetString(buffer, 0, bufferDataLength)); }
/// <summary> /// Fills the buffer asynchronous. /// </summary> /// <returns></returns> Task <bool> IBufferedStream.FillBufferAsync() { return(baseStream.FillBufferAsync()); }