]> Cypherpunks repositories - gostls13.git/commitdiff
Add ReadByte to bytebuffer
authorScott Schwartz <scotts@golang.org>
Fri, 19 Jun 2009 23:29:30 +0000 (16:29 -0700)
committerScott Schwartz <scotts@golang.org>
Fri, 19 Jun 2009 23:29:30 +0000 (16:29 -0700)
R=rsc
APPROVED=rsc
DELTA=24  (24 added, 0 deleted, 0 changed)
OCL=30459
CL=30540

src/pkg/io/bytebuffer.go
src/pkg/io/bytebuffer_test.go

index 921ddb17aead963a105d11ea3d0bb2523cb7889e..000c05352d1644685806379713d0fab42a55a75d 100644 (file)
@@ -102,6 +102,17 @@ func (b *ByteBuffer) Read(p []byte) (n int, err os.Error) {
        return n, nil
 }
 
+// ReadByte reads and returns the next byte from the buffer.
+// If no byte is available, it returns error ErrEOF.
+func (b *ByteBuffer) ReadByte() (c byte, err os.Error) {
+       if b.off >= len(b.buf) {
+               return 0, ErrEOF;
+       }
+        c = b.buf[b.off];
+       b.off++;
+       return c, nil;
+}
+
 // NewByteBufferFromArray creates and initializes a new ByteBuffer
 // with buf as its initial contents.
 func NewByteBufferFromArray(buf []byte) *ByteBuffer {
index 5a54322237f978219f459a54a64cd6f86ca6e5dc..0ba83e916ba786f4d1cc3eeb87cfda8a0f3862a3 100644 (file)
@@ -118,6 +118,19 @@ func TestBasicOperations(t *testing.T) {
 
                empty(t, "TestBasicOperations (9)", &buf, string(data[0 : 20]), make([]byte, 5));
                empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100));
+
+               buf.WriteByte(data[1]);
+               c, err := buf.ReadByte();
+               if err != nil {
+                       t.Errorf("ReadByte unexpected eof\n");
+               }
+               if c != data[1] {
+                       t.Errorf("ReadByte wrong value c=%v\n", c);
+               }
+               c, err = buf.ReadByte();
+               if err == nil {
+                       t.Errorf("ReadByte unexpected not eof\n");
+               }
        }
 }