public long Read(EasyBuffer sink, long byteCount) { if (byteCount < 0) { throw new ArgumentException("byteCount < 0: " + byteCount); } if (byteCount == 0) { return(0); } try { _timeout.ThrowIfReached(); Segment tail = sink.WritableSegment(1); int maxToCopy = (int)Math.Min(byteCount, Segment.SIZE - tail.Limit); int bytesRead = _in.Read(tail.Data, tail.Limit, maxToCopy); if (bytesRead <= 0) { return(-1); } tail.Limit += bytesRead; sink.Size += bytesRead; return(bytesRead); } catch (Exception e) { throw e; } }
public void Write(EasyBuffer source, long byteCount) { Util.CheckOffsetAndCount(source.Size, 0, byteCount); while (byteCount > 0) { _timeout.ThrowIfReached(); Segment head = source.Head; int toCopy = (int)Math.Min(byteCount, head.Limit - head.Pos); _out.Write(head.Data, head.Pos, toCopy); head.Pos += toCopy; byteCount -= toCopy; source.Size -= toCopy; if (head.Pos == head.Limit) { source.Head = head.Pop(); SegmentPool.Recycle(head); } } }