1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00
This commit is contained in:
Darien Raymond 2018-02-13 11:15:04 +01:00
parent 31d1fb6cc3
commit ccb2a9f168
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 14 additions and 7 deletions

View File

@ -36,6 +36,7 @@ func (h *copyHandler) writeTo(writer Writer, mb MultiBuffer) error {
return err
}
// SizeCounter is for counting bytes copied by Copy().
type SizeCounter struct {
Size int64
}

View File

@ -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 {
return &seqWriter{
writer: writer,

View File

@ -12,6 +12,7 @@ type BytesToBufferReader struct {
buffer []byte
}
// NewBytesToBufferReader returns a new BytesToBufferReader.
func NewBytesToBufferReader(reader io.Reader) Reader {
return &BytesToBufferReader{
Reader: reader,
@ -52,19 +53,14 @@ func (r *BytesToBufferReader) ReadMultiBuffer() (MultiBuffer, error) {
return nil, err
}
var (
_ Reader = (*BufferedReader)(nil)
_ io.Reader = (*BufferedReader)(nil)
_ io.ByteReader = (*BufferedReader)(nil)
_ io.WriterTo = (*BufferedReader)(nil)
)
// BufferedReader is a Reader that keeps its internal buffer.
type BufferedReader struct {
stream Reader
leftOver MultiBuffer
buffered bool
}
// NewBufferedReader returns a new BufferedReader.
func NewBufferedReader(reader Reader) *BufferedReader {
return &BufferedReader{
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) {
r.buffered = f
}
// IsBuffered returns true if internal buffer is used.
func (r *BufferedReader) IsBuffered() bool {
return r.buffered
}
// ReadByte implements io.ByteReader.
func (r *BufferedReader) ReadByte() (byte, error) {
var b [1]byte
_, err := r.Read(b[:])
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) {
if r.leftOver != nil {
nBytes, _ := r.leftOver.Read(b)
@ -113,6 +113,7 @@ func (r *BufferedReader) Read(b []byte) (int, error) {
return 0, err
}
// ReadMultiBuffer implements Reader.
func (r *BufferedReader) ReadMultiBuffer() (MultiBuffer, error) {
if r.leftOver != nil {
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) {
nBytes, err := r.writeToInternal(writer)
if errors.Cause(err) == io.EOF {

View File

@ -11,6 +11,7 @@ type BufferToBytesWriter struct {
io.Writer
}
// NewBufferToBytesWriter returns a new BufferToBytesWriter.
func NewBufferToBytesWriter(writer io.Writer) *BufferToBytesWriter {
return &BufferToBytesWriter{
Writer: writer,
@ -49,6 +50,7 @@ func NewBufferedWriter(writer Writer) *BufferedWriter {
}
}
// WriteByte implements io.ByteWriter.
func (w *BufferedWriter) WriteByte(c byte) error {
_, err := w.Write([]byte{c})
return err
@ -121,6 +123,7 @@ func (w *BufferedWriter) Flush() error {
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 {
w.buffered = f
if !f {