From e47dd29ed1d1d42f3c38086dfc3a05a8f526ac5b Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 6 Feb 2016 22:28:35 +0100 Subject: [PATCH] more doc --- common/alloc/buffer.go | 12 ++++++++---- common/common.go | 1 - common/io/reader.go | 5 +++++ common/io/writer.go | 5 +++++ 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 08741e24..3afab300 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -6,7 +6,7 @@ import ( ) const ( - DefaultOffset = 16 + defaultOffset = 16 ) // Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles @@ -33,7 +33,7 @@ func (b *Buffer) Release() { // Clear clears the content of the buffer, results an empty buffer with // Len() = 0. func (b *Buffer) Clear() *Buffer { - b.offset = DefaultOffset + b.offset = defaultOffset b.Value = b.head[b.offset:b.offset] return b } @@ -58,6 +58,7 @@ func (b *Buffer) Prepend(data []byte) *Buffer { return b } +// Bytes returns the content bytes of this Buffer. func (b *Buffer) Bytes() []byte { return b.Value } @@ -74,6 +75,8 @@ func (b *Buffer) SliceFrom(from int) *Buffer { return b } +// SliceBack extends the Buffer to its front by offset bytes. +// Caller must ensure cumulated offset is no more than 16. func (b *Buffer) SliceBack(offset int) *Buffer { newoffset := b.offset - offset if newoffset < 0 { @@ -103,6 +106,7 @@ func (b *Buffer) Write(data []byte) (int, error) { return len(data), nil } +// Read implements io.Reader.Read(). func (b *Buffer) Read(data []byte) (int, error) { if b.Len() == 0 { return 0, io.EOF @@ -144,8 +148,8 @@ func (p *bufferPool) allocate() *Buffer { return &Buffer{ head: b, pool: p, - Value: b[DefaultOffset:], - offset: DefaultOffset, + Value: b[defaultOffset:], + offset: defaultOffset, } } diff --git a/common/common.go b/common/common.go index 160695ba..27dc832b 100644 --- a/common/common.go +++ b/common/common.go @@ -1,4 +1,3 @@ // Package common contains common utilities that are shared among other packages. // See each sub-package for detail. - package common diff --git a/common/io/reader.go b/common/io/reader.go index 9e9204d6..5dd18fba 100644 --- a/common/io/reader.go +++ b/common/io/reader.go @@ -17,16 +17,20 @@ func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) { return buffer, err } +// Reader extends io.Reader with alloc.Buffer. type Reader interface { + // Read reads content from underlying reader, and put it into an alloc.Buffer. Read() (*alloc.Buffer, error) } +// AdaptiveReader is a Reader that adjusts its reading speed automatically. type AdaptiveReader struct { reader io.Reader allocate func() *alloc.Buffer isLarge bool } +// NewAdaptiveReader creates a new AdaptiveReader. func NewAdaptiveReader(reader io.Reader) *AdaptiveReader { return &AdaptiveReader{ reader: reader, @@ -35,6 +39,7 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader { } } +// Read implements Reader.Read(). func (this *AdaptiveReader) Read() (*alloc.Buffer, error) { buffer, err := ReadFrom(this.reader, this.allocate()) diff --git a/common/io/writer.go b/common/io/writer.go index 607b8d76..7fefa9d7 100644 --- a/common/io/writer.go +++ b/common/io/writer.go @@ -6,20 +6,25 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" ) +// Writer extends io.Writer with alloc.Buffer. type Writer interface { + // Write writes an alloc.Buffer into underlying writer. Write(*alloc.Buffer) error } +// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer. type AdaptiveWriter struct { writer io.Writer } +// NewAdaptiveWriter creates a new AdaptiveWriter. func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter { return &AdaptiveWriter{ writer: writer, } } +// Write implements Writer.Write(). func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error { nBytes, err := this.writer.Write(buffer.Value) if nBytes < buffer.Len() {