github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/compress/zlib/writer.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package zlib
     6  
     7  import (
     8  	"github.com/shogo82148/std/compress/flate"
     9  	"github.com/shogo82148/std/hash"
    10  	"github.com/shogo82148/std/io"
    11  )
    12  
    13  // これらの定数はflateパッケージからコピーされています。
    14  // これにより、「compress/zlib」をインポートするコードが「compress/flate」もインポートする必要がなくなります。
    15  const (
    16  	NoCompression      = flate.NoCompression
    17  	BestSpeed          = flate.BestSpeed
    18  	BestCompression    = flate.BestCompression
    19  	DefaultCompression = flate.DefaultCompression
    20  	HuffmanOnly        = flate.HuffmanOnly
    21  )
    22  
    23  // Writerは、書き込まれたデータを受け取り、そのデータの圧縮形式を下位のライターに書き込みます(NewWriterを参照)。
    24  type Writer struct {
    25  	w           io.Writer
    26  	level       int
    27  	dict        []byte
    28  	compressor  *flate.Writer
    29  	digest      hash.Hash32
    30  	err         error
    31  	scratch     [4]byte
    32  	wroteHeader bool
    33  }
    34  
    35  // NewWriterは、新しいWriterを作成します。
    36  // 返されたWriterに書き込まれたデータは圧縮され、wに書き込まれます。
    37  //
    38  // Writerを使用し終わったら、呼び出し元がCloseを呼び出す責任があります。
    39  // 書き込みはバッファリングされ、Closeが呼び出されるまでフラッシュされない場合があります。
    40  func NewWriter(w io.Writer) *Writer
    41  
    42  // NewWriterLevelは、NewWriterと同様ですが、デフォルトの圧縮レベルを仮定する代わりに、
    43  // 圧縮レベルを指定します。
    44  //
    45  // 圧縮レベルは、DefaultCompression、NoCompression、HuffmanOnly、BestSpeedからBestCompressionまでの
    46  // 任意の整数値であることができます。レベルが有効である場合、返されるエラーはnilになります。
    47  func NewWriterLevel(w io.Writer, level int) (*Writer, error)
    48  
    49  // NewWriterLevelDictは、NewWriterLevelと同様ですが、圧縮に使用する辞書を指定します。
    50  //
    51  // 辞書はnilである場合があります。そうでない場合、その内容はWriterが閉じられるまで変更されないようにする必要があります。
    52  func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)
    53  
    54  // Resetは、Writer zの状態をクリアし、NewWriterLevelまたはNewWriterLevelDictからの初期状態と同等になるようにしますが、
    55  // 代わりにwに書き込みます。
    56  func (z *Writer) Reset(w io.Writer)
    57  
    58  // Writeは、pの圧縮形式を基になるio.Writerに書き込みます。
    59  // 圧縮されたバイトは、Writerが閉じられるか、明示的にフラッシュされるまで必ずしもフラッシュされません。
    60  func (z *Writer) Write(p []byte) (n int, err error)
    61  
    62  // Flushは、Writerをその基になるio.Writerにフラッシュします。
    63  func (z *Writer) Flush() error
    64  
    65  // Closeは、Writerを閉じ、書き込まれていないデータを基になるio.Writerにフラッシュしますが、
    66  // 基になるio.Writerを閉じません。
    67  func (z *Writer) Close() error