type lengthDelimitedFrameWriter … func NewLengthDelimitedFrameWriter(w io.Writer) io.Writer { … } // Write writes a single frame to the nested writer, prepending it with the length // in bytes of data (as a 4 byte, bigendian uint32). func (w *lengthDelimitedFrameWriter) Write(data []byte) (int, error) { … } type lengthDelimitedFrameReader … // NewLengthDelimitedFrameReader returns an io.Reader that will decode length-prefixed // frames off of a stream. // // The protocol is: // // stream: message ... // message: prefix body // prefix: 4 byte uint32 in BigEndian order, denotes length of body // body: bytes (0..prefix) // // If the buffer passed to Read is not long enough to contain an entire frame, io.ErrShortRead // will be returned along with the number of bytes read. func NewLengthDelimitedFrameReader(r io.ReadCloser) io.ReadCloser { … } // Read attempts to read an entire frame into data. If that is not possible, io.ErrShortBuffer // is returned and subsequent calls will attempt to read the last frame. A frame is complete when // err is nil. func (r *lengthDelimitedFrameReader) Read(data []byte) (int, error) { … } func (r *lengthDelimitedFrameReader) Close() error { … } type jsonFrameReader … // NewJSONFramedReader returns an io.Reader that will decode individual JSON objects off // of a wire. // // The boundaries between each frame are valid JSON objects. A JSON parsing error will terminate // the read. func NewJSONFramedReader(r io.ReadCloser) io.ReadCloser { … } // ReadFrame decodes the next JSON object in the stream, or returns an error. The returned // byte slice will be modified the next time ReadFrame is invoked and should not be altered. func (r *jsonFrameReader) Read(data []byte) (int, error) { … } func (r *jsonFrameReader) Close() error { … }