mirror of https://github.com/v2ray/v2ray-core
comments
parent
31d1fb6cc3
commit
ccb2a9f168
|
@ -36,6 +36,7 @@ func (h *copyHandler) writeTo(writer Writer, mb MultiBuffer) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SizeCounter is for counting bytes copied by Copy().
|
||||||
type SizeCounter struct {
|
type SizeCounter struct {
|
||||||
Size int64
|
Size int64
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,7 @@ func NewWriter(writer io.Writer) Writer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSequentialWriter returns a Writer that write Buffers in a MultiBuffer sequentially.
|
||||||
func NewSequentialWriter(writer io.Writer) Writer {
|
func NewSequentialWriter(writer io.Writer) Writer {
|
||||||
return &seqWriter{
|
return &seqWriter{
|
||||||
writer: writer,
|
writer: writer,
|
||||||
|
|
|
@ -12,6 +12,7 @@ type BytesToBufferReader struct {
|
||||||
buffer []byte
|
buffer []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBytesToBufferReader returns a new BytesToBufferReader.
|
||||||
func NewBytesToBufferReader(reader io.Reader) Reader {
|
func NewBytesToBufferReader(reader io.Reader) Reader {
|
||||||
return &BytesToBufferReader{
|
return &BytesToBufferReader{
|
||||||
Reader: reader,
|
Reader: reader,
|
||||||
|
@ -52,19 +53,14 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
// BufferedReader is a Reader that keeps its internal buffer.
|
||||||
_ Reader = (*BufferedReader)(nil)
|
|
||||||
_ io.Reader = (*BufferedReader)(nil)
|
|
||||||
_ io.ByteReader = (*BufferedReader)(nil)
|
|
||||||
_ io.WriterTo = (*BufferedReader)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
type BufferedReader struct {
|
type BufferedReader struct {
|
||||||
stream Reader
|
stream Reader
|
||||||
leftOver MultiBuffer
|
leftOver MultiBuffer
|
||||||
buffered bool
|
buffered bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBufferedReader returns a new BufferedReader.
|
||||||
func NewBufferedReader(reader Reader) *BufferedReader {
|
func NewBufferedReader(reader Reader) *BufferedReader {
|
||||||
return &BufferedReader{
|
return &BufferedReader{
|
||||||
stream: reader,
|
stream: reader,
|
||||||
|
@ -72,20 +68,24 @@ func NewBufferedReader(reader Reader) *BufferedReader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetBuffered sets whether to keep the interal buffer.
|
||||||
func (r *BufferedReader) SetBuffered(f bool) {
|
func (r *BufferedReader) SetBuffered(f bool) {
|
||||||
r.buffered = f
|
r.buffered = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsBuffered returns true if internal buffer is used.
|
||||||
func (r *BufferedReader) IsBuffered() bool {
|
func (r *BufferedReader) IsBuffered() bool {
|
||||||
return r.buffered
|
return r.buffered
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadByte implements io.ByteReader.
|
||||||
func (r *BufferedReader) ReadByte() (byte, error) {
|
func (r *BufferedReader) ReadByte() (byte, error) {
|
||||||
var b [1]byte
|
var b [1]byte
|
||||||
_, err := r.Read(b[:])
|
_, err := r.Read(b[:])
|
||||||
return b[0], err
|
return b[0], err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read implements io.Reader. It reads from internal buffer first (if available) and then reads from the underlying reader.
|
||||||
func (r *BufferedReader) Read(b []byte) (int, error) {
|
func (r *BufferedReader) Read(b []byte) (int, error) {
|
||||||
if r.leftOver != nil {
|
if r.leftOver != nil {
|
||||||
nBytes, _ := r.leftOver.Read(b)
|
nBytes, _ := r.leftOver.Read(b)
|
||||||
|
@ -113,6 +113,7 @@ func (r *BufferedReader) Read(b []byte) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadMultiBuffer implements Reader.
|
||||||
func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) {
|
func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
if r.leftOver != nil {
|
if r.leftOver != nil {
|
||||||
mb := r.leftOver
|
mb := r.leftOver
|
||||||
|
@ -164,6 +165,7 @@ func (r *BufferedReader) writeToInternal(writer io.Writer) (int64, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteTo implements io.WriterTo.
|
||||||
func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error) {
|
func (r *BufferedReader) WriteTo(writer io.Writer) (int64, error) {
|
||||||
nBytes, err := r.writeToInternal(writer)
|
nBytes, err := r.writeToInternal(writer)
|
||||||
if errors.Cause(err) == io.EOF {
|
if errors.Cause(err) == io.EOF {
|
||||||
|
|
|
@ -11,6 +11,7 @@ type BufferToBytesWriter struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBufferToBytesWriter returns a new BufferToBytesWriter.
|
||||||
func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter {
|
func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter {
|
||||||
return &BufferToBytesWriter{
|
return &BufferToBytesWriter{
|
||||||
Writer: writer,
|
Writer: writer,
|
||||||
|
@ -49,6 +50,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteByte implements io.ByteWriter.
|
||||||
func (w *BufferedWriter) WriteByte(c byte) error {
|
func (w *BufferedWriter) WriteByte(c byte) error {
|
||||||
_, err := w.Write([]byte{c})
|
_, err := w.Write([]byte{c})
|
||||||
return err
|
return err
|
||||||
|
@ -121,6 +123,7 @@ func (w *BufferedWriter) Flush() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetBuffered sets whether the internal buffer is used. If set to false, Flush() will be called to clear the buffer.
|
||||||
func (w *BufferedWriter) SetBuffered(f bool) error {
|
func (w *BufferedWriter) SetBuffered(f bool) error {
|
||||||
w.buffered = f
|
w.buffered = f
|
||||||
if !f {
|
if !f {
|
||||||
|
|
Loading…
Reference in New Issue