本文整理汇总了Golang中io.Reader类的典型用法代码示例。如果您正苦于以下问题:Golang Reader类的具体用法?Golang Reader怎么用?Golang Reader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Reader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Get
func (s *buffer) Get(r io.Reader, n int) ([]byte, error) {
// compress the data we already have
if s.begin > defaultBufferSize {
copy(s.buf[:], s.buf[s.begin:s.end])
s.end -= s.begin
s.begin = 0
}
toget := len(s.buf) - s.end
if toget > defaultBufferSize {
toget = defaultBufferSize
}
// only get up to the buffer size
if n > defaultBufferSize {
n = defaultBufferSize
}
for s.end-s.begin < n {
got, err := r.Read(s.buf[s.end : s.end+toget])
s.end += got
if err != nil {
return nil, err
}
}
return s.buf[s.begin : s.begin+n], nil
}
示例2: ReadFrom
// ReadFrom implements io.ReaderFrom.
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
if b.Buffered() == 0 {
if w, ok := b.wr.(io.ReaderFrom); ok {
return w.ReadFrom(r)
}
}
var m int
for {
m, err = r.Read(b.buf[b.n:])
if m == 0 {
break
}
b.n += m
n += int64(m)
if b.Available() == 0 {
if err1 := b.Flush(); err1 != nil {
return n, err1
}
}
if err != nil {
break
}
}
if err == io.EOF {
err = nil
}
return n, err
}
示例3: messageCopy
func messageCopy(ws *websocket.Conn, r io.Reader, base64Encode, ping bool, timeout time.Duration) error {
buf := make([]byte, 2048)
if ping {
resetTimeout(ws, timeout)
if err := websocket.Message.Send(ws, []byte{}); err != nil {
return err
}
}
for {
resetTimeout(ws, timeout)
n, err := r.Read(buf)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if n > 0 {
if base64Encode {
if err := websocket.Message.Send(ws, base64.StdEncoding.EncodeToString(buf[:n])); err != nil {
return err
}
} else {
if err := websocket.Message.Send(ws, buf[:n]); err != nil {
return err
}
}
}
}
}
示例4: pipe
func pipe(r io.Reader, w io.Writer) {
var err error
defer func() {
if err != nil && err != io.EOF {
log.Print(err)
}
}()
firstChunk := true
buf := make([]byte, cmdline.bufsize)
for {
var n int
n, err = r.Read(buf)
if n <= 0 {
return
}
if firstChunk {
firstChunk = false
} else if cmdline.maxPostDelay > 0 {
delay(cmdline.minPostDelay, cmdline.maxPostDelay)
}
_, err = w.Write(buf[:n])
if err != nil {
return
}
}
}
示例5: Decode
func (this *packetClientLoginEncryptRequestCodec17) Decode(reader io.Reader, util []byte) (decode packet.Packet, err error) {
packetClientLoginEncryptRequest := new(PacketClientLoginEncryptRequest)
packetClientLoginEncryptRequest.ServerId, err = packet.ReadString(reader, util)
if err != nil {
return
}
publicKeyLength, err := packet.ReadUint16(reader, util)
if err != nil {
return
}
packetClientLoginEncryptRequest.PublicKey = make([]byte, publicKeyLength)
_, err = reader.Read(packetClientLoginEncryptRequest.PublicKey)
if err != nil {
return
}
verifyTokenLength, err := packet.ReadUint16(reader, util)
if err != nil {
return
}
packetClientLoginEncryptRequest.VerifyToken = make([]byte, verifyTokenLength)
_, err = reader.Read(packetClientLoginEncryptRequest.VerifyToken)
if err != nil {
return
}
decode = packetClientLoginEncryptRequest
return
}
示例6: ReadFrom
// ReadFrom implements io.ReaderFrom.
func (b *Writer) ReadFrom(r io.Reader) (n int64, err error) {
if b.Buffered() == 0 {
if w, ok := b.wr.(io.ReaderFrom); ok {
return w.ReadFrom(r)
}
}
var m int
for {
if b.Available() == 0 {
if err1 := b.Flush(); err1 != nil {
return n, err1
}
}
m, err = r.Read(b.buf[b.n:])
if m == 0 {
break
}
b.n += m
n += int64(m)
if err != nil {
break
}
}
if err == io.EOF {
// If we filled the buffer exactly, flush pre-emptively.
if b.Available() == 0 {
err = b.Flush()
} else {
err = nil
}
}
return n, err
}
示例7: ReadFrom
// ReadFrom reads data from r until error or io.EOF and appends it to the buffer.
// The amount of bytes read is returned plus any error except io.EOF.
func (b *Buffer) ReadFrom(r io.Reader) (int64, error) {
err := b.err
if err != nil && err != ErrNoMoreBytes {
return 0, b.ioErr()
}
if b.fixed {
return 0, ErrOperationNotAllowed
}
var buf [4096]byte
var total int64
for {
n, err := r.Read(buf[:])
if err != nil {
if err == io.EOF {
break
}
return total, err
}
_, err = b.Write(buf[:n])
if err != nil {
return total, err
}
total += int64(n)
}
return total, nil
}
示例8: ReadFrom
// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
b.lastRead = opInvalid
// If buffer is empty, reset to recover space.
if b.off >= len(b.buf) {
b.Truncate(0)
}
for {
if free := cap(b.buf) - len(b.buf); free < MinRead {
// not enough space at end
newBuf := b.buf
if b.off+free < MinRead {
// not enough space using beginning of buffer;
// double buffer capacity
newBuf = makeSlice(2*cap(b.buf) + MinRead)
}
copy(newBuf, b.buf[b.off:])
b.buf = newBuf[:len(b.buf)-b.off]
b.off = 0
}
m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
b.buf = b.buf[0 : len(b.buf)+m]
n += int64(m)
if e == io.EOF {
break
}
if e != nil {
return n, e
}
}
return n, nil // err is EOF, so return nil explicitly
}
示例9: TtyLiner
func TtyLiner(conn io.Reader, output chan string) {
buf := make([]byte, 1)
line := []byte{}
cr := false
emit := false
for {
nr, err := conn.Read(buf)
if err != nil || nr < 1 {
glog.V(1).Info("Input byte chan closed, close the output string chan")
close(output)
return
}
switch buf[0] {
case '\n':
emit = !cr
cr = false
case '\r':
emit = true
cr = true
default:
cr = false
line = append(line, buf[0])
}
if emit {
output <- string(line)
line = []byte{}
emit = false
}
}
}
示例10: collisionOrCorrupt
// Compute the MD5 digest of a data block (consisting of buf1 + buf2 +
// all bytes readable from rdr). If all data is read successfully,
// return DiskHashError or CollisionError depending on whether it
// matches expectMD5. If an error occurs while reading, return that
// error.
//
// "content has expected MD5" is called a collision because this
// function is used in cases where we have another block in hand with
// the given MD5 but different content.
func collisionOrCorrupt(expectMD5 string, buf1, buf2 []byte, rdr io.Reader) error {
outcome := make(chan error)
data := make(chan []byte, 1)
go func() {
h := md5.New()
for b := range data {
h.Write(b)
}
if fmt.Sprintf("%x", h.Sum(nil)) == expectMD5 {
outcome <- CollisionError
} else {
outcome <- DiskHashError
}
}()
data <- buf1
if buf2 != nil {
data <- buf2
}
var err error
for rdr != nil && err == nil {
buf := make([]byte, 1<<18)
var n int
n, err = rdr.Read(buf)
data <- buf[:n]
}
close(data)
if rdr != nil && err != io.EOF {
<-outcome
return err
}
return <-outcome
}
示例11: compareReaderWithBuf
func compareReaderWithBuf(rdr io.Reader, expect []byte, hash string) error {
bufLen := 1 << 20
if bufLen > len(expect) && len(expect) > 0 {
// No need for bufLen to be longer than
// expect, except that len(buf)==0 would
// prevent us from handling empty readers the
// same way as non-empty readers: reading 0
// bytes at a time never reaches EOF.
bufLen = len(expect)
}
buf := make([]byte, bufLen)
cmp := expect
// Loop invariants: all data read so far matched what
// we expected, and the first N bytes of cmp are
// expected to equal the next N bytes read from
// rdr.
for {
n, err := rdr.Read(buf)
if n > len(cmp) || bytes.Compare(cmp[:n], buf[:n]) != 0 {
return collisionOrCorrupt(hash, expect[:len(expect)-len(cmp)], buf[:n], rdr)
}
cmp = cmp[n:]
if err == io.EOF {
if len(cmp) != 0 {
return collisionOrCorrupt(hash, expect[:len(expect)-len(cmp)], nil, nil)
}
return nil
} else if err != nil {
return err
}
}
}
示例12: DecodeUInt29
func DecodeUInt29(r io.Reader) (uint32, error) {
var n uint32 = 0
i := 0
b := make([]byte, 1)
for {
_, err := r.Read(b)
if err != nil {
return 0, err
}
if i != 3 {
n |= uint32(b[0] & 0x7F)
if b[0]&0x80 != 0 {
if i != 2 {
n <<= 7
} else {
n <<= 8
}
} else {
break
}
} else {
n |= uint32(b[0])
break
}
i++
}
return n, nil
}
示例13: copyWithBuf
// Like `io.Copy` except it only ever does one allocation of the 32K buffer.
func (tsd *Digest) copyWithBuf(dst io.Writer, src io.Reader) (written int64, err error) {
if tsd.copyBuf == nil {
tsd.copyBuf = make([]byte, 32*1024)
}
for {
nr, er := src.Read(tsd.copyBuf)
if nr > 0 {
nw, ew := dst.Write(tsd.copyBuf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return
}
示例14: copyWithExit
func copyWithExit(w io.Writer, r io.Reader, ch chan int) {
buf := make([]byte, 1024)
isTerminalRaw := false
for {
n, err := r.Read(buf)
if err == io.EOF {
ch <- 1
return
}
if err != nil {
break
}
if !isTerminalRaw {
terminal.MakeRaw(int(os.Stdin.Fd()))
isTerminalRaw = true
}
if s := string(buf[0:n]); strings.HasPrefix(s, StatusCodePrefix) {
code, _ := strconv.Atoi(strings.TrimSpace(s[37:]))
ch <- code
return
}
_, err = w.Write(buf[0:n])
if err != nil {
break
}
}
}
示例15: readTrace
// readTrace does wire-format parsing and verification.
// It does not care about specific event types and argument meaning.
func readTrace(r io.Reader) ([]rawEvent, error) {
// Read and validate trace header.
var buf [16]byte
off, err := r.Read(buf[:])
if off != 16 || err != nil {
return nil, fmt.Errorf("failed to read header: read %v, err %v", off, err)
}
if bytes.Compare(buf[:], []byte("go 1.5 trace\x00\x00\x00\x00")) != 0 {
return nil, fmt.Errorf("not a trace file")
}
// Read events.
var events []rawEvent
for {
// Read event type and number of arguments (1 byte).
off0 := off
n, err := r.Read(buf[:1])
if err == io.EOF {
break
}
if err != nil || n != 1 {
return nil, fmt.Errorf("failed to read trace at offset 0x%x: n=%v err=%v", off0, n, err)
}
off += n
typ := buf[0] << 2 >> 2
narg := buf[0] >> 6
ev := rawEvent{typ: typ, off: off0}
if narg < 3 {
for i := 0; i < int(narg)+2; i++ { // sequence number and time stamp are present but not counted in narg
var v uint64
v, off, err = readVal(r, off)
if err != nil {
return nil, err
}
ev.args = append(ev.args, v)
}
} else {
// If narg == 3, the first value is length of the event in bytes.
var v uint64
v, off, err = readVal(r, off)
if err != nil {
return nil, err
}
evLen := v
off1 := off
for evLen > uint64(off-off1) {
v, off, err = readVal(r, off)
if err != nil {
return nil, err
}
ev.args = append(ev.args, v)
}
if evLen != uint64(off-off1) {
return nil, fmt.Errorf("event has wrong length at offset 0x%x: want %v, got %v", off0, evLen, off-off1)
}
}
events = append(events, ev)
}
return events, nil
}