diff options
author | Jakob Borg <jakob@nym.se> | 2014-09-04 08:39:39 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-09-04 08:39:39 +0200 |
commit | 2cd3ee969833a53f8b7ecb7dd95f20f08e7710c2 (patch) | |
tree | e54a77055a38797aa4779f0457f59df4b057321f | |
parent | dd3080e01857bd78fe5e5d98f9025e362a9640df (diff) |
Dead code cleanup
-rw-r--r-- | protocol/lz4stream.go | 119 | ||||
-rw-r--r-- | protocol/lz4stream_test.go | 60 |
2 files changed, 0 insertions, 179 deletions
diff --git a/protocol/lz4stream.go b/protocol/lz4stream.go deleted file mode 100644 index ef58696ea5..0000000000 --- a/protocol/lz4stream.go +++ /dev/null @@ -1,119 +0,0 @@ -package protocol - -import ( - "bytes" - "encoding/binary" - "errors" - "io" - "sync" - - lz4 "github.com/bkaradzic/go-lz4" -) - -const lz4Magic = 0x5e63b278 - -type lz4Writer struct { - wr io.Writer - mut sync.Mutex - buf []byte -} - -func newLZ4Writer(w io.Writer) *lz4Writer { - return &lz4Writer{wr: w} -} - -func (w *lz4Writer) Write(bs []byte) (int, error) { - w.mut.Lock() - defer w.mut.Unlock() - - var err error - w.buf, err = lz4.Encode(w.buf[:cap(w.buf)], bs) - if err != nil { - return 0, err - } - - var hdr [8]byte - binary.BigEndian.PutUint32(hdr[0:], lz4Magic) - binary.BigEndian.PutUint32(hdr[4:], uint32(len(w.buf))) - _, err = w.wr.Write(hdr[:]) - if err != nil { - return 0, err - } - - _, err = w.wr.Write(w.buf) - if err != nil { - return 0, err - } - - if debug { - l.Debugf("lz4 write; %d / %d bytes", len(bs), 8+len(w.buf)) - } - return len(bs), nil -} - -type lz4Reader struct { - rd io.Reader - mut sync.Mutex - buf []byte - ebuf []byte - obuf *bytes.Buffer - ibytes uint64 - obytes uint64 -} - -func newLZ4Reader(r io.Reader) *lz4Reader { - return &lz4Reader{rd: r} -} - -func (r *lz4Reader) Read(bs []byte) (int, error) { - r.mut.Lock() - defer r.mut.Unlock() - - if r.obuf == nil { - r.obuf = bytes.NewBuffer(nil) - } - - if r.obuf.Len() == 0 { - if err := r.moreBits(); err != nil { - return 0, err - } - } - - n, err := r.obuf.Read(bs) - if debug { - l.Debugf("lz4 read; %d bytes", n) - } - return n, err -} - -func (r *lz4Reader) moreBits() error { - var hdr [8]byte - _, err := io.ReadFull(r.rd, hdr[:]) - if binary.BigEndian.Uint32(hdr[0:]) != lz4Magic { - return errors.New("bad magic") - } - - ln := int(binary.BigEndian.Uint32(hdr[4:])) - if len(r.buf) < ln { - r.buf = make([]byte, int(ln)) - } else { - r.buf = r.buf[:ln] - } - - _, err = io.ReadFull(r.rd, r.buf) - if err != nil { - return err - } - - r.ebuf, err = lz4.Decode(r.ebuf[:cap(r.ebuf)], r.buf) - if err != nil { - return err - } - - if debug { - l.Debugf("lz4 moreBits: %d / %d bytes", ln+8, len(r.ebuf)) - } - - _, err = r.obuf.Write(r.ebuf) - return err -} diff --git a/protocol/lz4stream_test.go b/protocol/lz4stream_test.go deleted file mode 100644 index 079a3fb209..0000000000 --- a/protocol/lz4stream_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package protocol - -import ( - "bytes" - "crypto/rand" - "io" - "testing" -) - -var toWrite = [][]byte{ - []byte("this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed this is a short byte string that should pass through somewhat compressed"), - []byte("this is another short byte string that should pass through uncompressed"), - []byte{0, 1, 2, 3, 4, 5}, -} - -func TestLZ4Stream(t *testing.T) { - tb := make([]byte, 128*1024) - rand.Reader.Read(tb) - toWrite = append(toWrite, tb) - tb = make([]byte, 512*1024) - rand.Reader.Read(tb) - toWrite = append(toWrite, tb) - toWrite = append(toWrite, toWrite[0]) - toWrite = append(toWrite, toWrite[1]) - - rd, wr := io.Pipe() - lz4r := newLZ4Reader(rd) - lz4w := newLZ4Writer(wr) - - go func() { - for i := 0; i < 5; i++ { - for _, bs := range toWrite { - n, err := lz4w.Write(bs) - if err != nil { - t.Error(err) - } - if n != len(bs) { - t.Errorf("weird write length; %d != %d", n, len(bs)) - } - } - } - }() - - buf := make([]byte, 512*1024) - - for i := 0; i < 5; i++ { - for _, bs := range toWrite { - n, err := lz4r.Read(buf) - if err != nil { - t.Fatal(err) - } - if n != len(bs) { - t.Errorf("Unexpected len %d != %d", n, len(bs)) - } - if bytes.Compare(bs, buf[:n]) != 0 { - t.Error("Unexpected data") - } - } - } -} |