Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 //go:generate go run gen.go -output fixedhuff.go | 5 //go:generate go run gen.go -output fixedhuff.go |
6 | 6 |
7 // Package flate implements the DEFLATE compressed data format, described in | 7 // Package flate implements the DEFLATE compressed data format, described in |
8 // RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file | 8 // RFC 1951. The gzip and zlib packages implement access to DEFLATE-based file |
9 // formats. | 9 // formats. |
10 package flate | 10 package flate |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 // A WriteError reports an error encountered while writing output. | 49 // A WriteError reports an error encountered while writing output. |
50 type WriteError struct { | 50 type WriteError struct { |
51 Offset int64 // byte offset where error occurred | 51 Offset int64 // byte offset where error occurred |
52 Err error // error returned by underlying Write | 52 Err error // error returned by underlying Write |
53 } | 53 } |
54 | 54 |
55 func (e *WriteError) Error() string { | 55 func (e *WriteError) Error() string { |
56 return "flate: write error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error() | 56 return "flate: write error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error() |
57 } | 57 } |
58 | 58 |
59 // This interface allows resetting a reader to read from a new stream | |
kortschak
2014/09/28 23:33:55
s/This interface/<Type>/
Comment on Go1 promise p
bradfitz
2014/09/29 18:39:49
Yes.
Perhaps:
// Resetter resets a ReadCloser
james.robinson
2014/09/30 05:33:18
Done.
| |
60 // of compressed data, reusing internal buffers. This can be more efficient | |
61 // than allocating a new reader for each stream. The NewReader and | |
62 // NewReaderDict functions return types that implement this interface, although | |
63 // that is not in the formal return type due to Go 1 interface stability | |
64 // concerns. | |
65 type Resettable interface { | |
kortschak
2014/09/28 23:33:55
Resetter.
It would simplify things if you had jus
bradfitz
2014/09/29 18:39:49
Yes. This.
james.robinson
2014/09/30 05:33:18
I didn't do this because it would be inconsistent
| |
66 // Reset discards any buffered data and resets the Resettable | |
67 // as if it was newly initialized with the given reader. | |
68 Reset(r io.Reader) | |
69 | |
70 // ResetDict discards any buffered data and resets the Resettable | |
71 // as if it was newly initialized with the given reader and | |
72 // dictionary. | |
73 ResetDict(r io.Reader, dict []byte) | |
74 } | |
75 | |
59 // Note that much of the implementation of huffmanDecoder is also copied | 76 // Note that much of the implementation of huffmanDecoder is also copied |
60 // into gen.go (in package main) for the purpose of precomputing the | 77 // into gen.go (in package main) for the purpose of precomputing the |
61 // fixed huffman tables so they can be included statically. | 78 // fixed huffman tables so they can be included statically. |
62 | 79 |
63 // The data structure for decoding Huffman tables is based on that of | 80 // The data structure for decoding Huffman tables is based on that of |
64 // zlib. There is a lookup table of a fixed bit width (huffmanChunkBits), | 81 // zlib. There is a lookup table of a fixed bit width (huffmanChunkBits), |
65 // For codes smaller than the table width, there are multiple entries | 82 // For codes smaller than the table width, there are multiple entries |
66 // (each combination of trailing bits has the same value). For codes | 83 // (each combination of trailing bits has the same value). For codes |
67 // larger than the table width, the table contains a link to an overflow | 84 // larger than the table width, the table contains a link to an overflow |
68 // table. The width of each entry in the link table is the maximum code | 85 // table. The width of each entry in the link table is the maximum code |
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 f.step = step | 689 f.step = step |
673 } | 690 } |
674 | 691 |
675 func makeReader(r io.Reader) Reader { | 692 func makeReader(r io.Reader) Reader { |
676 if rr, ok := r.(Reader); ok { | 693 if rr, ok := r.(Reader); ok { |
677 return rr | 694 return rr |
678 } | 695 } |
679 return bufio.NewReader(r) | 696 return bufio.NewReader(r) |
680 } | 697 } |
681 | 698 |
682 // NewReader returns a new ReadCloser that can be used | 699 func (f *decompressor) Reset(r io.Reader) { |
700 » *f = decompressor{ | |
701 » » r: makeReader(r), | |
702 » » bits: f.bits, | |
703 » » codebits: f.codebits, | |
704 » » hist: f.hist, | |
705 » » step: (*decompressor).nextBlock, | |
706 » } | |
707 } | |
708 | |
709 func (f *decompressor) ResetDict(r io.Reader, dict []byte) { | |
710 » f.Reset(r) | |
711 » f.setDict(dict) | |
712 } | |
713 | |
714 // NewReader returns a new io.ReadCloser that can be used | |
bradfitz
2014/09/29 18:39:49
drop the "io." addition
james.robinson
2014/09/30 05:33:18
Done.
| |
683 // to read the uncompressed version of r. It is the caller's | 715 // to read the uncompressed version of r. It is the caller's |
684 // responsibility to call Close on the ReadCloser when | 716 // responsibility to call Close on the io.ReadCloser when |
bradfitz
2014/09/29 18:39:49
likewise
james.robinson
2014/09/30 05:33:18
Done.
| |
685 // finished reading. | 717 // finished reading. |
718 // | |
719 // The io.ReadCloser returned by NewReader also implements | |
bradfitz
2014/09/29 18:39:50
// The returned ReadCloser also implements Resette
james.robinson
2014/09/30 05:33:18
Done.
| |
720 // the Resettable interface. | |
686 func NewReader(r io.Reader) io.ReadCloser { | 721 func NewReader(r io.Reader) io.ReadCloser { |
687 var f decompressor | 722 var f decompressor |
688 f.bits = new([maxLit + maxDist]int) | 723 f.bits = new([maxLit + maxDist]int) |
689 f.codebits = new([numCodes]int) | 724 f.codebits = new([numCodes]int) |
690 f.r = makeReader(r) | 725 f.r = makeReader(r) |
691 f.hist = new([maxHist]byte) | 726 f.hist = new([maxHist]byte) |
692 f.step = (*decompressor).nextBlock | 727 f.step = (*decompressor).nextBlock |
693 return &f | 728 return &f |
694 } | 729 } |
695 | 730 |
696 // NewReaderDict is like NewReader but initializes the reader | 731 // NewReaderDict is like NewReader but initializes the reader |
697 // with a preset dictionary. The returned Reader behaves as if | 732 // with a preset dictionary. The returned Reader behaves as if |
698 // the uncompressed data stream started with the given dictionary, | 733 // the uncompressed data stream started with the given dictionary, |
699 // which has already been read. NewReaderDict is typically used | 734 // which has already been read. NewReaderDict is typically used |
700 // to read data compressed by NewWriterDict. | 735 // to read data compressed by NewWriterDict. |
736 // | |
737 // The io.ReadCloser returned by NewReader also implements | |
bradfitz
2014/09/29 18:39:50
// The returned ReadCloser also implements Resette
james.robinson
2014/09/30 05:33:18
Done.
| |
738 // the Resettable interface. | |
701 func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser { | 739 func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser { |
702 var f decompressor | 740 var f decompressor |
703 f.r = makeReader(r) | 741 f.r = makeReader(r) |
704 f.hist = new([maxHist]byte) | 742 f.hist = new([maxHist]byte) |
705 f.bits = new([maxLit + maxDist]int) | 743 f.bits = new([maxLit + maxDist]int) |
706 f.codebits = new([numCodes]int) | 744 f.codebits = new([numCodes]int) |
707 f.step = (*decompressor).nextBlock | 745 f.step = (*decompressor).nextBlock |
708 f.setDict(dict) | 746 f.setDict(dict) |
709 return &f | 747 return &f |
710 } | 748 } |
OLD | NEW |