@@ -11,20 +11,27 @@ buffer [1 << 3]byte input []byte hash uint16 } -// Returns a closure over the provided writer that compresses data when called. +type compressor func([]byte) error + +func (w compressor) Write(data []byte) (int, error) { + return len(data), w(data) +} + +// Returns an io.Writer implementation that wraps the provided io.Writer +// and compresses data according to the predictor algorithm // // It can buffer data as the predictor mandates 8-byte blocks with a header. // A call with no data will force a flush. -func Compressor(writer io.Writer) func([]byte) error { +func Compressor(writer io.Writer) io.Writer { var ctx context ctx.input = ctx.buffer[:0] // Forward declaration as it is required for recursion - var write func(data []byte) error + var write compressor write = func(data []byte) error { var ( blockSize int = 8 bufferLength int = len(ctx.input) @@ -101,22 +108,25 @@ } return write } -type reader func([]byte) (int, error) +// A function type alias so that it can have methods attached to it +type decompressor func([]byte) (int, error) -func (r reader) Read(output []byte) (int, error) { +// Required to implement io.Reader +func (r decompressor) Read(output []byte) (int, error) { return r(output) } -// TODO - document +// Returns an io.Reader implementation that wraps the provided io.Reader +// and decompresses data according to the predictor algorithm func Decompressor(wrapped io.Reader) io.Reader { var ctx context ctx.input = ctx.buffer[:0] - return reader(func(output []byte) (int, error) { + return decompressor(func(output []byte) (int, error) { var ( err error flags byte readCount int )