You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
6.6 KiB

  1. # fwd
  2. import "github.com/philhofer/fwd"
  3. The `fwd` package provides a buffered reader
  4. and writer. Each has methods that help improve
  5. the encoding/decoding performance of some binary
  6. protocols.
  7. The `fwd.Writer` and `fwd.Reader` type provide similar
  8. functionality to their counterparts in `bufio`, plus
  9. a few extra utility methods that simplify read-ahead
  10. and write-ahead. I wrote this package to improve serialization
  11. performance for <a href="http://github.com/tinylib/msgp">http://github.com/tinylib/msgp</a>,
  12. where it provided about a 2x speedup over `bufio` for certain
  13. workloads. However, care must be taken to understand the semantics of the
  14. extra methods provided by this package, as they allow
  15. the user to access and manipulate the buffer memory
  16. directly.
  17. The extra methods for `fwd.Reader` are `Peek`, `Skip`
  18. and `Next`. `(*fwd.Reader).Peek`, unlike `(*bufio.Reader).Peek`,
  19. will re-allocate the read buffer in order to accommodate arbitrarily
  20. large read-ahead. `(*fwd.Reader).Skip` skips the next `n` bytes
  21. in the stream, and uses the `io.Seeker` interface if the underlying
  22. stream implements it. `(*fwd.Reader).Next` returns a slice pointing
  23. to the next `n` bytes in the read buffer (like `Peek`), but also
  24. increments the read position. This allows users to process streams
  25. in arbitrary block sizes without having to manage appropriately-sized
  26. slices. Additionally, obviating the need to copy the data from the
  27. buffer to another location in memory can improve performance dramatically
  28. in CPU-bound applications.
  29. `fwd.Writer` only has one extra method, which is `(*fwd.Writer).Next`, which
  30. returns a slice pointing to the next `n` bytes of the writer, and increments
  31. the write position by the length of the returned slice. This allows users
  32. to write directly to the end of the buffer.
  33. ## Constants
  34. ``` go
  35. const (
  36. // DefaultReaderSize is the default size of the read buffer
  37. DefaultReaderSize = 2048
  38. )
  39. ```
  40. ``` go
  41. const (
  42. // DefaultWriterSize is the
  43. // default write buffer size.
  44. DefaultWriterSize = 2048
  45. )
  46. ```
  47. ## type Reader
  48. ``` go
  49. type Reader struct {
  50. // contains filtered or unexported fields
  51. }
  52. ```
  53. Reader is a buffered look-ahead reader
  54. ### func NewReader
  55. ``` go
  56. func NewReader(r io.Reader) *Reader
  57. ```
  58. NewReader returns a new *Reader that reads from 'r'
  59. ### func NewReaderSize
  60. ``` go
  61. func NewReaderSize(r io.Reader, n int) *Reader
  62. ```
  63. NewReaderSize returns a new *Reader that
  64. reads from 'r' and has a buffer size 'n'
  65. ### func (\*Reader) BufferSize
  66. ``` go
  67. func (r *Reader) BufferSize() int
  68. ```
  69. BufferSize returns the total size of the buffer
  70. ### func (\*Reader) Buffered
  71. ``` go
  72. func (r *Reader) Buffered() int
  73. ```
  74. Buffered returns the number of bytes currently in the buffer
  75. ### func (\*Reader) Next
  76. ``` go
  77. func (r *Reader) Next(n int) ([]byte, error)
  78. ```
  79. Next returns the next 'n' bytes in the stream.
  80. Unlike Peek, Next advances the reader position.
  81. The returned bytes point to the same
  82. data as the buffer, so the slice is
  83. only valid until the next reader method call.
  84. An EOF is considered an unexpected error.
  85. If an the returned slice is less than the
  86. length asked for, an error will be returned,
  87. and the reader position will not be incremented.
  88. ### func (\*Reader) Peek
  89. ``` go
  90. func (r *Reader) Peek(n int) ([]byte, error)
  91. ```
  92. Peek returns the next 'n' buffered bytes,
  93. reading from the underlying reader if necessary.
  94. It will only return a slice shorter than 'n' bytes
  95. if it also returns an error. Peek does not advance
  96. the reader. EOF errors are *not* returned as
  97. io.ErrUnexpectedEOF.
  98. ### func (\*Reader) Read
  99. ``` go
  100. func (r *Reader) Read(b []byte) (int, error)
  101. ```
  102. Read implements `io.Reader`
  103. ### func (\*Reader) ReadByte
  104. ``` go
  105. func (r *Reader) ReadByte() (byte, error)
  106. ```
  107. ReadByte implements `io.ByteReader`
  108. ### func (\*Reader) ReadFull
  109. ``` go
  110. func (r *Reader) ReadFull(b []byte) (int, error)
  111. ```
  112. ReadFull attempts to read len(b) bytes into
  113. 'b'. It returns the number of bytes read into
  114. 'b', and an error if it does not return len(b).
  115. EOF is considered an unexpected error.
  116. ### func (\*Reader) Reset
  117. ``` go
  118. func (r *Reader) Reset(rd io.Reader)
  119. ```
  120. Reset resets the underlying reader
  121. and the read buffer.
  122. ### func (\*Reader) Skip
  123. ``` go
  124. func (r *Reader) Skip(n int) (int, error)
  125. ```
  126. Skip moves the reader forward 'n' bytes.
  127. Returns the number of bytes skipped and any
  128. errors encountered. It is analogous to Seek(n, 1).
  129. If the underlying reader implements io.Seeker, then
  130. that method will be used to skip forward.
  131. If the reader encounters
  132. an EOF before skipping 'n' bytes, it
  133. returns io.ErrUnexpectedEOF. If the
  134. underlying reader implements io.Seeker, then
  135. those rules apply instead. (Many implementations
  136. will not return `io.EOF` until the next call
  137. to Read.)
  138. ### func (\*Reader) WriteTo
  139. ``` go
  140. func (r *Reader) WriteTo(w io.Writer) (int64, error)
  141. ```
  142. WriteTo implements `io.WriterTo`
  143. ## type Writer
  144. ``` go
  145. type Writer struct {
  146. // contains filtered or unexported fields
  147. }
  148. ```
  149. Writer is a buffered writer
  150. ### func NewWriter
  151. ``` go
  152. func NewWriter(w io.Writer) *Writer
  153. ```
  154. NewWriter returns a new writer
  155. that writes to 'w' and has a buffer
  156. that is `DefaultWriterSize` bytes.
  157. ### func NewWriterSize
  158. ``` go
  159. func NewWriterSize(w io.Writer, size int) *Writer
  160. ```
  161. NewWriterSize returns a new writer
  162. that writes to 'w' and has a buffer
  163. that is 'size' bytes.
  164. ### func (\*Writer) BufferSize
  165. ``` go
  166. func (w *Writer) BufferSize() int
  167. ```
  168. BufferSize returns the maximum size of the buffer.
  169. ### func (\*Writer) Buffered
  170. ``` go
  171. func (w *Writer) Buffered() int
  172. ```
  173. Buffered returns the number of buffered bytes
  174. in the reader.
  175. ### func (\*Writer) Flush
  176. ``` go
  177. func (w *Writer) Flush() error
  178. ```
  179. Flush flushes any buffered bytes
  180. to the underlying writer.
  181. ### func (\*Writer) Next
  182. ``` go
  183. func (w *Writer) Next(n int) ([]byte, error)
  184. ```
  185. Next returns the next 'n' free bytes
  186. in the write buffer, flushing the writer
  187. as necessary. Next will return `io.ErrShortBuffer`
  188. if 'n' is greater than the size of the write buffer.
  189. Calls to 'next' increment the write position by
  190. the size of the returned buffer.
  191. ### func (\*Writer) ReadFrom
  192. ``` go
  193. func (w *Writer) ReadFrom(r io.Reader) (int64, error)
  194. ```
  195. ReadFrom implements `io.ReaderFrom`
  196. ### func (\*Writer) Write
  197. ``` go
  198. func (w *Writer) Write(p []byte) (int, error)
  199. ```
  200. Write implements `io.Writer`
  201. ### func (\*Writer) WriteByte
  202. ``` go
  203. func (w *Writer) WriteByte(b byte) error
  204. ```
  205. WriteByte implements `io.ByteWriter`
  206. ### func (\*Writer) WriteString
  207. ``` go
  208. func (w *Writer) WriteString(s string) (int, error)
  209. ```
  210. WriteString is analogous to Write, but it takes a string.
  211. - - -
  212. Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)