From 8bfcb6a5a38b5beeaf3bbf0b4f50f8e54af3bdb3 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 11 May 2011 08:19:23 -0700 Subject: [PATCH] Move CountingReader to new camli/misc library --- build.pl | 1 + lib/go/camli/client/upload.go | 16 ++++--------- lib/go/camli/misc/countingreader.go | 35 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 lib/go/camli/misc/countingreader.go diff --git a/build.pl b/build.pl index 8bfef0b67..bb1fa205a 100755 --- a/build.pl +++ b/build.pl @@ -417,6 +417,7 @@ TARGET: lib/go/camli/jsonconfig TARGET: lib/go/camli/jsonsign TARGET: lib/go/camli/lru TARGET: lib/go/camli/magic +TARGET: lib/go/camli/misc TARGET: lib/go/camli/misc/amazon/s3 TARGET: lib/go/camli/misc/httprange TARGET: lib/go/camli/mysqlindexer diff --git a/lib/go/camli/client/upload.go b/lib/go/camli/client/upload.go index 04e643e15..caaf59e29 100644 --- a/lib/go/camli/client/upload.go +++ b/lib/go/camli/client/upload.go @@ -18,7 +18,6 @@ package client import ( "bytes" - "camli/blobref" "crypto/sha1" "encoding/base64" "fmt" @@ -29,6 +28,9 @@ import ( "log" "os" "strings" + + "camli/blobref" + "camli/misc" ) var _ = log.Printf @@ -293,7 +295,7 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) { contentsSize := int64(0) req.Body = ioutil.NopCloser(io.MultiReader( strings.NewReader(multiPartHeader), - countingReader{h.Contents, &contentsSize}, + misc.CountingReader{h.Contents, &contentsSize}, strings.NewReader(multiPartFooter))) if h.Size >= 0 { @@ -380,13 +382,3 @@ func (c *Client) Upload(h *UploadHandle) (*PutResult, os.Error) { return nil, os.NewError("Server didn't receive blob.") } -type countingReader struct { - r io.Reader - n *int64 -} - -func (cr countingReader) Read(p []byte) (n int, err os.Error) { - n, err = cr.r.Read(p) - *cr.n += int64(n) - return -} diff --git a/lib/go/camli/misc/countingreader.go b/lib/go/camli/misc/countingreader.go new file mode 100644 index 000000000..6037f6a8f --- /dev/null +++ b/lib/go/camli/misc/countingreader.go @@ -0,0 +1,35 @@ +/* +Copyright 2011 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package misc + +import ( + "io" + "os" +) + +// CountingReader wraps a Reader, incrementing N by the number of +// bytes read. No locking is performed. +type CountingReader struct { + Reader io.Reader + N *int64 +} + +func (cr CountingReader) Read(p []byte) (n int, err os.Error) { + n, err = cr.Reader.Read(p) + *cr.N += int64(n) + return +}