MCPcopy
hub / github.com/kovidgoyal/kitty / NewStreamDecompressor

Function NewStreamDecompressor

tools/utils/stream_decompressor.go:39–124  ·  view source on GitHub ↗

Wrap Go's awful decompressor routines to allow feeding them data in chunks. For example: sd := NewStreamDecompressor(zlib.NewReader, output) sd(chunk, false) ... sd(last_chunk, true) after this call, calling sd() further will just return io.EOF. To close the decompressor at any time, call sd(nil, tr

(constructor func(io.Reader) (io.ReadCloser, error), output io.Writer)

Source from the content-addressed store, hash-verified

37// To close the decompressor at any time, call sd(nil, true).
38// Note: output.Write() may be called from a different thread, but only while the main thread is in sd()
39func NewStreamDecompressor(constructor func(io.Reader) (io.ReadCloser, error), output io.Writer) StreamDecompressor {
40 if constructor == nil { // identity decompressor
41 var err error
42 return func(chunk []byte, is_last bool) error {
43 if err != nil {
44 return err
45 }
46 if len(chunk) > 0 {
47 _, err = output.Write(chunk)
48 }
49 if is_last {
50 if err == nil {
51 err = io.EOF
52 return nil
53 }
54 }
55 return err
56 }
57 }
58 pipe_r, pipe_w := io.Pipe()
59 pr := pipe_reader{pr: pipe_r}
60 finished := make(chan error, 1)
61 finished_err := errors.New("finished")
62 go func() {
63 var err error
64 defer func() {
65 finished <- err
66 }()
67 var impl io.ReadCloser
68 impl, err = constructor(&pr)
69 if err != nil {
70 pipe_r.CloseWithError(err)
71 return
72 }
73 _, err = io.Copy(output, impl)
74 cerr := impl.Close()
75 if err == nil {
76 err = cerr
77 }
78 if err == nil {
79 err = finished_err
80 }
81 pipe_r.CloseWithError(err)
82 }()
83
84 var iter_err error
85 return func(chunk []byte, is_last bool) error {
86 if iter_err != nil {
87 if iter_err == finished_err {
88 iter_err = io.EOF
89 }
90 return iter_err
91 }
92 if len(chunk) > 0 {
93 var n int
94 n, iter_err = pipe_w.Write(chunk)
95 if iter_err != nil && iter_err != finished_err {
96 return iter_err

Callers 2

new_remote_fileFunction · 0.92
TestStreamDecompressorFunction · 0.85

Calls 3

CopyMethod · 0.80
WriteMethod · 0.45
CloseMethod · 0.45

Tested by 1

TestStreamDecompressorFunction · 0.68