Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
luffyma(马林) committed Aug 18, 2016
1 parent 6a55646 commit 55edf1b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions gstac/codebyte/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type CodeByteBuffer struct {
buffer []byte
file *os.File
empty bool

// Guaranteed to correctly handle simultaneous read and write files case
position int64
}

func NewCodeByteBuffer(fileName string) (*CodeByteBuffer, error) {
Expand All @@ -23,9 +26,10 @@ func NewCodeByteBuffer(fileName string) (*CodeByteBuffer, error) {
return nil, err
}
return &CodeByteBuffer{
buffer: []byte{},
file: file,
empty: false,
buffer: []byte{},
file: file,
empty: false,
position: 0,
}, nil
}

Expand All @@ -49,15 +53,15 @@ func (buffer *CodeByteBuffer) Read(b []byte) (int, error) {
if buffer.IsEmpty() {
return 0, nil
}
cnt, err := buffer.file.Read(b)
cnt, err := buffer.file.ReadAt(b, buffer.position)
if err != nil {
if err == io.EOF {
buffer.empty = true
} else {
return cnt, err
err = nil
}
}
return cnt, nil
buffer.position += int64(cnt)
return cnt, err
}

func (buffer *CodeByteBuffer) Sync() error {
Expand Down

0 comments on commit 55edf1b

Please sign in to comment.