mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
more doc
This commit is contained in:
parent
4bb1d822af
commit
e47dd29ed1
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DefaultOffset = 16
|
defaultOffset = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
|
// 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
|
// Clear clears the content of the buffer, results an empty buffer with
|
||||||
// Len() = 0.
|
// Len() = 0.
|
||||||
func (b *Buffer) Clear() *Buffer {
|
func (b *Buffer) Clear() *Buffer {
|
||||||
b.offset = DefaultOffset
|
b.offset = defaultOffset
|
||||||
b.Value = b.head[b.offset:b.offset]
|
b.Value = b.head[b.offset:b.offset]
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
@ -58,6 +58,7 @@ func (b *Buffer) Prepend(data []byte) *Buffer {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes returns the content bytes of this Buffer.
|
||||||
func (b *Buffer) Bytes() []byte {
|
func (b *Buffer) Bytes() []byte {
|
||||||
return b.Value
|
return b.Value
|
||||||
}
|
}
|
||||||
@ -74,6 +75,8 @@ func (b *Buffer) SliceFrom(from int) *Buffer {
|
|||||||
return b
|
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 {
|
func (b *Buffer) SliceBack(offset int) *Buffer {
|
||||||
newoffset := b.offset - offset
|
newoffset := b.offset - offset
|
||||||
if newoffset < 0 {
|
if newoffset < 0 {
|
||||||
@ -103,6 +106,7 @@ func (b *Buffer) Write(data []byte) (int, error) {
|
|||||||
return len(data), nil
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read implements io.Reader.Read().
|
||||||
func (b *Buffer) Read(data []byte) (int, error) {
|
func (b *Buffer) Read(data []byte) (int, error) {
|
||||||
if b.Len() == 0 {
|
if b.Len() == 0 {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
@ -144,8 +148,8 @@ func (p *bufferPool) allocate() *Buffer {
|
|||||||
return &Buffer{
|
return &Buffer{
|
||||||
head: b,
|
head: b,
|
||||||
pool: p,
|
pool: p,
|
||||||
Value: b[DefaultOffset:],
|
Value: b[defaultOffset:],
|
||||||
offset: DefaultOffset,
|
offset: defaultOffset,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
// Package common contains common utilities that are shared among other packages.
|
// Package common contains common utilities that are shared among other packages.
|
||||||
// See each sub-package for detail.
|
// See each sub-package for detail.
|
||||||
|
|
||||||
package common
|
package common
|
||||||
|
@ -17,16 +17,20 @@ func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) {
|
|||||||
return buffer, err
|
return buffer, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reader extends io.Reader with alloc.Buffer.
|
||||||
type Reader interface {
|
type Reader interface {
|
||||||
|
// Read reads content from underlying reader, and put it into an alloc.Buffer.
|
||||||
Read() (*alloc.Buffer, error)
|
Read() (*alloc.Buffer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AdaptiveReader is a Reader that adjusts its reading speed automatically.
|
||||||
type AdaptiveReader struct {
|
type AdaptiveReader struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
allocate func() *alloc.Buffer
|
allocate func() *alloc.Buffer
|
||||||
isLarge bool
|
isLarge bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewAdaptiveReader creates a new AdaptiveReader.
|
||||||
func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
|
func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
|
||||||
return &AdaptiveReader{
|
return &AdaptiveReader{
|
||||||
reader: reader,
|
reader: reader,
|
||||||
@ -35,6 +39,7 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read implements Reader.Read().
|
||||||
func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
|
func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
|
||||||
buffer, err := ReadFrom(this.reader, this.allocate())
|
buffer, err := ReadFrom(this.reader, this.allocate())
|
||||||
|
|
||||||
|
@ -6,20 +6,25 @@ import (
|
|||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"github.com/v2ray/v2ray-core/common/alloc"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Writer extends io.Writer with alloc.Buffer.
|
||||||
type Writer interface {
|
type Writer interface {
|
||||||
|
// Write writes an alloc.Buffer into underlying writer.
|
||||||
Write(*alloc.Buffer) error
|
Write(*alloc.Buffer) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
|
||||||
type AdaptiveWriter struct {
|
type AdaptiveWriter struct {
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewAdaptiveWriter creates a new AdaptiveWriter.
|
||||||
func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter {
|
func NewAdaptiveWriter(writer io.Writer) *AdaptiveWriter {
|
||||||
return &AdaptiveWriter{
|
return &AdaptiveWriter{
|
||||||
writer: writer,
|
writer: writer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write implements Writer.Write().
|
||||||
func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
|
func (this *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
|
||||||
nBytes, err := this.writer.Write(buffer.Value)
|
nBytes, err := this.writer.Write(buffer.Value)
|
||||||
if nBytes < buffer.Len() {
|
if nBytes < buffer.Len() {
|
||||||
|
Loading…
Reference in New Issue
Block a user