generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package kio | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
) | ||
|
||
// The byte count of 32 and 16 integer values. | ||
const ( | ||
Size32 = 4 | ||
Size16 = 2 | ||
) | ||
|
||
// ByteBuffer is the core abstraction of buffer. | ||
type ByteBuffer interface { | ||
io.ReadWriter | ||
|
||
// Next reads the next n bytes sequentially and returns the original buffer. | ||
Next(n int) (p []byte, err error) | ||
|
||
// Peek returns the next n bytes without advancing the reader. | ||
Peek(n int) (buf []byte, err error) | ||
|
||
// Skip is used to skip the next few bytes quickly. It's faster than Next and doesn't cause release. | ||
Skip(n int) (err error) | ||
|
||
// Release will free the buffer. After release, buffer read by Next/Skip/Peek is invalid. | ||
// Param e is used when the buffer release depend on error. | ||
// For example, usually the write buffer will be released inside flush, | ||
// but if flush error happen, write buffer may need to be released explicitly. | ||
Release(e error) (err error) | ||
|
||
// ReadableLen returns the total length of readable buffer. | ||
// Return: -1 means unreadable. | ||
ReadableLen() (n int) | ||
|
||
// ReadLen returns the size already read. | ||
ReadLen() (n int) | ||
|
||
// ReadString is a more efficient way to read string than Next. | ||
ReadString(n int) (s string, err error) | ||
|
||
// ReadBinary like ReadString. | ||
// Returns a copy of original buffer. | ||
ReadBinary(n int) (p []byte, err error) | ||
|
||
// Malloc n bytes sequentially in the writer buffer. | ||
Malloc(n int) (buf []byte, err error) | ||
|
||
// MallocLen returns the total length of the buffer malloced. | ||
MallocLen() (length int) | ||
|
||
// WriteString is a more efficient way to write string, using the unsafe method to convert the string to []byte. | ||
WriteString(s string) (n int, err error) | ||
|
||
// WriteBinary writes the []byte directly. Callers must guarantee that the []byte doesn't change. | ||
WriteBinary(b []byte) (n int, err error) | ||
|
||
// Flush writes any malloc data to the underlying io.Writer. | ||
// The malloced buffer must be set correctly. | ||
Flush() (err error) | ||
|
||
// NewBuffer returns a new writable remote.ByteBuffer. | ||
NewBuffer() ByteBuffer | ||
// AppendBuffer appends buf to the original buffer. | ||
AppendBuffer(buf ByteBuffer) (err error) | ||
|
||
// Bytes return the backing bytes slice of this buffer | ||
Bytes() (buf []byte, err error) | ||
} | ||
|
||
// WriteByte ... | ||
func WriteByte(val byte, out ByteBuffer) error { | ||
var buf []byte | ||
var err error | ||
if buf, err = out.Malloc(1); err != nil { | ||
return err | ||
} | ||
buf[0] = val | ||
return nil | ||
} | ||
|
||
// WriteUint32 ... | ||
func WriteUint32(val uint32, out ByteBuffer) error { | ||
var buf []byte | ||
var err error | ||
if buf, err = out.Malloc(Size32); err != nil { | ||
return err | ||
} | ||
binary.BigEndian.PutUint32(buf, val) | ||
return nil | ||
} | ||
|
||
// WriteUint16 ... | ||
func WriteUint16(val uint16, out ByteBuffer) error { | ||
var buf []byte | ||
var err error | ||
if buf, err = out.Malloc(Size16); err != nil { | ||
return err | ||
} | ||
binary.BigEndian.PutUint16(buf, val) | ||
return nil | ||
} | ||
|
||
// WriteString ... | ||
func WriteString(val string, out ByteBuffer) (int, error) { | ||
strLen := len(val) | ||
if err := WriteUint32(uint32(strLen), out); err != nil { | ||
return 0, err | ||
} | ||
n, err := out.WriteString(val) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return n + 4, nil | ||
} | ||
|
||
// WriteString2BLen ... | ||
func WriteString2BLen(val string, out ByteBuffer) (int, error) { | ||
strLen := len(val) | ||
if err := WriteUint16(uint16(strLen), out); err != nil { | ||
return 0, err | ||
} | ||
n, err := out.WriteString(val) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return n + 2, nil | ||
} |
Oops, something went wrong.