Implement ByteReader for ChunkReader

This commit is contained in:
世界 2023-04-29 12:44:30 +08:00
parent ed2d05ab51
commit 7662278795
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 8 additions and 104 deletions

View file

@ -104,10 +104,6 @@ func With(data []byte) *Buffer {
}
}
func (b *Buffer) Closed() bool {
return b.closed
}
func (b *Buffer) Byte(index int) byte {
return b.data[b.start+index]
}

View file

@ -2,104 +2,11 @@ package bufio
import (
"io"
"os"
"sync"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
N "github.com/sagernet/sing/common/network"
)
type BufferedReader struct {
upstream N.ExtendedReader
buffer *buf.Buffer
}
func NewBufferedReader(upstream io.Reader, buffer *buf.Buffer) *BufferedReader {
return &BufferedReader{
upstream: NewExtendedReader(upstream),
buffer: buffer,
}
}
func (r *BufferedReader) Read(p []byte) (n int, err error) {
if r.buffer == nil {
return r.upstream.Read(p)
}
if r.buffer.Closed() {
return 0, os.ErrClosed
}
if r.buffer.IsEmpty() {
r.buffer.Reset()
err = r.upstream.ReadBuffer(r.buffer)
if err != nil {
r.buffer.Release()
return
}
}
return r.buffer.Read(p)
}
func (r *BufferedReader) ReadBuffer(buffer *buf.Buffer) error {
if r.buffer == nil {
return r.upstream.ReadBuffer(buffer)
}
if r.buffer.Closed() {
return os.ErrClosed
}
var err error
if r.buffer.IsEmpty() {
r.buffer.Reset()
err = r.upstream.ReadBuffer(r.buffer)
if err != nil {
r.buffer.Release()
return err
}
}
if r.buffer.Len() > buffer.FreeLen() {
err = common.Error(buffer.ReadFullFrom(r.buffer, buffer.FreeLen()))
} else {
err = common.Error(buffer.ReadFullFrom(r.buffer, r.buffer.Len()))
}
if err != nil {
r.buffer.Release()
}
return err
}
//TODO: add CopyBuffer
/*func (r *BufferedReader) WriteTo(w io.Writer) (n int64, err error) {
if r.buffer == nil {
return Copy(w, r.upstream)
}
if r.buffer.Closed() {
return 0, os.ErrClosed
}
defer r.buffer.Release()
return CopyExtendedBuffer(NewExtendedWriter(w), NewExtendedReader(r.upstream), r.buffer)
}*/
func (r *BufferedReader) ReadCached() *buf.Buffer {
buffer := r.buffer
r.buffer = nil
return buffer
}
func (r *BufferedReader) Close() error {
r.buffer.Release()
r.buffer = nil
return nil
}
func (r *BufferedReader) Upstream() any {
return r.upstream
}
func (r *BufferedReader) ReaderReplaceable() bool {
buffer := r.buffer
return buffer == nil || buffer.Closed()
}
type BufferedWriter struct {
upstream io.Writer
buffer *buf.Buffer
@ -151,13 +58,6 @@ func (w *BufferedWriter) Fallthrough() error {
return nil
}
/*func (w *BufferedWriter) ReadFrom(r io.Reader) (n int64, err error) {
if w.buffer == nil {
return Copy(w.upstream, r)
}
return CopyExtendedBuffer(NewExtendedWriter(w), NewExtendedReader(r), w.buffer)
}*/
func (w *BufferedWriter) WriterReplaceable() bool {
return w.buffer == nil
}

View file

@ -56,6 +56,14 @@ func (c *ChunkReader) Read(p []byte) (n int, err error) {
return c.cache.Read(p)
}
func (c *ChunkReader) ReadByte() (byte, error) {
buffer, err := c.ReadChunk()
if err != nil {
return 0, err
}
return buffer.ReadByte()
}
func (c *ChunkReader) ReadChunk() (*buf.Buffer, error) {
if c.cache == nil {
c.cache = buf.NewSize(c.maxChunkSize)