From 3fae002aa702a20dd963ca5773f70ee48a575e62 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Tue, 28 May 2019 11:39:49 +0200 Subject: [PATCH] add `sioutil` package This commit introduces the `sioutil` package. It contains just a `NopCloser` method that is related to https://golang.org/pkg/io/ioutil/#NopCloser. It wraps a io.Writer and returns a io.WriteCloser that does nothing on Close(). This is useful when en/decrypting an io.Writer but completing the en/decryption process should not close the sink. For example: ``` ew := s.EncryptWriter(sioutil.NopCloser(w), nonce, associatedData) defer ew.Close() // This does not close 'w'. ``` updates #7 --- .travis.yml | 4 ++-- sioutil/sio.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 sioutil/sio.go diff --git a/.travis.yml b/.travis.yml index cebe937..41e2d06 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,5 +19,5 @@ before_script: script: - diff -au <(gofmt -d .) <(printf "") - diff -au <(go vet .) <(printf "") - - go test - - go test -sio.BufSize=23 -sio.PlaintextSize=1825 + - go test ./... + - go test -sio.BufSize=23 -sio.PlaintextSize=1825 ./... diff --git a/sioutil/sio.go b/sioutil/sio.go new file mode 100644 index 0000000..a9441a1 --- /dev/null +++ b/sioutil/sio.go @@ -0,0 +1,22 @@ +// Copyright (c) 2019 Andreas Auernhammer. All rights reserved. +// Use of this source code is governed by a license that can be +// found in the LICENSE file. + +// Package sioutil implements some I/O utility functions. +package sioutil + +import ( + "io" +) + +type nopCloser struct { + io.Writer +} + +func (nopCloser) Close() error { return nil } + +// NopCloser returns a WriteCloser with a no-op Close method +// wrapping the provided Writer w. +func NopCloser(w io.Writer) io.WriteCloser { + return nopCloser{w} +}