public string ReadExisting() { if (!this.IsOpen) { throw new InvalidOperationException(SR.GetString("Port_not_open")); } byte[] dst = new byte[this.BytesToRead]; if (this.readPos < this.readLen) { Buffer.BlockCopy(this.inBuffer, this.readPos, dst, 0, this.CachedBytesToRead); } this.internalSerialStream.Read(dst, this.CachedBytesToRead, dst.Length - this.CachedBytesToRead); System.Text.Decoder decoder = this.Encoding.GetDecoder(); int num = decoder.GetCharCount(dst, 0, dst.Length); int length = dst.Length; if (num == 0) { Buffer.BlockCopy(dst, 0, this.inBuffer, 0, dst.Length); this.readPos = 0; this.readLen = dst.Length; return(""); } do { decoder.Reset(); length--; }while (decoder.GetCharCount(dst, 0, length) == num); this.readPos = 0; this.readLen = dst.Length - (length + 1); Buffer.BlockCopy(dst, length + 1, this.inBuffer, 0, dst.Length - (length + 1)); return(this.Encoding.GetString(dst, 0, length + 1)); }
/// <summary>Read all of the text from the current position of the stream.</summary> public unsafe string ReadAllText(Stream source) { int bytesRead; while ((bytesRead = source.Read(_bytes, 0, _bytes.Length)) != 0) { int charCount = _decoder.GetChars(_bytes, 0, bytesRead, _chars, 0); _builder.Append(_chars, 0, charCount); } string s = _builder.ToString(); _builder.Clear(); _decoder.Reset(); return(s); }
// convert bytes to string using a fixed blocksize. // If something bad happens, try to recover using the // DecoderFallbackException info. private void DecoderFallbackExceptions_Convert ( char [] chars, int testno, Decoder dec, DecoderFallbackExceptionTest t, int block_size) { int charsUsed, bytesUsed; bool completed; int ce = 0; // current exception for (int c = 0; c < t.bytes.Length; ) { try { int bu = c + block_size > t.bytes.Length ? t.bytes.Length - c : block_size; dec.Convert ( t.bytes, c, bu, chars, 0, chars.Length, c + bu >= t.bytes.Length, out bytesUsed, out charsUsed, out completed); c += bytesUsed; } catch (DecoderFallbackException ex) { Assert.IsTrue ( t.eindex.Length > ce, String.Format ( "test#{0}-2-{1}#{2}: UNEXPECTED FAIL (c={3}, eIndex={4}, eBytesUnknwon={5})", testno, block_size, ce, c, ex.Index, ex.BytesUnknown.Length)); Assert.IsTrue ( ex.Index + c == t.eindex[ce], String.Format ( "test#{0}-2-{1}#{2}: Expected at {3} not {4}.", testno, block_size, ce, t.eindex[ce], ex.Index + c)); Assert.IsTrue ( ex.BytesUnknown.Length == t.elen[ce], String.Format ( "test#{0}-2-{1}#{2}: Expected BytesUnknown.Length of {3} not {4} @{5}.", testno, block_size, ce, t.elen[0], ex.BytesUnknown.Length, c)); for (int i = 0; i < ex.BytesUnknown.Length; i++) Assert.IsTrue ( ex.BytesUnknown[i] == t.bytes[ex.Index + i + c], String.Format ( "test#{0}-2-{1}#{2}: Expected byte {3:X} not {4:X} at {5}.", testno, block_size, ce, t.bytes[ex.Index + i + c], ex.BytesUnknown[i], ex.Index + i)); c += ex.BytesUnknown.Length + ex.Index; dec.Reset (); ce++; } } Assert.IsTrue ( ce == t.eindex.Length, String.Format ( "test#{0}-2-{1}: UNEXPECTED SUCCESS (expected {2} exceptions, but happened {3})", testno, block_size, t.eindex.Length, ce)); }
// try to convert the all current test's bytes with Getchars() // in only one step private void DecoderFallbackExceptions_GetChars ( char [] chars, int testno, Decoder dec, DecoderFallbackExceptionTest t) { try { dec.GetChars (t.bytes, 0, t.bytes.Length, chars, 0, true); Assert.IsTrue ( t.eindex.Length == 0, String.Format ( "test#{0}-1: UNEXPECTED SUCCESS", testno)); } catch(DecoderFallbackException ex) { Assert.IsTrue ( t.eindex.Length > 0, String.Format ( "test#{0}-1: UNEXPECTED FAIL", testno)); Assert.IsTrue ( ex.Index == t.eindex[0], String.Format ( "test#{0}-1: Expected exception at {1} not {2}.", testno, t.eindex[0], ex.Index)); Assert.IsTrue ( ex.BytesUnknown.Length == t.elen[0], String.Format ( "test#{0}-1: Expected BytesUnknown.Length of {1} not {2}.", testno, t.elen[0], ex.BytesUnknown.Length)); for (int i = 0; i < ex.BytesUnknown.Length; i++) Assert.IsTrue ( ex.BytesUnknown[i] == t.bytes[ex.Index + i], String.Format ( "test#{0}-1: expected byte {1:X} not {2:X} at {3}.", testno, t.bytes[ex.Index + i], ex.BytesUnknown[i], ex.Index + i)); dec.Reset (); } }
public override void Reset() { decoder.Reset(); }