diff options
author | David Symonds <dsymonds@golang.org> | 2009-10-05 04:08:24 -0700 |
---|---|---|
committer | David Symonds <dsymonds@golang.org> | 2009-10-05 04:08:24 -0700 |
commit | a3ce9fff79da30bcc57fdddc4feda3060b8e6877 (patch) | |
tree | f6f2e2b5dcd6e0e1693c20978de3f35dc9ce68c0 /src/pkg/testing | |
parent | 75df1a1c6c07eed1a469e24c1e18c2deff801b6c (diff) | |
download | golang-a3ce9fff79da30bcc57fdddc4feda3060b8e6877.tar.gz |
Add write support for the GNU tar binary numeric field extension.
R=rsc
APPROVED=rsc
DELTA=102 (89 added, 1 deleted, 12 changed)
OCL=35321
CL=35327
Diffstat (limited to 'src/pkg/testing')
-rw-r--r-- | src/pkg/testing/iotest/Makefile | 1 | ||||
-rw-r--r-- | src/pkg/testing/iotest/writer.go | 38 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/pkg/testing/iotest/Makefile b/src/pkg/testing/iotest/Makefile index b223fb932..a37a9d622 100644 --- a/src/pkg/testing/iotest/Makefile +++ b/src/pkg/testing/iotest/Makefile @@ -8,5 +8,6 @@ TARG=testing/iotest GOFILES=\ logger.go\ reader.go\ + writer.go\ include $(GOROOT)/src/Make.pkg diff --git a/src/pkg/testing/iotest/writer.go b/src/pkg/testing/iotest/writer.go new file mode 100644 index 000000000..7bd5ddda6 --- /dev/null +++ b/src/pkg/testing/iotest/writer.go @@ -0,0 +1,38 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package iotest + +import ( + "io"; + "os"; +) + +// TruncateWriter returns a Writer that writes to w +// but stops silently after n bytes. +func TruncateWriter(w io.Writer, n int64) io.Writer { + return &truncateWriter{w, n}; +} + +type truncateWriter struct { + w io.Writer; + n int64; +} + +func (t *truncateWriter) Write(p []byte) (n int, err os.Error) { + if t.n <= 0 { + return len(p), nil + } + // real write + n = len(p); + if int64(n) > t.n { + n = int(t.n); + } + n, err = t.w.Write(p[0:n]); + t.n -= int64(n); + if err == nil { + n = len(p); + } + return +} |