~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/code.google.com/p/go.crypto/openpgp/packet/compressed.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-29 11:40:20 UTC
  • mfrom: (23.1.1 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20140129114020-ejieitm8smtt5vln
Tags: 1.17.1-0ubuntu2
d/tests/local-provider: Don't fail tests if ~/.juju is present as its
created by the juju version command. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
        Body io.Reader
20
20
}
21
21
 
 
22
const (
 
23
        NoCompression      = flate.NoCompression
 
24
        BestSpeed          = flate.BestSpeed
 
25
        BestCompression    = flate.BestCompression
 
26
        DefaultCompression = flate.DefaultCompression
 
27
)
 
28
 
 
29
// CompressionConfig contains compressor configuration settings.
 
30
type CompressionConfig struct {
 
31
        // Level is the compression level to use. It must be set to
 
32
        // between -1 and 9, with -1 causing the compressor to use the
 
33
        // default compression level, 0 causing the compressor to use
 
34
        // no compression and 1 to 9 representing increasing (better,
 
35
        // slower) compression levels. If Level is less than -1 or
 
36
        // more then 9, a non-nil error will be returned during
 
37
        // encryption. See the constants above for convenient common
 
38
        // settings for Level.
 
39
        Level int
 
40
}
 
41
 
22
42
func (c *Compressed) parse(r io.Reader) error {
23
43
        var buf [1]byte
24
44
        _, err := readFull(r, buf[:])
39
59
 
40
60
        return err
41
61
}
 
62
 
 
63
// compressedWriterCloser represents the serialized compression stream
 
64
// header and the compressor. Its Close() method ensures that both the
 
65
// compressor and serialized stream header are closed. Its Write()
 
66
// method writes to the compressor.
 
67
type compressedWriteCloser struct {
 
68
        sh io.Closer      // Stream Header
 
69
        c  io.WriteCloser // Compressor
 
70
}
 
71
 
 
72
func (cwc compressedWriteCloser) Write(p []byte) (int, error) {
 
73
        return cwc.c.Write(p)
 
74
}
 
75
 
 
76
func (cwc compressedWriteCloser) Close() (err error) {
 
77
        err = cwc.c.Close()
 
78
        if err != nil {
 
79
                return err
 
80
        }
 
81
 
 
82
        return cwc.sh.Close()
 
83
}
 
84
 
 
85
// SerializeCompressed serializes a compressed data packet to w and
 
86
// returns a WriteCloser to which the literal data packets themselves
 
87
// can be written and which MUST be closed on completion. If cc is
 
88
// nil, sensible defaults will be used to configure the compression
 
89
// algorithm.
 
90
func SerializeCompressed(w io.WriteCloser, algo CompressionAlgo, cc *CompressionConfig) (literaldata io.WriteCloser, err error) {
 
91
        compressed, err := serializeStreamHeader(w, packetTypeCompressed)
 
92
        if err != nil {
 
93
                return
 
94
        }
 
95
 
 
96
        _, err = compressed.Write([]byte{uint8(algo)})
 
97
        if err != nil {
 
98
                return
 
99
        }
 
100
 
 
101
        level := DefaultCompression
 
102
        if cc != nil {
 
103
                level = cc.Level
 
104
        }
 
105
 
 
106
        var compressor io.WriteCloser
 
107
        switch algo {
 
108
        case CompressionZIP:
 
109
                compressor, err = flate.NewWriter(compressed, level)
 
110
        case CompressionZLIB:
 
111
                compressor, err = zlib.NewWriterLevel(compressed, level)
 
112
        default:
 
113
                s := strconv.Itoa(int(algo))
 
114
                err = errors.UnsupportedError("Unsupported compression algorithm: " + s)
 
115
        }
 
116
        if err != nil {
 
117
                return
 
118
        }
 
119
 
 
120
        literaldata = compressedWriteCloser{compressed, compressor}
 
121
 
 
122
        return
 
123
}