/// ReadFrom reads data from r until EOF and appends it to the buffer, growing /// the buffer as needed. The return value n is the number of bytes read. Any /// error except io.EOF encountered during the read is also returned. If the /// buffer becomes too large, ReadFrom will panic with ErrTooLarge. public long ReadFrom(Stream r) //func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { { var n = 0L; _lastRead = ReadOp.opInvalid; // If buffer is empty, reset to recover space. if (_off >= _buf.Length) { Reset(); } for (;;) { var free = _buf.Capacity - _buf.Length; if (free < MinRead) { // not enough space at end var newBuf = _buf; if (_off + free < MinRead) { // not enough space using beginning of buffer; // double buffer capacity newBuf = MakeSlice(2 * _buf.Capacity + MinRead); } newBuf.CopyFrom(_buf.Slice(_off)); _buf = newBuf.Slice(upper: _buf.Length - _off); _off = 0; } var bytes = new byte[_buf.Capacity - _buf.Length]; var m = r.Read(bytes, 0, bytes.Length); if (m > 0) { var slice = bytes.Slice(upper: m); _buf = _buf.AppendAll(slice); n += (long)m; } else { break; } // m, e := r.Read(b.buf[len(b.buf):cap(b.buf)]) // b.buf = b.buf[0 : len(b.buf)+m] // n += int64(m) // if e == io.EOF { // break // } // if e != nil { // return n, e // } } return(n); //return n, nil // err is EOF, so return nil explicitly }
/// ReadBytes reads until the first occurrence of delim in the input, /// returning a slice containing the data up to and including the delimiter. /// If ReadBytes encounters an error before finding a delimiter, /// it returns the data read before the error and the error itself (often io.EOF). /// ReadBytes returns err != nil if and only if the returned data does not end in /// delim. public (slice <byte> line, bool eof) ReadBytes(byte delim) { var line = new slice <byte>(); var(slice, eof) = ReadSlice(delim); // return a copy of slice. The buffer's backing array may // be overwritten by later calls. line = line.AppendAll(slice); return(line, eof); }