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));
}