Browse Source

Added BufferCloser

pull/157/head
Jae Kwon 9 years ago
parent
commit
14356a7739
2 changed files with 52 additions and 0 deletions
  1. +51
    -0
      common/io.go
  2. +1
    -0
      process/process.go

+ 51
- 0
common/io.go View File

@ -1,6 +1,8 @@
package common
import (
"bytes"
"errors"
"io"
)
@ -22,3 +24,52 @@ func (pr *PrefixedReader) Read(p []byte) (n int, err error) {
return pr.reader.Read(p)
}
}
// NOTE: Not goroutine safe
type BufferCloser struct {
bytes.Buffer
Closed bool
}
func NewBufferCloser(buf []byte) *BufferCloser {
return &BufferCloser{
*bytes.NewBuffer(buf),
false,
}
}
func (bc *BufferCloser) Close() error {
if bc.Closed {
return errors.New("BufferCloser already closed")
}
bc.Closed = true
return nil
}
func (bc *BufferCloser) Write(p []byte) (n int, err error) {
if bc.Closed {
return 0, errors.New("Cannot write to closed BufferCloser")
}
return bc.Buffer.Write(p)
}
func (bc *BufferCloser) WriteByte(c byte) error {
if bc.Closed {
return errors.New("Cannot write to closed BufferCloser")
}
return bc.Buffer.WriteByte(c)
}
func (bc *BufferCloser) WriteRune(r rune) (n int, err error) {
if bc.Closed {
return 0, errors.New("Cannot write to closed BufferCloser")
}
return bc.Buffer.WriteRune(r)
}
func (bc *BufferCloser) WriteString(s string) (n int, err error) {
if bc.Closed {
return 0, errors.New("Cannot write to closed BufferCloser")
}
return bc.Buffer.WriteString(s)
}

+ 1
- 0
process/process.go View File

@ -52,6 +52,7 @@ func Create(label string, execPath string, args []string, inFile io.Reader, outF
proc.ExitState = exitError.ProcessState
}
}
proc.ExitState = proc.Cmd.ProcessState
proc.EndTime = time.Now() // TODO make this goroutine-safe
err = proc.OutputFile.Close()
if err != nil {


Loading…
Cancel
Save