chaokunyang commented on code in PR #3374:
URL: https://github.com/apache/fory/pull/3374#discussion_r2867106901


##########
go/fory/buffer.go:
##########
@@ -28,12 +29,76 @@ type ByteBuffer struct {
        data        []byte // Most accessed field first for cache locality
        writerIndex int
        readerIndex int
+       reader      io.Reader
+       minCap      int
 }
 
 func NewByteBuffer(data []byte) *ByteBuffer {
        return &ByteBuffer{data: data}
 }
 
+func NewByteBufferFromReader(r io.Reader, minCap int) *ByteBuffer {
+       if minCap <= 0 {
+               minCap = 4096
+       }
+       return &ByteBuffer{
+               data:   make([]byte, 0, minCap),
+               reader: r,
+               minCap: minCap,
+       }
+}
+
+//go:noinline
+func (b *ByteBuffer) fill(n int) bool {
+       if b.reader == nil {
+               return false
+       }
+
+       available := len(b.data) - b.readerIndex
+       if available >= n {
+               return true
+       }
+
+       if b.readerIndex > 0 {
+               copy(b.data, b.data[b.readerIndex:])
+               b.writerIndex -= b.readerIndex
+               b.readerIndex = 0
+               b.data = b.data[:b.writerIndex]
+       }
+
+       if cap(b.data) < n {
+               newCap := cap(b.data) * 2
+               if newCap < n {
+                       newCap = n
+               }
+               if newCap < b.minCap {
+                       newCap = b.minCap
+               }
+               newData := make([]byte, len(b.data), newCap)
+               copy(newData, b.data)
+               b.data = newData
+       }
+
+       for len(b.data) < n {
+               spare := b.data[len(b.data):cap(b.data)]
+               if len(spare) == 0 {
+                       return false
+               }
+               readBytes, err := b.reader.Read(spare)
+               if readBytes > 0 {
+                       b.data = b.data[:len(b.data)+readBytes]
+                       b.writerIndex += readBytes
+               }
+               if err != nil {

Review Comment:
   `fill` currently folds reader errors into `false`, and callers then emit 
`BufferOutOfBoundError`. This masks non-EOF transport failures (for example 
connection reset) as bounds issues. Please preserve/propagate non-EOF read 
errors so stream deserialization reports the real I/O failure.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to