Artifact
c11fdb5a5392a412b129ad98c85caf7197ee8a0c:
0000: 2f 2f 20 50 61 63 6b 61 67 65 20 69 6f 75 74 69 // Package iouti
0010: 6c 20 63 6f 6e 74 61 69 6e 73 20 76 61 72 69 6f l contains vario
0020: 75 73 20 63 6f 6e 73 74 72 75 63 74 73 20 66 6f us constructs fo
0030: 72 20 69 6f 20 6f 70 65 72 61 74 69 6f 6e 73 2e r io operations.
0040: 0a 70 61 63 6b 61 67 65 20 69 6f 75 74 69 6c 0a .package ioutil.
0050: 0a 69 6d 70 6f 72 74 20 28 0a 09 22 69 6f 22 0a .import (.."io".
0060: 29 0a 0a 2f 2f 20 41 6e 20 66 75 6e 63 74 69 6f )..// An functio
0070: 6e 20 61 6c 69 61 73 20 74 79 70 65 20 74 68 61 n alias type tha
0080: 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 69 6f 2e t implements io.
0090: 57 72 69 74 65 72 2e 0a 74 79 70 65 20 57 72 69 Writer..type Wri
00a0: 74 65 72 46 75 6e 63 20 66 75 6e 63 28 5b 5d 62 terFunc func([]b
00b0: 79 74 65 29 20 28 69 6e 74 2c 20 65 72 72 6f 72 yte) (int, error
00c0: 29 0a 0a 2f 2f 20 44 65 6c 65 67 61 74 65 73 20 )..// Delegates
00d0: 74 68 65 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 the call to the
00e0: 57 72 69 74 65 72 46 75 6e 63 20 77 68 69 6c 65 WriterFunc while
00f0: 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 69 6f implementing io
0100: 2e 57 72 69 74 65 72 2e 0a 66 75 6e 63 20 28 77 .Writer..func (w
0110: 20 57 72 69 74 65 72 46 75 6e 63 29 20 57 72 69 WriterFunc) Wri
0120: 74 65 28 62 20 5b 5d 62 79 74 65 29 20 28 69 6e te(b []byte) (in
0130: 74 2c 20 65 72 72 6f 72 29 20 7b 0a 09 72 65 74 t, error) {..ret
0140: 75 72 6e 20 77 28 62 29 0a 7d 0a 0a 2f 2f 20 41 urn w(b).}..// A
0150: 6e 20 66 75 6e 63 74 69 6f 6e 20 61 6c 69 61 73 n function alias
0160: 20 74 79 70 65 20 74 68 61 74 20 69 6d 70 6c 65 type that imple
0170: 6d 65 6e 74 73 20 69 6f 2e 52 65 61 64 65 72 2e ments io.Reader.
0180: 0a 74 79 70 65 20 52 65 61 64 65 72 46 75 6e 63 .type ReaderFunc
0190: 20 66 75 6e 63 28 5b 5d 62 79 74 65 29 20 28 69 func([]byte) (i
01a0: 6e 74 2c 20 65 72 72 6f 72 29 0a 0a 2f 2f 20 44 nt, error)..// D
01b0: 65 6c 65 67 61 74 65 73 20 74 68 65 20 63 61 6c elegates the cal
01c0: 6c 20 74 6f 20 74 68 65 20 57 72 69 74 65 72 46 l to the WriterF
01d0: 75 6e 63 20 77 68 69 6c 65 20 69 6d 70 6c 65 6d unc while implem
01e0: 65 6e 74 69 6e 67 20 69 6f 2e 52 65 61 64 65 72 enting io.Reader
01f0: 2e 0a 66 75 6e 63 20 28 72 20 52 65 61 64 65 72 ..func (r Reader
0200: 46 75 6e 63 29 20 52 65 61 64 28 62 20 5b 5d 62 Func) Read(b []b
0210: 79 74 65 29 20 28 69 6e 74 2c 20 65 72 72 6f 72 yte) (int, error
0220: 29 20 7b 0a 09 72 65 74 75 72 6e 20 72 28 62 29 ) {..return r(b)
0230: 0a 7d 0a 0a 2f 2f 20 52 65 74 75 72 6e 73 20 61 .}..// Returns a
0240: 20 77 72 69 74 65 72 20 74 68 61 74 20 64 65 6c writer that del
0250: 65 67 61 74 65 73 20 63 61 6c 6c 73 20 74 6f 20 egates calls to
0260: 57 72 69 74 65 28 2e 2e 2e 29 20 77 68 69 6c 65 Write(...) while
0270: 20 65 6e 73 75 72 69 6e 67 0a 2f 2f 20 74 68 61 ensuring.// tha
0280: 74 20 69 74 20 69 73 20 6e 65 76 65 72 20 63 61 t it is never ca
0290: 6c 6c 65 64 20 77 69 74 68 20 6c 65 73 73 20 62 lled with less b
02a0: 79 74 65 73 20 74 68 61 6e 20 74 68 65 20 73 70 ytes than the sp
02b0: 65 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 2e 0a ecified amount..
02c0: 2f 2f 0a 2f 2f 20 43 61 6c 6c 73 20 77 69 74 68 //.// Calls with
02d0: 20 66 65 77 65 72 20 62 79 74 65 73 20 61 72 65 fewer bytes are
02e0: 20 62 75 66 66 65 72 65 64 20 77 68 69 6c 65 20 buffered while
02f0: 61 20 63 61 6c 6c 20 77 69 74 68 20 61 20 6e 69 a call with a ni
0300: 6c 20 73 6c 69 63 65 0a 2f 2f 20 63 61 75 73 65 l slice.// cause
0310: 73 20 74 68 65 20 62 75 66 66 65 72 20 74 6f 20 s the buffer to
0320: 62 65 20 66 6c 75 73 68 65 64 20 74 6f 20 74 68 be flushed to th
0330: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 77 72 69 e underlying wri
0340: 74 65 72 2e 0a 66 75 6e 63 20 53 69 7a 65 64 57 ter..func SizedW
0350: 72 69 74 65 72 28 77 72 69 74 65 72 20 69 6f 2e riter(writer io.
0360: 57 72 69 74 65 72 2c 20 73 69 7a 65 20 69 6e 74 Writer, size int
0370: 29 20 69 6f 2e 57 72 69 74 65 72 20 7b 0a 09 76 ) io.Writer {..v
0380: 61 72 20 73 77 20 73 69 7a 65 64 57 72 69 74 65 ar sw sizedWrite
0390: 72 0a 09 73 77 2e 77 72 69 74 65 72 20 3d 20 77 r..sw.writer = w
03a0: 72 69 74 65 72 0a 09 73 77 2e 62 75 66 66 65 72 riter..sw.buffer
03b0: 20 3d 20 6d 61 6b 65 28 5b 5d 62 79 74 65 2c 20 = make([]byte,
03c0: 30 2c 20 73 69 7a 65 29 0a 09 73 77 2e 73 69 7a 0, size)..sw.siz
03d0: 65 20 3d 20 73 69 7a 65 0a 09 72 65 74 75 72 6e e = size..return
03e0: 20 26 73 77 0a 7d 0a 0a 74 79 70 65 20 73 69 7a &sw.}..type siz
03f0: 65 64 57 72 69 74 65 72 20 73 74 72 75 63 74 20 edWriter struct
0400: 7b 0a 09 77 72 69 74 65 72 20 69 6f 2e 57 72 69 {..writer io.Wri
0410: 74 65 72 0a 09 62 75 66 66 65 72 20 5b 5d 62 79 ter..buffer []by
0420: 74 65 0a 09 73 69 7a 65 20 20 20 69 6e 74 0a 7d te..size int.}
0430: 0a 0a 66 75 6e 63 20 28 73 77 20 2a 73 69 7a 65 ..func (sw *size
0440: 64 57 72 69 74 65 72 29 20 57 72 69 74 65 28 69 dWriter) Write(i
0450: 6e 70 75 74 20 5b 5d 62 79 74 65 29 20 28 69 6e nput []byte) (in
0460: 74 2c 20 65 72 72 6f 72 29 20 7b 0a 09 76 61 72 t, error) {..var
0470: 20 28 0a 09 09 63 6f 75 6e 74 20 69 6e 74 0a 09 (...count int..
0480: 09 65 72 72 20 20 20 65 72 72 6f 72 0a 09 29 0a .err error..).
0490: 0a 09 2f 2f 20 46 6c 75 73 68 20 74 68 65 20 62 ..// Flush the b
04a0: 75 66 66 65 72 20 77 68 65 6e 20 63 61 6c 6c 65 uffer when calle
04b0: 64 20 77 69 74 68 20 6e 6f 20 62 79 74 65 73 20 d with no bytes
04c0: 74 6f 20 77 72 69 74 65 0a 09 69 66 20 69 6e 70 to write..if inp
04d0: 75 74 20 3d 3d 20 6e 69 6c 20 7b 0a 09 09 2f 2f ut == nil {...//
04e0: 20 43 61 6c 6c 20 74 68 65 20 77 72 69 74 65 72 Call the writer
04f0: 20 77 69 74 68 20 77 68 61 74 65 76 65 72 20 77 with whatever w
0500: 65 20 68 61 76 65 20 69 6e 20 73 74 6f 72 65 2e e have in store.
0510: 2e 0a 09 09 63 6f 75 6e 74 2c 20 65 72 72 20 3d ....count, err =
0520: 20 73 77 2e 77 72 69 74 65 72 2e 57 72 69 74 65 sw.writer.Write
0530: 28 73 77 2e 62 75 66 66 65 72 29 0a 0a 09 09 2f (sw.buffer)..../
0540: 2f 20 41 64 76 61 6e 63 65 20 74 68 65 20 62 75 / Advance the bu
0550: 66 66 65 72 0a 09 09 73 77 2e 62 75 66 66 65 72 ffer...sw.buffer
0560: 20 3d 20 73 77 2e 62 75 66 66 65 72 5b 3a 63 6f = sw.buffer[:co
0570: 70 79 28 73 77 2e 62 75 66 66 65 72 2c 20 73 77 py(sw.buffer, sw
0580: 2e 62 75 66 66 65 72 5b 63 6f 75 6e 74 3a 5d 29 .buffer[count:])
0590: 5d 0a 0a 09 09 72 65 74 75 72 6e 20 30 2c 20 65 ]....return 0, e
05a0: 72 72 0a 09 7d 0a 0a 09 2f 2f 20 44 65 6c 65 67 rr..}...// Deleg
05b0: 61 74 65 20 74 6f 20 74 68 65 20 77 72 69 74 65 ate to the write
05c0: 72 20 69 66 20 74 68 65 20 73 69 7a 65 20 69 73 r if the size is
05d0: 20 72 69 67 68 74 0a 09 69 66 20 6c 65 6e 28 73 right..if len(s
05e0: 77 2e 62 75 66 66 65 72 29 20 3d 3d 20 30 20 26 w.buffer) == 0 &
05f0: 26 20 6c 65 6e 28 69 6e 70 75 74 29 20 3e 3d 20 & len(input) >=
0600: 73 77 2e 73 69 7a 65 20 7b 0a 09 09 72 65 64 75 sw.size {...redu
0610: 63 65 64 20 3a 3d 20 28 6c 65 6e 28 69 6e 70 75 ced := (len(inpu
0620: 74 29 20 2f 20 73 77 2e 73 69 7a 65 29 20 2a 20 t) / sw.size) *
0630: 73 77 2e 73 69 7a 65 0a 09 09 63 6f 75 6e 74 2c sw.size...count,
0640: 20 65 72 72 20 3d 20 73 77 2e 77 72 69 74 65 72 err = sw.writer
0650: 2e 57 72 69 74 65 28 69 6e 70 75 74 5b 3a 72 65 .Write(input[:re
0660: 64 75 63 65 64 5d 29 0a 09 09 69 66 20 63 6f 75 duced])...if cou
0670: 6e 74 20 3c 20 72 65 64 75 63 65 64 20 7c 7c 20 nt < reduced ||
0680: 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 09 err != nil {....
0690: 72 65 74 75 72 6e 20 63 6f 75 6e 74 2c 20 65 72 return count, er
06a0: 72 0a 09 09 7d 0a 0a 09 09 2f 2f 20 53 74 61 67 r...}....// Stag
06b0: 65 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20 e any remaining
06c0: 64 61 74 61 20 69 6e 20 74 68 65 20 62 75 66 66 data in the buff
06d0: 65 72 0a 09 09 73 77 2e 62 75 66 66 65 72 20 3d er...sw.buffer =
06e0: 20 61 70 70 65 6e 64 28 73 77 2e 62 75 66 66 65 append(sw.buffe
06f0: 72 2c 20 69 6e 70 75 74 5b 63 6f 75 6e 74 3a 5d r, input[count:]
0700: 2e 2e 2e 29 0a 09 09 72 65 74 75 72 6e 20 6c 65 ...)...return le
0710: 6e 28 69 6e 70 75 74 29 2c 20 6e 69 6c 0a 09 7d n(input), nil..}
0720: 0a 0a 09 2f 2f 20 41 70 70 65 6e 64 20 64 61 74 ...// Append dat
0730: 61 20 74 6f 20 74 68 65 20 62 75 66 66 65 72 0a a to the buffer.
0740: 09 63 6f 75 6e 74 20 3d 20 63 6f 70 79 28 73 77 .count = copy(sw
0750: 2e 62 75 66 66 65 72 5b 6c 65 6e 28 73 77 2e 62 .buffer[len(sw.b
0760: 75 66 66 65 72 29 3a 73 77 2e 73 69 7a 65 5d 2c uffer):sw.size],
0770: 20 69 6e 70 75 74 29 0a 09 73 77 2e 62 75 66 66 input)..sw.buff
0780: 65 72 20 3d 20 73 77 2e 62 75 66 66 65 72 5b 3a er = sw.buffer[:
0790: 6c 65 6e 28 73 77 2e 62 75 66 66 65 72 29 2b 63 len(sw.buffer)+c
07a0: 6f 75 6e 74 5d 0a 0a 09 2f 2f 20 52 65 74 75 72 ount]...// Retur
07b0: 6e 20 69 66 20 77 65 20 64 6f 6e 27 74 20 68 61 n if we don't ha
07c0: 76 65 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 ve enough bytes
07d0: 74 6f 20 77 72 69 74 65 0a 09 69 66 20 6c 65 6e to write..if len
07e0: 28 73 77 2e 62 75 66 66 65 72 29 20 3c 20 73 77 (sw.buffer) < sw
07f0: 2e 73 69 7a 65 20 7b 0a 09 09 72 65 74 75 72 6e .size {...return
0800: 20 6c 65 6e 28 69 6e 70 75 74 29 2c 20 6e 69 6c len(input), nil
0810: 0a 09 7d 0a 0a 09 2f 2f 20 46 6c 75 73 68 20 74 ..}...// Flush t
0820: 68 65 20 62 75 66 66 65 72 20 61 73 20 69 74 20 he buffer as it
0830: 69 73 20 66 69 6c 6c 65 64 0a 09 5f 2c 20 65 72 is filled.._, er
0840: 72 20 3d 20 73 77 2e 57 72 69 74 65 28 6e 69 6c r = sw.Write(nil
0850: 29 0a 09 69 66 20 65 72 72 20 21 3d 20 6e 69 6c )..if err != nil
0860: 20 7b 0a 09 09 72 65 74 75 72 6e 20 63 6f 75 6e {...return coun
0870: 74 2c 20 65 72 72 0a 09 7d 0a 0a 09 2f 2f 20 48 t, err..}...// H
0880: 61 6e 64 6c 65 20 74 68 65 20 72 65 73 74 20 6f andle the rest o
0890: 66 20 74 68 65 20 69 6e 70 75 74 0a 09 72 65 74 f the input..ret
08a0: 75 72 6e 20 73 77 2e 57 72 69 74 65 28 69 6e 70 urn sw.Write(inp
08b0: 75 74 5b 63 6f 75 6e 74 3a 5d 29 0a 7d 0a 0a 2f ut[count:]).}../
08c0: 2f 20 52 65 74 75 72 6e 73 20 61 20 72 65 61 64 / Returns a read
08d0: 65 72 20 74 68 61 74 20 64 65 6c 65 67 61 74 65 er that delegate
08e0: 73 20 63 61 6c 6c 73 20 74 6f 20 52 65 61 64 28 s calls to Read(
08f0: 2e 2e 2e 29 20 77 68 69 6c 65 20 65 6e 73 75 72 ...) while ensur
0900: 69 6e 67 0a 2f 2f 20 74 68 61 74 20 74 68 65 20 ing.// that the
0910: 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69 73 output buffer is
0920: 20 6e 65 76 65 72 20 73 6d 61 6c 6c 65 72 20 74 never smaller t
0930: 68 61 6e 20 74 68 65 20 72 65 71 75 69 72 65 64 han the required
0940: 20 73 69 7a 65 0a 2f 2f 20 61 6e 64 20 69 73 20 size.// and is
0950: 64 6f 77 6e 73 69 7a 65 64 20 74 6f 20 61 20 6d downsized to a m
0960: 75 6c 74 69 70 6c 65 20 6f 66 20 74 68 65 20 72 ultiple of the r
0970: 65 71 75 69 72 65 64 20 73 69 7a 65 20 69 66 20 equired size if
0980: 6c 61 72 67 65 72 2e 0a 66 75 6e 63 20 53 69 7a larger..func Siz
0990: 65 64 52 65 61 64 65 72 28 72 65 61 64 65 72 20 edReader(reader
09a0: 69 6f 2e 52 65 61 64 65 72 2c 20 73 69 7a 65 20 io.Reader, size
09b0: 69 6e 74 29 20 69 6f 2e 52 65 61 64 65 72 20 7b int) io.Reader {
09c0: 0a 09 76 61 72 20 73 72 20 73 69 7a 65 64 52 65 ..var sr sizedRe
09d0: 61 64 65 72 0a 09 73 72 2e 72 65 61 64 65 72 20 ader..sr.reader
09e0: 3d 20 72 65 61 64 65 72 0a 09 73 72 2e 62 75 66 = reader..sr.buf
09f0: 66 65 72 20 3d 20 6d 61 6b 65 28 5b 5d 62 79 74 fer = make([]byt
0a00: 65 2c 20 73 69 7a 65 29 0a 09 73 72 2e 73 69 7a e, size)..sr.siz
0a10: 65 2c 20 73 72 2e 66 72 6f 6d 2c 20 73 72 2e 74 e, sr.from, sr.t
0a20: 6f 20 3d 20 73 69 7a 65 2c 20 30 2c 20 30 0a 09 o = size, 0, 0..
0a30: 72 65 74 75 72 6e 20 26 73 72 0a 7d 0a 0a 74 79 return &sr.}..ty
0a40: 70 65 20 73 69 7a 65 64 52 65 61 64 65 72 20 73 pe sizedReader s
0a50: 74 72 75 63 74 20 7b 0a 09 72 65 61 64 65 72 20 truct {..reader
0a60: 20 20 20 20 20 20 20 20 69 6f 2e 52 65 61 64 65 io.Reade
0a70: 72 0a 09 62 75 66 66 65 72 20 20 20 20 20 20 20 r..buffer
0a80: 20 20 5b 5d 62 79 74 65 0a 09 66 72 6f 6d 2c 20 []byte..from,
0a90: 74 6f 2c 20 73 69 7a 65 20 69 6e 74 0a 7d 0a 0a to, size int.}..
0aa0: 66 75 6e 63 20 28 73 72 20 2a 73 69 7a 65 64 52 func (sr *sizedR
0ab0: 65 61 64 65 72 29 20 52 65 61 64 28 6f 75 74 70 eader) Read(outp
0ac0: 75 74 20 5b 5d 62 79 74 65 29 20 28 69 6e 74 2c ut []byte) (int,
0ad0: 20 65 72 72 6f 72 29 20 7b 0a 09 76 61 72 20 28 error) {..var (
0ae0: 0a 09 09 63 6f 75 6e 74 20 69 6e 74 0a 09 09 65 ...count int...e
0af0: 72 72 20 20 20 65 72 72 6f 72 0a 09 29 0a 0a 73 rr error..)..s
0b00: 74 61 72 74 3a 0a 09 2f 2f 20 52 65 70 6c 79 20 tart:..// Reply
0b10: 77 69 74 68 20 74 68 65 20 62 75 66 66 65 72 65 with the buffere
0b20: 64 20 64 61 74 61 20 69 66 20 74 68 65 72 65 20 d data if there
0b30: 69 73 20 61 6e 79 0a 09 69 66 20 73 72 2e 74 6f is any..if sr.to
0b40: 20 3e 20 30 20 7b 0a 09 09 63 6f 75 6e 74 20 3d > 0 {...count =
0b50: 20 63 6f 70 79 28 6f 75 74 70 75 74 2c 20 73 72 copy(output, sr
0b60: 2e 62 75 66 66 65 72 5b 73 72 2e 66 72 6f 6d 3a .buffer[sr.from:
0b70: 73 72 2e 74 6f 5d 29 0a 0a 09 09 2f 2f 20 41 64 sr.to])....// Ad
0b80: 76 61 6e 63 65 20 74 68 65 20 64 61 74 61 20 69 vance the data i
0b90: 6e 20 74 68 65 20 62 75 66 66 65 72 0a 09 09 73 n the buffer...s
0ba0: 72 2e 66 72 6f 6d 20 2b 3d 20 63 6f 75 6e 74 0a r.from += count.
0bb0: 0a 09 09 2f 2f 20 43 68 65 63 6b 20 77 68 65 74 ...// Check whet
0bc0: 68 65 72 20 77 65 20 68 61 76 65 20 72 65 61 63 her we have reac
0bd0: 68 65 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74 hed the end of t
0be0: 68 65 20 62 75 66 66 65 72 0a 09 09 69 66 20 73 he buffer...if s
0bf0: 72 2e 66 72 6f 6d 20 3d 3d 20 73 72 2e 74 6f 20 r.from == sr.to
0c00: 7b 0a 09 09 09 2f 2f 20 52 65 73 65 74 20 74 68 {....// Reset th
0c10: 65 20 62 75 66 66 65 72 0a 09 09 09 73 72 2e 66 e buffer....sr.f
0c20: 72 6f 6d 2c 20 73 72 2e 74 6f 20 3d 20 30 2c 20 rom, sr.to = 0,
0c30: 30 0a 0a 09 09 09 72 65 74 75 72 6e 20 63 6f 75 0.....return cou
0c40: 6e 74 2c 20 65 72 72 0a 09 09 7d 0a 0a 09 09 2f nt, err...}..../
0c50: 2f 20 44 6f 20 6e 6f 74 20 70 72 6f 70 61 67 61 / Do not propaga
0c60: 74 65 20 61 6e 20 65 72 72 6f 72 20 75 6e 74 69 te an error unti
0c70: 6c 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 l the buffer is
0c80: 65 78 68 61 75 73 74 65 64 0a 09 09 72 65 74 75 exhausted...retu
0c90: 72 6e 20 63 6f 75 6e 74 2c 20 6e 69 6c 0a 09 7d rn count, nil..}
0ca0: 0a 0a 09 2f 2f 20 44 65 6c 65 67 61 74 65 20 69 ...// Delegate i
0cb0: 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20 f the buffer is
0cc0: 65 6d 70 74 79 20 61 6e 64 20 74 68 65 20 64 65 empty and the de
0cd0: 73 74 69 6e 61 74 69 6f 6e 20 62 75 66 66 65 72 stination buffer
0ce0: 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 is large enough
0cf0: 0a 09 69 66 20 6c 65 6e 28 6f 75 74 70 75 74 29 ..if len(output)
0d00: 20 3e 3d 20 73 72 2e 73 69 7a 65 20 7b 0a 09 09 >= sr.size {...
0d10: 72 65 74 75 72 6e 20 73 72 2e 72 65 61 64 65 72 return sr.reader
0d20: 2e 52 65 61 64 28 6f 75 74 70 75 74 5b 3a 28 6c .Read(output[:(l
0d30: 65 6e 28 6f 75 74 70 75 74 29 2f 73 72 2e 73 69 en(output)/sr.si
0d40: 7a 65 29 2a 73 72 2e 73 69 7a 65 5d 29 0a 09 7d ze)*sr.size])..}
0d50: 0a 0a 09 2f 2f 20 50 65 72 66 6f 72 6d 20 61 20 ...// Perform a
0d60: 72 65 61 64 20 69 6e 74 6f 20 74 68 65 20 62 75 read into the bu
0d70: 66 66 65 72 0a 09 63 6f 75 6e 74 2c 20 65 72 72 ffer..count, err
0d80: 20 3d 20 73 72 2e 72 65 61 64 65 72 2e 52 65 61 = sr.reader.Rea
0d90: 64 28 73 72 2e 62 75 66 66 65 72 29 0a 0a 09 2f d(sr.buffer).../
0da0: 2f 20 53 69 7a 65 20 74 68 65 20 62 75 66 66 65 / Size the buffe
0db0: 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72 65 r down to the re
0dc0: 61 64 20 64 61 74 61 20 73 69 7a 65 0a 09 2f 2f ad data size..//
0dd0: 20 61 6e 64 20 72 65 73 74 61 72 74 20 69 66 20 and restart if
0de0: 77 65 20 68 61 76 65 20 73 75 63 63 65 73 73 66 we have successf
0df0: 75 6c 6c 79 20 72 65 61 64 20 73 6f 6d 65 20 62 ully read some b
0e00: 79 74 65 73 0a 09 73 72 2e 66 72 6f 6d 2c 20 73 ytes..sr.from, s
0e10: 72 2e 74 6f 20 3d 20 30 2c 20 63 6f 75 6e 74 0a r.to = 0, count.
0e20: 09 69 66 20 73 72 2e 74 6f 20 3e 20 30 20 7b 0a .if sr.to > 0 {.
0e30: 09 09 67 6f 74 6f 20 73 74 61 72 74 0a 09 7d 0a ..goto start..}.
0e40: 0a 09 2f 2f 20 52 65 74 75 72 6e 69 6e 67 20 6f ..// Returning o
0e50: 6e 20 65 72 72 2f 6d 69 73 62 65 68 61 76 69 6e n err/misbehavin
0e60: 67 20 6e 6f 6f 70 20 72 65 61 64 65 72 0a 09 72 g noop reader..r
0e70: 65 74 75 72 6e 20 30 2c 20 65 72 72 0a 7d 0a eturn 0, err.}.