| 193 | } |
| 194 | |
| 195 | func (w *EncryptWriter) Write(p []byte) (n int, err error) { |
| 196 | if w.err != nil { |
| 197 | return 0, w.err |
| 198 | } |
| 199 | if len(p) == 0 { |
| 200 | return 0, nil |
| 201 | } |
| 202 | |
| 203 | total := len(p) |
| 204 | for len(p) > 0 { |
| 205 | n := min(len(p), ChunkSize-w.buf.Len()) |
| 206 | w.buf.Write(p[:n]) |
| 207 | p = p[n:] |
| 208 | |
| 209 | // Only flush if there's a full chunk with bytes still to write, or we |
| 210 | // can't know if this is the last chunk yet. |
| 211 | if w.buf.Len() == ChunkSize && len(p) > 0 { |
| 212 | if err := w.flushChunk(notLastChunk); err != nil { |
| 213 | w.err = err |
| 214 | return 0, err |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | return total, nil |
| 219 | } |
| 220 | |
| 221 | // Close flushes the last chunk. It does not close the underlying Writer. |
| 222 | func (w *EncryptWriter) Close() error { |