Restarted some stuff so I can do it better.

Made a reader that can reade across data blocks if necessary
Still can't get things to read right
This commit is contained in:
Caleb Gardner
2020-11-13 16:11:44 -06:00
parent dbf7e9465a
commit 06b188d53c
20 changed files with 802 additions and 248 deletions
+60
View File
@@ -0,0 +1,60 @@
package bytereadwrite
import "io"
//ByteReaderWriter allows you to read to and from a byte slice. When writing, it expands the slice to accomodate any data.
type ByteReaderWriter struct {
byts []byte
offset int
}
//NewByteReaderWriter creates a ByteReaderWriter with an internal byte slice of the given length.
func NewByteReaderWriter() *ByteReaderWriter {
return &ByteReaderWriter{
byts: make([]byte, 0),
offset: 0,
}
}
//NewByteReaderWriter creates a ByteReaderWriter with an internal byte slice of the given length.
func NewByteReaderWriterWithLength(length int) *ByteReaderWriter {
return &ByteReaderWriter{
byts: make([]byte, length),
offset: 0,
}
}
//NewByteReaderWriterFromBytes creates a new ByteReaderWriter initialized with the given bytes
func NewByteReaderWriterFromBytes(byts []byte) *ByteReaderWriter {
return &ByteReaderWriter{
byts: byts,
offset: 0,
}
}
//GetBytes return the underlyting byte slice of the readerwriter
func (bwr *ByteReaderWriter) GetBytes() []byte {
return bwr.byts
}
//Read reads the bytes.
func (bwr *ByteReaderWriter) Read(byt []byte) (int, error) {
if len(bwr.byts) < bwr.offset+len(byt) {
bytesWritten := len(bwr.byts) - bwr.offset
for i := 0; i < bytesWritten; i++ {
byt[i] = bwr.byts[i+bwr.offset]
}
return bytesWritten, io.EOF
}
for i := 0; i < len(byt); i++ {
byt[i] = bwr.byts[bwr.offset+i]
}
bwr.offset += len(byt)
return len(byt), nil
}
//Write writes to the end of the bytes. WILL expand to accept the incoming bytes.
func (bwr *ByteReaderWriter) Write(byts []byte) (int, error) {
bwr.byts = append(bwr.byts, byts...)
return len(byts), nil
}