1 // Copyright 2010 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.
9 // The Stream* objects are so simple that all their members are public. Users
10 // can create them themselves.
12 // StreamReader wraps a Stream into an io.Reader. It calls XORKeyStream
13 // to process each slice of data which passes through.
14 type StreamReader
struct {
19 func (r StreamReader
) Read(dst
[]byte) (n
int, err error
) {
20 n
, err
= r
.R
.Read(dst
)
21 r
.S
.XORKeyStream(dst
[:n
], dst
[:n
])
25 // StreamWriter wraps a Stream into an io.Writer. It calls XORKeyStream
26 // to process each slice of data which passes through. If any Write call
27 // returns short then the StreamWriter is out of sync and must be discarded.
28 // A StreamWriter has no internal buffering; Close does not need
29 // to be called to flush write data.
30 type StreamWriter
struct {
36 func (w StreamWriter
) Write(src
[]byte) (n
int, err error
) {
37 c
:= make([]byte, len(src
))
38 w
.S
.XORKeyStream(c
, src
)
41 if err
== nil { // should never happen
42 err
= io
.ErrShortWrite
48 // Close closes the underlying Writer and returns its Close return value, if the Writer
49 // is also an io.Closer. Otherwise it returns nil.
50 func (w StreamWriter
) Close() error
{
51 if c
, ok
:= w
.W
.(io
.Closer
); ok
{