##### `MentoryFile` 实现了一个内存文件,其行为基本与 `os.File` 相同
##### `ByteBuffer` 实现了一个可移动读写位置、自动扩容的字节缓冲区,其行为基本与 `bytes.Buffer` 相同
```
type MentoryFile struct {
name string
buffer ByteBuffer
}
func (mf *MentoryFile) Bytes() []byte {
return mf.buffer.buffer
}
func MakeMentoryFile(name string, buffer []byte) MentoryFile {
return MentoryFile{name: name, buffer: MakeByteBuffer(buffer)}
}
func (mf *MentoryFile) Stat() (fs.FileInfo, error) {
return mf, nil
}
func (mf *MentoryFile) Read(buffer []byte) (int, error) {
return mf.buffer.Read(buffer)
}
func (mf *MentoryFile) Close() error {
return nil
}
func (mf *MentoryFile) Write(buffer []byte) (n int, err error) {
return mf.buffer.Write(buffer)
}
func (mf *MentoryFile) Seek(offset int64, whence int) (int64, error) {
return mf.buffer.Seek(offset, whence)
}
func (mf *MentoryFile) Name() string { return mf.name }
func (mf *MentoryFile) Size() int64 { return int64(mf.buffer.Len()) }
func (mf *MentoryFile) Mode() os.FileMode { return 0666 }
func (mf *MentoryFile) ModTime() time.Time { return time.Time{} }
func (mf *MentoryFile) IsDir() bool { return false }
func (mf *MentoryFile) Sys() interface{} { return nil }
type ByteBuffer struct {
buffer []byte
index int
}
func MakeByteBuffer(buffer []byte) ByteBuffer {
return ByteBuffer{
buffer: buffer,
index: 0,
}
}
func (bb *ByteBuffer) Reset() {
bb.index = 0
}
func (bb *ByteBuffer) Len() int {
return len(bb.buffer)
}
func (bb *ByteBuffer) Position() int {
return bb.index
}
func (bb *ByteBuffer) Bytes() []byte {
return bb.buffer
}
func (bb *ByteBuffer) Read(buffer []byte) (int, error) {
if len(buffer) == 0 {
return 0, nil
}
if bb.index >= bb.Len() {
return 0, io.EOF
}
// copy 会判断 buffer 的大小
last := copy(buffer, bb.buffer[bb.index:])
bb.index += last
return last, nil
}
func (bb *ByteBuffer) Write(buffer []byte) (int, error) {
bb.buffer = append(bb.buffer[:bb.index], buffer...)
bb.index += len(buffer)
return len(buffer), nil
}
func (bb *ByteBuffer) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
case io.SeekStart:
bb.index = int(offset)
case io.SeekCurrent:
bb.index += int(offset)
case io.SeekEnd:
bb.index = bb.Len() - 1 - int(offset)
}
return int64(bb.index), nil
}
```
有疑问加站长微信联系(非本文作者))