summaryrefslogtreecommitdiff
path: root/src/pkg/encoding/base64
diff options
context:
space:
mode:
authorMichael Stapelberg <stapelberg@debian.org>2013-03-04 21:27:36 +0100
committerMichael Stapelberg <michael@stapelberg.de>2013-03-04 21:27:36 +0100
commit04b08da9af0c450d645ab7389d1467308cfc2db8 (patch)
treedb247935fa4f2f94408edc3acd5d0d4f997aa0d8 /src/pkg/encoding/base64
parent917c5fb8ec48e22459d77e3849e6d388f93d3260 (diff)
downloadgolang-upstream/1.1_hg20130304.tar.gz
Imported Upstream version 1.1~hg20130304upstream/1.1_hg20130304
Diffstat (limited to 'src/pkg/encoding/base64')
-rw-r--r--src/pkg/encoding/base64/base64.go3
-rw-r--r--src/pkg/encoding/base64/base64_test.go1
-rw-r--r--src/pkg/encoding/base64/example_test.go45
3 files changed, 47 insertions, 2 deletions
diff --git a/src/pkg/encoding/base64/base64.go b/src/pkg/encoding/base64/base64.go
index 0b842f066..e66672a1c 100644
--- a/src/pkg/encoding/base64/base64.go
+++ b/src/pkg/encoding/base64/base64.go
@@ -216,7 +216,6 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
var dbuf [4]byte
dlen := 4
- dbufloop:
for j := 0; j < 4; {
if len(src) == 0 {
return n, false, CorruptInputError(len(osrc) - len(src) - j)
@@ -240,7 +239,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
}
dlen = j
end = true
- break dbufloop
+ break
}
dbuf[j] = enc.decodeMap[in]
if dbuf[j] == 0xFF {
diff --git a/src/pkg/encoding/base64/base64_test.go b/src/pkg/encoding/base64/base64_test.go
index f9b863c36..2166abd7a 100644
--- a/src/pkg/encoding/base64/base64_test.go
+++ b/src/pkg/encoding/base64/base64_test.go
@@ -257,6 +257,7 @@ func TestDecoderIssue3577(t *testing.T) {
wantErr := errors.New("my error")
next <- nextRead{5, nil}
next <- nextRead{10, wantErr}
+ next <- nextRead{0, wantErr}
d := NewDecoder(StdEncoding, &faultInjectReader{
source: "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw==", // twas brillig...
nextc: next,
diff --git a/src/pkg/encoding/base64/example_test.go b/src/pkg/encoding/base64/example_test.go
new file mode 100644
index 000000000..d18b856a0
--- /dev/null
+++ b/src/pkg/encoding/base64/example_test.go
@@ -0,0 +1,45 @@
+// Copyright 2012 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.
+
+// Keep in sync with ../base32/example_test.go.
+
+package base64_test
+
+import (
+ "encoding/base64"
+ "fmt"
+ "os"
+)
+
+func ExampleEncoding_EncodeToString() {
+ data := []byte("any + old & data")
+ str := base64.StdEncoding.EncodeToString(data)
+ fmt.Println(str)
+ // Output:
+ // YW55ICsgb2xkICYgZGF0YQ==
+}
+
+func ExampleEncoding_DecodeString() {
+ str := "c29tZSBkYXRhIHdpdGggACBhbmQg77u/"
+ data, err := base64.StdEncoding.DecodeString(str)
+ if err != nil {
+ fmt.Println("error:", err)
+ return
+ }
+ fmt.Printf("%q\n", data)
+ // Output:
+ // "some data with \x00 and \ufeff"
+}
+
+func ExampleNewEncoder() {
+ input := []byte("foo\x00bar")
+ encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
+ encoder.Write(input)
+ // Must close the encoder when finished to flush any partial blocks.
+ // If you comment out the following line, the last partial block "r"
+ // won't be encoded.
+ encoder.Close()
+ // Output:
+ // Zm9vAGJhcg==
+}