Skip to content

Commit

Permalink
feat: spilit ttheader codec logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantxie committed Aug 6, 2024
1 parent 969ae87 commit 5f7a997
Show file tree
Hide file tree
Showing 7 changed files with 1,156 additions and 0 deletions.
129 changes: 129 additions & 0 deletions kio/bytebuffer.go
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
}
Loading

0 comments on commit 5f7a997

Please sign in to comment.