Skip to content

Commit

Permalink
Merge branch 'orisano-feat/byte-reader-optimization'
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Jul 3, 2023
2 parents ab29f6d + 68a6854 commit b2cf794
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion internal/lzma/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewReader(p []byte, s uint64, readers []io.ReadCloser) (io.ReadCloser, erro
h := bytes.NewBuffer(p)
_ = binary.Write(h, binary.LittleEndian, s)

lr, err := lzma.NewReader(io.MultiReader(h, readers[0]))
lr, err := lzma.NewReader(multiReader(h, readers[0]))
if err != nil {
return nil, err
}
Expand All @@ -51,3 +51,35 @@ func NewReader(p []byte, s uint64, readers []io.ReadCloser) (io.ReadCloser, erro
r: lr,
}, nil
}

func multiReader(b *bytes.Buffer, rc io.ReadCloser) io.Reader {
mr := io.MultiReader(b, rc)

if br, ok := rc.(io.ByteReader); ok {
return &multiByteReader{
b: b,
br: br,
mr: mr,
}
}

return mr
}

type multiByteReader struct {
b *bytes.Buffer
br io.ByteReader
mr io.Reader
}

func (m *multiByteReader) ReadByte() (byte, error) {
if m.b.Len() > 0 {
return m.b.ReadByte()
}

return m.br.ReadByte()
}

func (m *multiByteReader) Read(p []byte) (n int, err error) {
return m.mr.Read(p)
}

0 comments on commit b2cf794

Please sign in to comment.