MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / ReadBodyRange

Method ReadBodyRange

internal/caches/reader_memory.go:145–199  ·  view source on GitHub ↗
(buf []byte, start int64, end int64, callback ReaderFunc)

Source from the content-addressed store, hash-verified

143}
144
145func (this *MemoryReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error {
146 offset := start
147 bodySize := int64(len(this.item.BodyValue))
148 if start < 0 {
149 offset = bodySize + end
150 end = bodySize - 1
151 } else if end < 0 {
152 offset = start
153 end = bodySize - 1
154 }
155
156 if end >= bodySize {
157 end = bodySize - 1
158 }
159
160 if offset < 0 || end < 0 || offset > end {
161 return ErrInvalidRange
162 }
163
164 newData := this.item.BodyValue[offset : end+1]
165
166 l := len(buf)
167 if l == 0 {
168 return errors.New("using empty buffer")
169 }
170
171 size := len(newData)
172 offset2 := 0
173 for {
174 left := size - offset2
175 if l <= left {
176 copy(buf, newData[offset2:offset2+l])
177 goNext, e := callback(l)
178 if e != nil {
179 return e
180 }
181 if !goNext {
182 break
183 }
184 } else {
185 copy(buf, newData[offset2:])
186 _, e := callback(left)
187 if e != nil {
188 return e
189 }
190 break
191 }
192 offset2 += l
193 if offset2 >= size {
194 break
195 }
196 }
197
198 return nil
199}
200
201// ContainsRange 是否包含某些区间内容
202func (this *MemoryReader) ContainsRange(r rangeutils.Range) (r2 rangeutils.Range, ok bool) {

Callers 1

Calls

no outgoing calls

Tested by 1