Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s2: Document and test how to peek the stream for skippable blocks #918

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions s2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func ReaderIgnoreStreamIdentifier() ReaderOption {
// For each chunk with the ID, the callback is called with the content.
// Any returned non-nil error will abort decompression.
// Only one callback per ID is supported, latest sent will be used.
// You can peek the stream, triggering the callback, by doing a Read with a 0
// byte buffer.
func ReaderSkippableCB(id uint8, fn func(r io.Reader) error) ReaderOption {
return func(r *Reader) error {
if id < 0x80 || id > 0xfd {
Expand Down Expand Up @@ -1053,6 +1055,8 @@ func (r *Reader) ReadByte() (byte, error) {
// Any returned non-nil error will abort decompression.
// Only one callback per ID is supported, latest sent will be used.
// Sending a nil function will disable previous callbacks.
// You can peek the stream, triggering the callback, by doing a Read with a 0
// byte buffer.
func (r *Reader) SkippableCB(id uint8, fn func(r io.Reader) error) error {
if id < 0x80 || id >= chunkTypePadding {
return fmt.Errorf("ReaderSkippableCB: Invalid id provided, must be 0x80-0xfe (inclusive)")
Expand Down
45 changes: 45 additions & 0 deletions s2/reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2019+ Klaus Post. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package s2

import (
"bytes"
"io"
"testing"
)

func TestLeadingSkippableBlock(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
if err := w.AddSkippableBlock(0x80, []byte("skippable block")); err != nil {
t.Fatalf("w.AddSkippableBlock: %v", err)
}
if _, err := w.Write([]byte("some data")); err != nil {
t.Fatalf("w.Write: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("w.Close: %v", err)
}
r := NewReader(&buf)
var sb []byte
r.SkippableCB(0x80, func(sr io.Reader) error {
var err error
sb, err = io.ReadAll(sr)
return err
})
if _, err := r.Read([]byte{}); err != nil {
t.Errorf("empty read failed: %v", err)
}
if !bytes.Equal(sb, []byte("skippable block")) {
t.Errorf("didn't get correct data from skippable block: %q", string(sb))
}
data, err := io.ReadAll(r)
if err != nil {
t.Fatalf("r.Read: %v", err)
}
if !bytes.Equal(data, []byte("some data")) {
t.Errorf("didn't get correct compressed data: %q", string(data))
}
}
Loading