Commit 93368be6 authored by Dave Cheney's avatar Dave Cheney

cmd/internal/bio: embed bufio.{Reader,Writer} in bio.{Reader,Writer}

Change-Id: Ie95b0b0d4f724f4769cf2d4f8063cb5019fa9bc9
Reviewed-on: https://go-review.googlesource.com/21781Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent ca397bb6
...@@ -407,7 +407,7 @@ func dumpexport() { ...@@ -407,7 +407,7 @@ func dumpexport() {
pkgs = savedPkgs pkgs = savedPkgs
pkgMap = savedPkgMap pkgMap = savedPkgMap
} else { } else {
size = export(bout.Writer(), Debug_export != 0) size = export(bout.Writer, Debug_export != 0)
} }
exportf("\n$$\n") exportf("\n$$\n")
} else { } else {
......
...@@ -17,21 +17,15 @@ const EOF = -1 ...@@ -17,21 +17,15 @@ const EOF = -1
// Reader implements a seekable buffered io.Reader. // Reader implements a seekable buffered io.Reader.
type Reader struct { type Reader struct {
f *os.File f *os.File
r *bufio.Reader *bufio.Reader
} }
// Writer implements a seekable buffered io.Writer. // Writer implements a seekable buffered io.Writer.
type Writer struct { type Writer struct {
f *os.File f *os.File
w *bufio.Writer *bufio.Writer
} }
// Reader returns this Reader's underlying bufio.Reader.
func (r *Reader) Reader() *bufio.Reader { return r.r }
// Writer returns this Writer's underlying bufio.Writer.
func (w *Writer) Writer() *bufio.Writer { return w.w }
// Create creates the file named name and returns a Writer // Create creates the file named name and returns a Writer
// for that file. // for that file.
func Create(name string) (*Writer, error) { func Create(name string) (*Writer, error) {
...@@ -39,7 +33,7 @@ func Create(name string) (*Writer, error) { ...@@ -39,7 +33,7 @@ func Create(name string) (*Writer, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Writer{f: f, w: bufio.NewWriter(f)}, nil return &Writer{f: f, Writer: bufio.NewWriter(f)}, nil
} }
// Open returns a Reader for the file named name. // Open returns a Reader for the file named name.
...@@ -48,31 +42,23 @@ func Open(name string) (*Reader, error) { ...@@ -48,31 +42,23 @@ func Open(name string) (*Reader, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &Reader{f: f, r: bufio.NewReader(f)}, nil return &Reader{f: f, Reader: bufio.NewReader(f)}, nil
}
func (w *Writer) Write(p []byte) (int, error) {
return w.w.Write(p)
}
func (w *Writer) WriteString(p string) (int, error) {
return w.w.WriteString(p)
} }
func (r *Reader) Seek(offset int64, whence int) int64 { func (r *Reader) Seek(offset int64, whence int) int64 {
if whence == 1 { if whence == 1 {
offset -= int64(r.r.Buffered()) offset -= int64(r.Buffered())
} }
off, err := r.f.Seek(offset, whence) off, err := r.f.Seek(offset, whence)
if err != nil { if err != nil {
log.Fatalf("seeking in output: %v", err) log.Fatalf("seeking in output: %v", err)
} }
r.r.Reset(r.f) r.Reset(r.f)
return off return off
} }
func (w *Writer) Seek(offset int64, whence int) int64 { func (w *Writer) Seek(offset int64, whence int) int64 {
if err := w.w.Flush(); err != nil { if err := w.Flush(); err != nil {
log.Fatalf("writing output: %v", err) log.Fatalf("writing output: %v", err)
} }
off, err := w.f.Seek(offset, whence) off, err := w.f.Seek(offset, whence)
...@@ -87,12 +73,12 @@ func (r *Reader) Offset() int64 { ...@@ -87,12 +73,12 @@ func (r *Reader) Offset() int64 {
if err != nil { if err != nil {
log.Fatalf("seeking in output [0, 1]: %v", err) log.Fatalf("seeking in output [0, 1]: %v", err)
} }
off -= int64(r.r.Buffered()) off -= int64(r.Buffered())
return off return off
} }
func (w *Writer) Offset() int64 { func (w *Writer) Offset() int64 {
if err := w.w.Flush(); err != nil { if err := w.Flush(); err != nil {
log.Fatalf("writing output: %v", err) log.Fatalf("writing output: %v", err)
} }
off, err := w.f.Seek(0, 1) off, err := w.f.Seek(0, 1)
...@@ -102,16 +88,8 @@ func (w *Writer) Offset() int64 { ...@@ -102,16 +88,8 @@ func (w *Writer) Offset() int64 {
return off return off
} }
func (w *Writer) Flush() error {
return w.w.Flush()
}
func (w *Writer) WriteByte(c byte) error {
return w.w.WriteByte(c)
}
func Bread(r *Reader, p []byte) int { func Bread(r *Reader, p []byte) int {
n, err := io.ReadFull(r.r, p) n, err := io.ReadFull(r, p)
if n == 0 { if n == 0 {
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
n = -1 n = -1
...@@ -121,7 +99,7 @@ func Bread(r *Reader, p []byte) int { ...@@ -121,7 +99,7 @@ func Bread(r *Reader, p []byte) int {
} }
func Bgetc(r *Reader) int { func Bgetc(r *Reader) int {
c, err := r.r.ReadByte() c, err := r.ReadByte()
if err != nil { if err != nil {
if err != io.EOF { if err != io.EOF {
log.Fatalf("reading input: %v", err) log.Fatalf("reading input: %v", err)
...@@ -131,16 +109,8 @@ func Bgetc(r *Reader) int { ...@@ -131,16 +109,8 @@ func Bgetc(r *Reader) int {
return int(c) return int(c)
} }
func (r *Reader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
func (r *Reader) Peek(n int) ([]byte, error) {
return r.r.Peek(n)
}
func Brdline(r *Reader, delim int) string { func Brdline(r *Reader, delim int) string {
s, err := r.r.ReadBytes(byte(delim)) s, err := r.ReadBytes(byte(delim))
if err != nil { if err != nil {
log.Fatalf("reading input: %v", err) log.Fatalf("reading input: %v", err)
} }
...@@ -152,7 +122,7 @@ func (r *Reader) Close() error { ...@@ -152,7 +122,7 @@ func (r *Reader) Close() error {
} }
func (w *Writer) Close() error { func (w *Writer) Close() error {
err := w.w.Flush() err := w.Flush()
err1 := w.f.Close() err1 := w.f.Close()
if err == nil { if err == nil {
err = err1 err = err1
......
...@@ -377,7 +377,7 @@ func (w *objWriter) writeLengths() { ...@@ -377,7 +377,7 @@ func (w *objWriter) writeLengths() {
func newObjWriter(ctxt *Link, b *bio.Writer) *objWriter { func newObjWriter(ctxt *Link, b *bio.Writer) *objWriter {
return &objWriter{ return &objWriter{
ctxt: ctxt, ctxt: ctxt,
wr: b.Writer(), wr: b.Writer,
vrefIdx: make(map[string]int), vrefIdx: make(map[string]int),
refIdx: make(map[string]int), refIdx: make(map[string]int),
} }
......
...@@ -150,7 +150,7 @@ type objReader struct { ...@@ -150,7 +150,7 @@ type objReader struct {
func LoadObjFile(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { func LoadObjFile(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
start := f.Offset() start := f.Offset()
r := &objReader{ r := &objReader{
rd: f.Reader(), rd: f.Reader,
pkg: pkg, pkg: pkg,
ctxt: ctxt, ctxt: ctxt,
pn: pn, pn: pn,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment