Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / testing / iotest / writer.go
blob71f504ce2a5b4c09551755bda0509d59f887326e
1 // Copyright 2009 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.
5 package iotest
7 import (
8 "io"
9 "os"
12 // TruncateWriter returns a Writer that writes to w
13 // but stops silently after n bytes.
14 func TruncateWriter(w io.Writer, n int64) io.Writer {
15 return &truncateWriter{w, n}
18 type truncateWriter struct {
19 w io.Writer
20 n int64
23 func (t *truncateWriter) Write(p []byte) (n int, err os.Error) {
24 if t.n <= 0 {
25 return len(p), nil
27 // real write
28 n = len(p)
29 if int64(n) > t.n {
30 n = int(t.n)
32 n, err = t.w.Write(p[0:n])
33 t.n -= int64(n)
34 if err == nil {
35 n = len(p)
37 return