|
@@ -27,6 +27,7 @@ func BytesToInt64(bytes []byte) int64 {
|
|
|
return num
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func ioError(field string, err error) IErrorArgs {
|
|
|
return NewError(
|
|
|
"ErrIO",
|
|
@@ -39,6 +40,7 @@ func ioError(field string, err error) IErrorArgs {
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
func ReadBuf(r io.Reader, size int, field string) ([]byte, IErrorArgs) {
|
|
|
buf := make([]byte, size)
|
|
|
if _, err := r.Read(buf); err != nil {
|
|
@@ -47,6 +49,8 @@ func ReadBuf(r io.Reader, size int, field string) ([]byte, IErrorArgs) {
|
|
|
return buf, nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
func ReadBufSize(r io.Reader, lenSize int, field string) ([]byte, IErrorArgs) {
|
|
|
var size int64
|
|
|
if err := ReadInt64(r, lenSize, field+"_size", &size); err != nil {
|
|
@@ -56,6 +60,9 @@ func ReadBufSize(r io.Reader, lenSize int, field string) ([]byte, IErrorArgs) {
|
|
|
return buf, err
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
func ReadInt64(r io.Reader, size int, field string, result *int64) IErrorArgs {
|
|
|
buf := make([]byte, size)
|
|
|
if _, err := r.Read(buf); err != nil {
|
|
@@ -65,6 +72,7 @@ func ReadInt64(r io.Reader, size int, field string, result *int64) IErrorArgs {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func ReadString(r io.Reader, lenSize int, field string, result *string) IErrorArgs {
|
|
|
strBuf, err := ReadBufSize(r, lenSize, field)
|
|
|
if err != nil {
|
|
@@ -74,6 +82,7 @@ func ReadString(r io.Reader, lenSize int, field string, result *string) IErrorAr
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func ReadByte(r io.Reader, field string, result *byte) IErrorArgs {
|
|
|
buf, err := ReadBuf(r, 1, field)
|
|
|
if err != nil {
|
|
@@ -85,6 +94,7 @@ func ReadByte(r io.Reader, field string, result *byte) IErrorArgs {
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
func WriteBuf(w io.Writer, buf []byte, field string) IErrorArgs {
|
|
|
if _, err := w.Write(buf); err != nil {
|
|
|
return ioError(field, err)
|
|
@@ -92,11 +102,15 @@ func WriteBuf(w io.Writer, buf []byte, field string) IErrorArgs {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func WriteInt64(w io.Writer, val int64, size int, field string) IErrorArgs {
|
|
|
buf := Int64ToBytes(val, size)
|
|
|
return WriteBuf(w, buf, field)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
func WriteString(w io.Writer, val, field string, lenSize int) IErrorArgs {
|
|
|
|
|
|
if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
|
|
@@ -109,10 +123,14 @@ func WriteString(w io.Writer, val, field string, lenSize int) IErrorArgs {
|
|
|
return WriteBuf(w, []byte(val), field)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
func WriteByte(w io.Writer, val byte, field string) IErrorArgs {
|
|
|
return WriteBuf(w, []byte{val}, field)
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
func WriteBufSize(w io.Writer, val []byte, lenSize int, field string) IErrorArgs {
|
|
|
|
|
|
if err := WriteInt64(w, int64(len(val)), lenSize, field+"_size"); err != nil {
|