mirror of
https://github.com/golang/go
synced 2024-11-25 17:37:58 -07:00
embeddability: change bufio.BufRead to bufio.Reader etc.
R=rsc DELTA=112 (0 added, 4 deleted, 108 changed) OCL=28537 CL=28543
This commit is contained in:
parent
c8b47c6fce
commit
01712ae7d3
@ -2,8 +2,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// This package implements buffered I/O. It wraps an io.Read or io.Write
|
// This package implements buffered I/O. It wraps an io.Reader or io.Writer
|
||||||
// object, creating another object (BufRead or BufWrite) that also implements
|
// object, creating another object (Reader or Writer) that also implements
|
||||||
// the interface but provides buffering and some help for textual I/O.
|
// the interface but provides buffering and some help for textual I/O.
|
||||||
package bufio
|
package bufio
|
||||||
|
|
||||||
@ -16,11 +16,9 @@ import (
|
|||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - maybe define an interface
|
// - maybe define an interface
|
||||||
// - BufRead: ReadRune, UnreadRune ?
|
// - Reader: ReadRune, UnreadRune ?
|
||||||
// could make ReadRune generic if we dropped UnreadRune
|
// could make ReadRune generic if we dropped UnreadRune
|
||||||
// - buffered output
|
// - buffered output
|
||||||
// - would like to rename to Read, Write, but breaks
|
|
||||||
// embedding of these: would lose the Read, Write methods.
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultBufSize = 4096
|
defaultBufSize = 4096
|
||||||
@ -44,8 +42,8 @@ func copySlice(dst []byte, src []byte) {
|
|||||||
|
|
||||||
// Buffered input.
|
// Buffered input.
|
||||||
|
|
||||||
// BufRead implements buffering for an io.Read object.
|
// Reader implements buffering for an io.Reader object.
|
||||||
type BufRead struct {
|
type Reader struct {
|
||||||
buf []byte;
|
buf []byte;
|
||||||
rd io.Reader;
|
rd io.Reader;
|
||||||
r, w int;
|
r, w int;
|
||||||
@ -53,38 +51,38 @@ type BufRead struct {
|
|||||||
lastbyte int;
|
lastbyte int;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufReadSize creates a new BufRead whose buffer has the specified size,
|
// NewReaderSize creates a new Reader whose buffer has the specified size,
|
||||||
// which must be greater than zero. If the argument io.Reader is already a
|
// which must be greater than zero. If the argument io.Reader is already a
|
||||||
// BufRead with large enough size, it returns the underlying BufRead.
|
// Reader with large enough size, it returns the underlying Reader.
|
||||||
// It returns the BufRead and any error.
|
// It returns the Reader and any error.
|
||||||
func NewBufReadSize(rd io.Reader, size int) (*BufRead, os.Error) {
|
func NewReaderSize(rd io.Reader, size int) (*Reader, os.Error) {
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
return nil, BadBufSize
|
return nil, BadBufSize
|
||||||
}
|
}
|
||||||
// Is it already a BufRead?
|
// Is it already a Reader?
|
||||||
b, ok := rd.(*BufRead);
|
b, ok := rd.(*Reader);
|
||||||
if ok && len(b.buf) >= size {
|
if ok && len(b.buf) >= size {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
b = new(BufRead);
|
b = new(Reader);
|
||||||
b.buf = make([]byte, size);
|
b.buf = make([]byte, size);
|
||||||
b.rd = rd;
|
b.rd = rd;
|
||||||
b.lastbyte = -1;
|
b.lastbyte = -1;
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufRead returns a new BufRead whose buffer has the default size.
|
// NewReader returns a new Reader whose buffer has the default size.
|
||||||
func NewBufRead(rd io.Reader) *BufRead {
|
func NewReader(rd io.Reader) *Reader {
|
||||||
b, err := NewBufReadSize(rd, defaultBufSize);
|
b, err := NewReaderSize(rd, defaultBufSize);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// cannot happen - defaultBufSize is a valid size
|
// cannot happen - defaultBufSize is a valid size
|
||||||
panic("bufio: NewBufRead: ", err.String());
|
panic("bufio: NewReader: ", err.String());
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
//.fill reads a new chunk into the buffer.
|
//.fill reads a new chunk into the buffer.
|
||||||
func (b *BufRead) fill() os.Error {
|
func (b *Reader) fill() os.Error {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return b.err
|
return b.err
|
||||||
}
|
}
|
||||||
@ -113,7 +111,7 @@ func (b *BufRead) fill() os.Error {
|
|||||||
// If nn < len(p), also returns an error explaining
|
// If nn < len(p), also returns an error explaining
|
||||||
// why the read is short. At EOF, the count will be
|
// why the read is short. At EOF, the count will be
|
||||||
// zero and err will be io.ErrEOF.
|
// zero and err will be io.ErrEOF.
|
||||||
func (b *BufRead) Read(p []byte) (nn int, err os.Error) {
|
func (b *Reader) Read(p []byte) (nn int, err os.Error) {
|
||||||
nn = 0;
|
nn = 0;
|
||||||
for len(p) > 0 {
|
for len(p) > 0 {
|
||||||
n := len(p);
|
n := len(p);
|
||||||
@ -157,7 +155,7 @@ func (b *BufRead) Read(p []byte) (nn int, err os.Error) {
|
|||||||
|
|
||||||
// ReadByte reads and returns a single byte.
|
// ReadByte reads and returns a single byte.
|
||||||
// If no byte is available, returns an error.
|
// If no byte is available, returns an error.
|
||||||
func (b *BufRead) ReadByte() (c byte, err os.Error) {
|
func (b *Reader) ReadByte() (c byte, err os.Error) {
|
||||||
if b.w == b.r {
|
if b.w == b.r {
|
||||||
b.fill();
|
b.fill();
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
@ -174,7 +172,7 @@ func (b *BufRead) ReadByte() (c byte, err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnreadByte unreads the last byte. Only one byte may be unread at a given time.
|
// UnreadByte unreads the last byte. Only one byte may be unread at a given time.
|
||||||
func (b *BufRead) UnreadByte() os.Error {
|
func (b *Reader) UnreadByte() os.Error {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return b.err
|
return b.err
|
||||||
}
|
}
|
||||||
@ -195,7 +193,7 @@ func (b *BufRead) UnreadByte() os.Error {
|
|||||||
|
|
||||||
// ReadRune reads a single UTF-8 encoded Unicode character and returns the
|
// ReadRune reads a single UTF-8 encoded Unicode character and returns the
|
||||||
// rune and its size in bytes.
|
// rune and its size in bytes.
|
||||||
func (b *BufRead) ReadRune() (rune int, size int, err os.Error) {
|
func (b *Reader) ReadRune() (rune int, size int, err os.Error) {
|
||||||
for b.r + utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) {
|
for b.r + utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) {
|
||||||
n := b.w - b.r;
|
n := b.w - b.r;
|
||||||
b.fill();
|
b.fill();
|
||||||
@ -231,7 +229,7 @@ func findByte(p []byte, c byte) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Buffered returns the number of bytes that can be read from the current buffer.
|
// Buffered returns the number of bytes that can be read from the current buffer.
|
||||||
func (b *BufRead) Buffered() int {
|
func (b *Reader) Buffered() int {
|
||||||
return b.w - b.r;
|
return b.w - b.r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,7 +239,7 @@ func (b *BufRead) Buffered() int {
|
|||||||
// Fails if the line doesn't fit in the buffer.
|
// Fails if the line doesn't fit in the buffer.
|
||||||
// For internal or advanced use only; most uses should
|
// For internal or advanced use only; most uses should
|
||||||
// call ReadLineString or ReadLineBytes instead.
|
// call ReadLineString or ReadLineBytes instead.
|
||||||
func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err os.Error) {
|
func (b *Reader) ReadLineSlice(delim byte) (line []byte, err os.Error) {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return nil, b.err
|
return nil, b.err
|
||||||
}
|
}
|
||||||
@ -288,7 +286,7 @@ func (b *BufRead) ReadLineSlice(delim byte) (line []byte, err os.Error) {
|
|||||||
// If an error happens, returns the data (without a delimiter)
|
// If an error happens, returns the data (without a delimiter)
|
||||||
// and the error. (It can't leave the data in the buffer because
|
// and the error. (It can't leave the data in the buffer because
|
||||||
// it might have read more than the buffer size.)
|
// it might have read more than the buffer size.)
|
||||||
func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
func (b *Reader) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return nil, b.err
|
return nil, b.err
|
||||||
}
|
}
|
||||||
@ -364,7 +362,7 @@ func (b *BufRead) ReadLineBytes(delim byte) (line []byte, err os.Error) {
|
|||||||
// ReadLineString reads until the first occurrence of delim in the input,
|
// ReadLineString reads until the first occurrence of delim in the input,
|
||||||
// returning a new string containing the line.
|
// returning a new string containing the line.
|
||||||
// If savedelim, keep delim in the result; otherwise drop it.
|
// If savedelim, keep delim in the result; otherwise drop it.
|
||||||
func (b *BufRead) ReadLineString(delim byte, savedelim bool) (line string, err os.Error) {
|
func (b *Reader) ReadLineString(delim byte, savedelim bool) (line string, err os.Error) {
|
||||||
bytes, e := b.ReadLineBytes(delim);
|
bytes, e := b.ReadLineBytes(delim);
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return string(bytes), e
|
return string(bytes), e
|
||||||
@ -378,45 +376,45 @@ func (b *BufRead) ReadLineString(delim byte, savedelim bool) (line string, err o
|
|||||||
|
|
||||||
// buffered output
|
// buffered output
|
||||||
|
|
||||||
// BufWrite implements buffering for an io.Writer object.
|
// Writer implements buffering for an io.Writer object.
|
||||||
type BufWrite struct {
|
type Writer struct {
|
||||||
err os.Error;
|
err os.Error;
|
||||||
buf []byte;
|
buf []byte;
|
||||||
n int;
|
n int;
|
||||||
wr io.Writer;
|
wr io.Writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufWriteSize creates a new BufWrite whose buffer has the specified size,
|
// NewWriterSize creates a new Writer whose buffer has the specified size,
|
||||||
// which must be greater than zero. If the argument io.Writer is already a
|
// which must be greater than zero. If the argument io.Writer is already a
|
||||||
// BufWrite with large enough size, it returns the underlying BufWrite.
|
// Writer with large enough size, it returns the underlying Writer.
|
||||||
// It returns the BufWrite and any error.
|
// It returns the Writer and any error.
|
||||||
func NewBufWriteSize(wr io.Writer, size int) (*BufWrite, os.Error) {
|
func NewWriterSize(wr io.Writer, size int) (*Writer, os.Error) {
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
return nil, BadBufSize
|
return nil, BadBufSize
|
||||||
}
|
}
|
||||||
// Is it already a BufWrite?
|
// Is it already a Writer?
|
||||||
b, ok := wr.(*BufWrite);
|
b, ok := wr.(*Writer);
|
||||||
if ok && len(b.buf) >= size {
|
if ok && len(b.buf) >= size {
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
b = new(BufWrite);
|
b = new(Writer);
|
||||||
b.buf = make([]byte, size);
|
b.buf = make([]byte, size);
|
||||||
b.wr = wr;
|
b.wr = wr;
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufWrite returns a new BufWrite whose buffer has the default size.
|
// NewWriter returns a new Writer whose buffer has the default size.
|
||||||
func NewBufWrite(wr io.Writer) *BufWrite {
|
func NewWriter(wr io.Writer) *Writer {
|
||||||
b, err := NewBufWriteSize(wr, defaultBufSize);
|
b, err := NewWriterSize(wr, defaultBufSize);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// cannot happen - defaultBufSize is valid size
|
// cannot happen - defaultBufSize is valid size
|
||||||
panic("bufio: NewBufWrite: ", err.String());
|
panic("bufio: NewWriter: ", err.String());
|
||||||
}
|
}
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flush writes any buffered data to the underlying io.Writer.
|
// Flush writes any buffered data to the underlying io.Writer.
|
||||||
func (b *BufWrite) Flush() os.Error {
|
func (b *Writer) Flush() os.Error {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return b.err
|
return b.err
|
||||||
}
|
}
|
||||||
@ -441,12 +439,12 @@ func (b *BufWrite) Flush() os.Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Available returns how many bytes are unused in the buffer.
|
// Available returns how many bytes are unused in the buffer.
|
||||||
func (b *BufWrite) Available() int {
|
func (b *Writer) Available() int {
|
||||||
return len(b.buf) - b.n
|
return len(b.buf) - b.n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buffered returns the number of bytes that have been written into the current buffer.
|
// Buffered returns the number of bytes that have been written into the current buffer.
|
||||||
func (b *BufWrite) Buffered() int {
|
func (b *Writer) Buffered() int {
|
||||||
return b.n
|
return b.n
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,7 +452,7 @@ func (b *BufWrite) Buffered() int {
|
|||||||
// It returns the number of bytes written.
|
// It returns the number of bytes written.
|
||||||
// If nn < len(p), also returns an error explaining
|
// If nn < len(p), also returns an error explaining
|
||||||
// why the write is short.
|
// why the write is short.
|
||||||
func (b *BufWrite) Write(p []byte) (nn int, err os.Error) {
|
func (b *Writer) Write(p []byte) (nn int, err os.Error) {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return 0, b.err
|
return 0, b.err
|
||||||
}
|
}
|
||||||
@ -490,7 +488,7 @@ func (b *BufWrite) Write(p []byte) (nn int, err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WriteByte writes a single byte.
|
// WriteByte writes a single byte.
|
||||||
func (b *BufWrite) WriteByte(c byte) os.Error {
|
func (b *Writer) WriteByte(c byte) os.Error {
|
||||||
if b.err != nil {
|
if b.err != nil {
|
||||||
return b.err
|
return b.err
|
||||||
}
|
}
|
||||||
@ -504,15 +502,15 @@ func (b *BufWrite) WriteByte(c byte) os.Error {
|
|||||||
|
|
||||||
// buffered input and output
|
// buffered input and output
|
||||||
|
|
||||||
// BufReadWrite stores (a pointer to) a BufRead and a BufWrite.
|
// ReadWriter stores (a pointer to) a Reader and a Writer.
|
||||||
// It implements io.ReadWriter.
|
// It implements io.ReadWriter.
|
||||||
type BufReadWrite struct {
|
type ReadWriter struct {
|
||||||
*BufRead;
|
*Reader;
|
||||||
*BufWrite;
|
*Writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufReadWrite allocates a new BufReadWrite holding r and w.
|
// NewReadWriter allocates a new ReadWriter holding r and w.
|
||||||
func NewBufReadWrite(r *BufRead, w *BufWrite) *BufReadWrite {
|
func NewReadWriter(r *Reader, w *Writer) *ReadWriter {
|
||||||
return &BufReadWrite{r, w}
|
return &ReadWriter{r, w}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +104,7 @@ var readMakers = []readMaker {
|
|||||||
|
|
||||||
// Call ReadLineString (which ends up calling everything else)
|
// Call ReadLineString (which ends up calling everything else)
|
||||||
// to accumulate the text of a file.
|
// to accumulate the text of a file.
|
||||||
func readLines(b *BufRead) string {
|
func readLines(b *Reader) string {
|
||||||
s := "";
|
s := "";
|
||||||
for {
|
for {
|
||||||
s1, e := b.ReadLineString('\n', true);
|
s1, e := b.ReadLineString('\n', true);
|
||||||
@ -120,7 +120,7 @@ func readLines(b *BufRead) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call ReadByte to accumulate the text of a file
|
// Call ReadByte to accumulate the text of a file
|
||||||
func readBytes(buf *BufRead) string {
|
func readBytes(buf *Reader) string {
|
||||||
var b [1000]byte;
|
var b [1000]byte;
|
||||||
nb := 0;
|
nb := 0;
|
||||||
for {
|
for {
|
||||||
@ -139,7 +139,7 @@ func readBytes(buf *BufRead) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Call Read to accumulate the text of a file
|
// Call Read to accumulate the text of a file
|
||||||
func reads(buf *BufRead, m int) string {
|
func reads(buf *Reader, m int) string {
|
||||||
var b [1000]byte;
|
var b [1000]byte;
|
||||||
nb := 0;
|
nb := 0;
|
||||||
for {
|
for {
|
||||||
@ -154,15 +154,15 @@ func reads(buf *BufRead, m int) string {
|
|||||||
|
|
||||||
type bufReader struct {
|
type bufReader struct {
|
||||||
name string;
|
name string;
|
||||||
fn func(*BufRead) string;
|
fn func(*Reader) string;
|
||||||
}
|
}
|
||||||
var bufreaders = []bufReader {
|
var bufreaders = []bufReader {
|
||||||
bufReader{ "1", func(b *BufRead) string { return reads(b, 1) } },
|
bufReader{ "1", func(b *Reader) string { return reads(b, 1) } },
|
||||||
bufReader{ "2", func(b *BufRead) string { return reads(b, 2) } },
|
bufReader{ "2", func(b *Reader) string { return reads(b, 2) } },
|
||||||
bufReader{ "3", func(b *BufRead) string { return reads(b, 3) } },
|
bufReader{ "3", func(b *Reader) string { return reads(b, 3) } },
|
||||||
bufReader{ "4", func(b *BufRead) string { return reads(b, 4) } },
|
bufReader{ "4", func(b *Reader) string { return reads(b, 4) } },
|
||||||
bufReader{ "5", func(b *BufRead) string { return reads(b, 5) } },
|
bufReader{ "5", func(b *Reader) string { return reads(b, 5) } },
|
||||||
bufReader{ "7", func(b *BufRead) string { return reads(b, 7) } },
|
bufReader{ "7", func(b *Reader) string { return reads(b, 7) } },
|
||||||
bufReader{ "bytes", readBytes },
|
bufReader{ "bytes", readBytes },
|
||||||
bufReader{ "lines", readLines },
|
bufReader{ "lines", readLines },
|
||||||
}
|
}
|
||||||
@ -172,19 +172,19 @@ var bufsizes = []int {
|
|||||||
23, 32, 46, 64, 93, 128, 1024, 4096
|
23, 32, 46, 64, 93, 128, 1024, 4096
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBufReadSimple(t *testing.T) {
|
func TestReaderSimple(t *testing.T) {
|
||||||
b := NewBufRead(newByteReader(io.StringBytes("hello world")));
|
b := NewReader(newByteReader(io.StringBytes("hello world")));
|
||||||
if s := readBytes(b); s != "hello world" {
|
if s := readBytes(b); s != "hello world" {
|
||||||
t.Errorf("simple hello world test failed: got %q", s);
|
t.Errorf("simple hello world test failed: got %q", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
b = NewBufRead(newRot13Reader(newByteReader(io.StringBytes("hello world"))));
|
b = NewReader(newRot13Reader(newByteReader(io.StringBytes("hello world"))));
|
||||||
if s := readBytes(b); s != "uryyb jbeyq" {
|
if s := readBytes(b); s != "uryyb jbeyq" {
|
||||||
t.Error("rot13 hello world test failed: got %q", s);
|
t.Error("rot13 hello world test failed: got %q", s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBufRead(t *testing.T) {
|
func TestReader(t *testing.T) {
|
||||||
var texts [31]string;
|
var texts [31]string;
|
||||||
str := "";
|
str := "";
|
||||||
all := "";
|
all := "";
|
||||||
@ -205,7 +205,7 @@ func TestBufRead(t *testing.T) {
|
|||||||
bufreader := bufreaders[j];
|
bufreader := bufreaders[j];
|
||||||
bufsize := bufsizes[k];
|
bufsize := bufsizes[k];
|
||||||
read := readmaker.fn(textbytes);
|
read := readmaker.fn(textbytes);
|
||||||
buf, e := NewBufReadSize(read, bufsize);
|
buf, e := NewReaderSize(read, bufsize);
|
||||||
s := bufreader.fn(buf);
|
s := bufreader.fn(buf);
|
||||||
if s != text {
|
if s != text {
|
||||||
t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q",
|
t.Errorf("reader=%s fn=%s bufsize=%d want=%q got=%q",
|
||||||
@ -277,7 +277,7 @@ type writeMaker struct {
|
|||||||
name string;
|
name string;
|
||||||
fn func()writeBuffer;
|
fn func()writeBuffer;
|
||||||
}
|
}
|
||||||
func TestBufWrite(t *testing.T) {
|
func TestWriter(t *testing.T) {
|
||||||
var data [8192]byte;
|
var data [8192]byte;
|
||||||
|
|
||||||
var writers = []writeMaker {
|
var writers = []writeMaker {
|
||||||
@ -299,10 +299,10 @@ func TestBufWrite(t *testing.T) {
|
|||||||
// and that the data is correct.
|
// and that the data is correct.
|
||||||
|
|
||||||
write := writers[k].fn();
|
write := writers[k].fn();
|
||||||
buf, e := NewBufWriteSize(write, bs);
|
buf, e := NewWriterSize(write, bs);
|
||||||
context := fmt.Sprintf("write=%s nwrite=%d bufsize=%d", writers[k].name, nwrite, bs);
|
context := fmt.Sprintf("write=%s nwrite=%d bufsize=%d", writers[k].name, nwrite, bs);
|
||||||
if e != nil {
|
if e != nil {
|
||||||
t.Errorf("%s: NewBufWriteSize %d: %v", context, bs, e);
|
t.Errorf("%s: NewWriterSize %d: %v", context, bs, e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
n, e1 := buf.Write(data[0:nwrite]);
|
n, e1 := buf.Write(data[0:nwrite]);
|
||||||
@ -330,50 +330,50 @@ func TestBufWrite(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewBufReadSizeIdempotent(t *testing.T) {
|
func TestNewReaderSizeIdempotent(t *testing.T) {
|
||||||
const BufSize = 1000;
|
const BufSize = 1000;
|
||||||
b, err := NewBufReadSize(newByteReader(io.StringBytes("hello world")), BufSize);
|
b, err := NewReaderSize(newByteReader(io.StringBytes("hello world")), BufSize);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("NewBufReadSize create fail", err);
|
t.Error("NewReaderSize create fail", err);
|
||||||
}
|
}
|
||||||
// Does it recognize itself?
|
// Does it recognize itself?
|
||||||
b1, err2 := NewBufReadSize(b, BufSize);
|
b1, err2 := NewReaderSize(b, BufSize);
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
t.Error("NewBufReadSize #2 create fail", err2);
|
t.Error("NewReaderSize #2 create fail", err2);
|
||||||
}
|
}
|
||||||
if b1 != b {
|
if b1 != b {
|
||||||
t.Error("NewBufReadSize did not detect underlying BufRead");
|
t.Error("NewReaderSize did not detect underlying Reader");
|
||||||
}
|
}
|
||||||
// Does it wrap if existing buffer is too small?
|
// Does it wrap if existing buffer is too small?
|
||||||
b2, err3 := NewBufReadSize(b, 2*BufSize);
|
b2, err3 := NewReaderSize(b, 2*BufSize);
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
t.Error("NewBufReadSize #3 create fail", err3);
|
t.Error("NewReaderSize #3 create fail", err3);
|
||||||
}
|
}
|
||||||
if b2 == b {
|
if b2 == b {
|
||||||
t.Error("NewBufReadSize did not enlarge buffer");
|
t.Error("NewReaderSize did not enlarge buffer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewBufWriteSizeIdempotent(t *testing.T) {
|
func TestNewWriterSizeIdempotent(t *testing.T) {
|
||||||
const BufSize = 1000;
|
const BufSize = 1000;
|
||||||
b, err := NewBufWriteSize(newByteWriter(), BufSize);
|
b, err := NewWriterSize(newByteWriter(), BufSize);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("NewBufWriteSize create fail", err);
|
t.Error("NewWriterSize create fail", err);
|
||||||
}
|
}
|
||||||
// Does it recognize itself?
|
// Does it recognize itself?
|
||||||
b1, err2 := NewBufWriteSize(b, BufSize);
|
b1, err2 := NewWriterSize(b, BufSize);
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
t.Error("NewBufWriteSize #2 create fail", err2);
|
t.Error("NewWriterSize #2 create fail", err2);
|
||||||
}
|
}
|
||||||
if b1 != b {
|
if b1 != b {
|
||||||
t.Error("NewBufWriteSize did not detect underlying BufWrite");
|
t.Error("NewWriterSize did not detect underlying Writer");
|
||||||
}
|
}
|
||||||
// Does it wrap if existing buffer is too small?
|
// Does it wrap if existing buffer is too small?
|
||||||
b2, err3 := NewBufWriteSize(b, 2*BufSize);
|
b2, err3 := NewWriterSize(b, 2*BufSize);
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
t.Error("NewBufWriteSize #3 create fail", err3);
|
t.Error("NewWriterSize #3 create fail", err3);
|
||||||
}
|
}
|
||||||
if b2 == b {
|
if b2 == b {
|
||||||
t.Error("NewBufWriteSize did not enlarge buffer");
|
t.Error("NewWriterSize did not enlarge buffer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ func (r *Request) ProtoAtLeast(major, minor int) bool {
|
|||||||
// Give up if the line exceeds maxLineLength.
|
// Give up if the line exceeds maxLineLength.
|
||||||
// The returned bytes are a pointer into storage in
|
// The returned bytes are a pointer into storage in
|
||||||
// the bufio, so they are only valid until the next bufio read.
|
// the bufio, so they are only valid until the next bufio read.
|
||||||
func readLineBytes(b *bufio.BufRead) (p []byte, err os.Error) {
|
func readLineBytes(b *bufio.Reader) (p []byte, err os.Error) {
|
||||||
if p, err = b.ReadLineSlice('\n'); err != nil {
|
if p, err = b.ReadLineSlice('\n'); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ func readLineBytes(b *bufio.BufRead) (p []byte, err os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// readLineBytes, but convert the bytes into a string.
|
// readLineBytes, but convert the bytes into a string.
|
||||||
func readLine(b *bufio.BufRead) (s string, err os.Error) {
|
func readLine(b *bufio.Reader) (s string, err os.Error) {
|
||||||
p, e := readLineBytes(b);
|
p, e := readLineBytes(b);
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return "", e
|
return "", e
|
||||||
@ -131,7 +131,7 @@ func readLine(b *bufio.BufRead) (s string, err os.Error) {
|
|||||||
// A key/value has the form Key: Value\r\n
|
// A key/value has the form Key: Value\r\n
|
||||||
// and the Value can continue on multiple lines if each continuation line
|
// and the Value can continue on multiple lines if each continuation line
|
||||||
// starts with a space.
|
// starts with a space.
|
||||||
func readKeyValue(b *bufio.BufRead) (key, value string, err os.Error) {
|
func readKeyValue(b *bufio.Reader) (key, value string, err os.Error) {
|
||||||
line, e := readLineBytes(b);
|
line, e := readLineBytes(b);
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return "", "", e
|
return "", "", e
|
||||||
@ -266,7 +266,7 @@ func CanonicalHeaderKey(s string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadRequest reads and parses a request from b.
|
// ReadRequest reads and parses a request from b.
|
||||||
func ReadRequest(b *bufio.BufRead) (req *Request, err os.Error) {
|
func ReadRequest(b *bufio.Reader) (req *Request, err os.Error) {
|
||||||
req = new(Request);
|
req = new(Request);
|
||||||
|
|
||||||
// First line: GET /index.html HTTP/1.0
|
// First line: GET /index.html HTTP/1.0
|
||||||
|
@ -44,7 +44,7 @@ type Conn struct {
|
|||||||
Req *Request; // current HTTP request
|
Req *Request; // current HTTP request
|
||||||
|
|
||||||
rwc io.ReadWriteCloser; // i/o connection
|
rwc io.ReadWriteCloser; // i/o connection
|
||||||
buf *bufio.BufReadWrite; // buffered rwc
|
buf *bufio.ReadWriter; // buffered rwc
|
||||||
handler Handler; // request handler
|
handler Handler; // request handler
|
||||||
hijacked bool; // connection has been hijacked by handler
|
hijacked bool; // connection has been hijacked by handler
|
||||||
|
|
||||||
@ -61,9 +61,9 @@ func newConn(rwc io.ReadWriteCloser, raddr string, handler Handler) (c *Conn, er
|
|||||||
c.RemoteAddr = raddr;
|
c.RemoteAddr = raddr;
|
||||||
c.handler = handler;
|
c.handler = handler;
|
||||||
c.rwc = rwc;
|
c.rwc = rwc;
|
||||||
br := bufio.NewBufRead(rwc);
|
br := bufio.NewReader(rwc);
|
||||||
bw := bufio.NewBufWrite(rwc);
|
bw := bufio.NewWriter(rwc);
|
||||||
c.buf = bufio.NewBufReadWrite(br, bw);
|
c.buf = bufio.NewReadWriter(br, bw);
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ func (c *Conn) readRequest() (req *Request, err os.Error) {
|
|||||||
if c.hijacked {
|
if c.hijacked {
|
||||||
return nil, ErrHijacked
|
return nil, ErrHijacked
|
||||||
}
|
}
|
||||||
if req, err = ReadRequest(c.buf.BufRead); err != nil {
|
if req, err = ReadRequest(c.buf.Reader); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -238,7 +238,7 @@ func (c *Conn) serve() {
|
|||||||
// will not do anything else with the connection.
|
// will not do anything else with the connection.
|
||||||
// It becomes the caller's responsibility to manage
|
// It becomes the caller's responsibility to manage
|
||||||
// and close the connection.
|
// and close the connection.
|
||||||
func (c *Conn) Hijack() (rwc io.ReadWriteCloser, buf *bufio.BufReadWrite, err os.Error) {
|
func (c *Conn) Hijack() (rwc io.ReadWriteCloser, buf *bufio.ReadWriter, err os.Error) {
|
||||||
if c.hijacked {
|
if c.hijacked {
|
||||||
return nil, nil, ErrHijacked;
|
return nil, nil, ErrHijacked;
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ func testLog(t *testing.T, flag int, prefix string, pattern string, useLogf bool
|
|||||||
}
|
}
|
||||||
defer r.Close();
|
defer r.Close();
|
||||||
defer w.Close();
|
defer w.Close();
|
||||||
buf := bufio.NewBufRead(r);
|
buf := bufio.NewReader(r);
|
||||||
l := NewLogger(w, nil, prefix, flag);
|
l := NewLogger(w, nil, prefix, flag);
|
||||||
if useLogf {
|
if useLogf {
|
||||||
l.Logf("hello %d world", 23);
|
l.Logf("hello %d world", 23);
|
||||||
|
@ -18,7 +18,7 @@ func TestReadLine(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("open %s: %v", filename, err);
|
t.Fatalf("open %s: %v", filename, err);
|
||||||
}
|
}
|
||||||
br := bufio.NewBufRead(fd);
|
br := bufio.NewReader(fd);
|
||||||
|
|
||||||
var file *file;
|
var file *file;
|
||||||
file, err = open(filename);
|
file, err = open(filename);
|
||||||
|
@ -100,7 +100,7 @@ func TestFp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer f.Close();
|
defer f.Close();
|
||||||
|
|
||||||
b := bufio.NewBufRead(f);
|
b := bufio.NewReader(f);
|
||||||
|
|
||||||
lineno := 0;
|
lineno := 0;
|
||||||
for {
|
for {
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio";
|
|
||||||
"container/vector";
|
"container/vector";
|
||||||
"flag";
|
"flag";
|
||||||
"fmt";
|
"fmt";
|
||||||
|
Loading…
Reference in New Issue
Block a user