jarvis657 / goprotobuf

Automatically exported from code.google.com/p/goprotobuf
Other
0 stars 0 forks source link

API defficiency: Buffer needs a function that detects EOF or remaining length #53

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The Buffer type is quite useful for building my own custom encoding format by 
reusing the protobuf's encoding functions for primitive types. But when 
decoding, there is no way to gracefully detect when an input buffer has been 
entirely consumed. Could a function be added to fix this deficiency?

Within goprotobuf, Buffer.index is used to detect end-of-input, but that field 
is private. Public Buffer functions return io.UnexpectedEOF if the buffer was 
already entirely consumed, but they return the same error in a lot of other 
cases (e.g. if a partial message is present).

I think that Buffer.Bytes() should return only the unconsumed portion of the 
buffer, so len(buf.Bytes()) can be used to see how much input is left. This is 
more in keeping with the bytes.Buffer API. I don't see that Buffer.Bytes() is 
used much inside goprotobuf, but it does mean breaking the API.

Alternatively, Buffer.index should be public or there should be a way to query 
its current value.

Original issue reported on code.google.com by kwa...@holycross.edu on 10 Aug 2014 at 2:15

GoogleCodeExporter commented 9 years ago
Protocol buffers are not a streaming format. You pass in a chunk of bytes, and 
you get a decoded message if and only if all the bytes were parsed. If you need 
to parse a stream then you need a separate protocol to take care of that.

The Buffer type is not meant to be used externally; it is unfortunate that it 
was exposed.

Original comment by dsymo...@golang.org on 24 Nov 2014 at 12:17