public override int Read(byte[] buffer, int offset, int count) { CheckDisposed(); EnsureReadable(); if (_inputBuffer.Length < count) { var readBuffer = new byte[PreferredBufferSize]; var read = default(int); try { read = _innerStream.Read(readBuffer, 0, readBuffer.Length); } catch (IOException) { _ioFailed = true; throw; } if (read != 0) { if (_compression) { _inflate.OutputBuffer = _compressionBuffer; _inflate.AvailableBytesOut = _compressionBuffer.Length; _inflate.NextOut = 0; _inflate.InputBuffer = readBuffer; _inflate.AvailableBytesIn = read; _inflate.NextIn = 0; var rc = _inflate.Inflate(Ionic.Zlib.FlushType.None); if (rc != Ionic.Zlib.ZlibConstants.Z_OK) { throw new IOException($"Error '{rc}' while decompressing the data."); } if (_inflate.AvailableBytesIn != 0) { throw new IOException("Decompression buffer too small."); } readBuffer = _compressionBuffer; read = _inflate.NextOut; } _inputBuffer.AddRange(readBuffer, read); } } var dataLength = _inputBuffer.ReadInto(ref buffer, offset, count); _position += dataLength; return(dataLength); }
/// <summary> /// OnEmulation event handler /// </summary> protected override void OnEmulation() { // Are we at the end of the file? if (_reader == null || _reader.EndOfStream) { // Yes. Re-open it from the beginning FileStream stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read); _reader = new StreamReader(stream); } // Read a line from the file string line = _reader.ReadLine(); // Don't write to the buffer if it's full if (line != null) { if (ReadBuffer.Count + line.Length > ReadBuffer.Capacity) { return; } // Write the string ReadBuffer.AddRange(Encoding.ASCII.GetBytes(line)); } // Sleep #if PocketPC Thread.Sleep((int)_ReadInterval.TotalMilliseconds); #else Thread.Sleep(_readInterval); #endif }
/// <summary> /// Writes the sentence to client. /// </summary> /// <param name="sentence">The sentence.</param> protected void WriteSentenceToClient(NmeaSentence sentence) { // Get a byte array of the sentence byte[] sentenceBytes = sentence.ToByteArray(); /* Some customers were found to make an emulator, but then not actually read from it. * To prevent huge buffers, we will only write a sentence if the buffer can handle it. * Otherwise, we'll ignore it completely. */ if (ReadBuffer.Count + sentenceBytes.Length + 2 > ReadBuffer.Capacity) { return; } // Add the bytes ReadBuffer.AddRange(sentenceBytes); // Add a CrLf ReadBuffer.Add(13); ReadBuffer.Add(10); }