gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/utils/io/write.go (about) 1 package io 2 3 import ( 4 "encoding/binary" 5 "gitee.com/sy_183/go-common/strings/unsafe" 6 "io" 7 ) 8 9 type Writer struct { 10 Buf []byte 11 off int 12 } 13 14 func (w *Writer) Len() int { 15 return w.off 16 } 17 18 func (w *Writer) Remain() int { 19 return len(w.Buf) - w.off 20 } 21 22 func (w *Writer) Bytes() []byte { 23 return w.Buf[:w.off] 24 } 25 26 func (w *Writer) ResetBuf(buf []byte) { 27 w.Buf = buf 28 w.off = 0 29 } 30 31 func (w *Writer) Reset() { 32 w.off = 0 33 } 34 35 func (w *Writer) Seek(off int, whence int) { 36 switch whence { 37 case 0: 38 w.off = off 39 case 1: 40 w.off += off 41 case 2: 42 w.off = len(w.Buf) + off 43 } 44 if w.off < 0 { 45 w.off = 0 46 } else if w.off > len(w.Buf) { 47 w.off = len(w.Buf) 48 } 49 } 50 51 func (w *Writer) Write(p []byte) (n int, err error) { 52 return w.WriteBytes(p), nil 53 } 54 55 func (w *Writer) WriteBytes(p []byte) (n int) { 56 n = copy(w.Buf[w.off:], p) 57 w.off += n 58 return 59 } 60 61 func (w *Writer) WriteBytesAnd(p []byte) *Writer { 62 w.WriteBytes(p) 63 return w 64 } 65 66 func (w *Writer) WriteString(s string) (n int) { 67 n = copy(w.Buf[w.off:], s) 68 w.off += n 69 return 70 } 71 72 func (w *Writer) WriteStringAnd(s string) *Writer { 73 w.WriteString(s) 74 return w 75 } 76 77 func (w *Writer) WriteByte(b byte) *Writer { 78 w.Buf[w.off] = b 79 w.off++ 80 return w 81 } 82 83 func (w *Writer) WriteUint16(u uint16) *Writer { 84 binary.BigEndian.PutUint16(w.Buf[w.off:], u) 85 w.off += 2 86 return w 87 } 88 89 func (w *Writer) WriteUint32(u uint32) *Writer { 90 binary.BigEndian.PutUint32(w.Buf[w.off:], u) 91 w.off += 4 92 return w 93 } 94 95 func (w *Writer) WriteUint64(u uint64) *Writer { 96 binary.BigEndian.PutUint64(w.Buf[w.off:], u) 97 w.off += 8 98 return w 99 } 100 101 func (w *Writer) WriteLittleUint16(u uint16) *Writer { 102 binary.LittleEndian.PutUint16(w.Buf[w.off:], u) 103 w.off += 2 104 return w 105 } 106 107 func (w *Writer) WriteLittleUint32(u uint32) *Writer { 108 binary.LittleEndian.PutUint32(w.Buf[w.off:], u) 109 w.off += 4 110 return w 111 } 112 113 func (w *Writer) WriteLittleUint64(u uint64) *Writer { 114 binary.LittleEndian.PutUint64(w.Buf[w.off:], u) 115 w.off += 8 116 return w 117 } 118 119 func (w *Writer) WriterTo(wt io.Writer, np *int64) error { 120 if w.Len() > 0 { 121 if err := Write(wt, w.Bytes(), np); err != nil { 122 return err 123 } 124 } 125 return nil 126 } 127 128 func (w *Writer) WriterToAndReset(wt io.Writer, np *int64) error { 129 if w.Len() > 0 { 130 if err := Write(wt, w.Bytes(), np); err != nil { 131 return err 132 } 133 w.Reset() 134 } 135 return nil 136 } 137 138 func Write(w io.Writer, p []byte, np *int64) error { 139 if len(p) > 0 { 140 n, err := w.Write(p) 141 *np += int64(n) 142 return err 143 } 144 return nil 145 } 146 147 func WritePanic(w io.Writer, p []byte, np *int64) { 148 if err := Write(w, p, np); err != nil { 149 panic(err) 150 } 151 } 152 153 func WriteString(w io.Writer, s string, np *int64) error { 154 return Write(w, unsafe.Bytes(s), np) 155 } 156 157 func WriteStringPanic(w io.Writer, s string, np *int64) { 158 if err := WriteString(w, s, np); err != nil { 159 panic(err) 160 } 161 } 162 163 func WriteTo(wt io.WriterTo, w io.Writer, np *int64) error { 164 n, err := wt.WriteTo(w) 165 *np += n 166 return err 167 } 168 169 func WriteToPanic(wt io.WriterTo, w io.Writer, np *int64) { 170 if err := WriteTo(wt, w, np); err != nil { 171 panic(err) 172 } 173 } 174 175 func WriteAndReset(w *Writer, iow io.Writer, np *int64) error { 176 if w.Len() > 0 { 177 if err := Write(iow, w.Bytes(), np); err != nil { 178 return err 179 } 180 w.Reset() 181 } 182 return nil 183 } 184 185 func WriteAndResetPanic(w *Writer, iow io.Writer, np *int64) { 186 if err := WriteAndReset(w, iow, np); err != nil { 187 panic(err) 188 } 189 } 190 191 func HandleRecovery(e any) error { 192 if e != nil { 193 if err, is := e.(error); is { 194 return err 195 } 196 panic(e) 197 } 198 return nil 199 }