clj-commons / clj-commons/gloss
Using gloss.io/contiguous with a single non-zero position ByteBuffer loses data.
- Dominant language
- Clojure
- Stars
- 486
- Forks
- 53
- PR merge metrics
- No merged PRs in 30d
Description
When using gloss.io/contiguous as a convenience for rolling a sequence of ByteBuffer into a single buffer I've found that:
- if my sequence contains only a single ByteBuffer
- that buffer has a non-zero position
then the output loses as many bytes from the end as the position is offset from the start.
e.g.
Two test buffers:
```
(def buff-a (gi/to-byte-buffer "some text "))
=> (var test/buff-a)
(def buff-b (gi/to-byte-buffer "more, then end!"))
=> (var test/buff-b)
buff-a
=> #
buff-b
=> #
```
Apply contiguous, result is as expected:
```
(gi/contiguous [buff-a buff-b])
=> #
(gi/decode (gc/string :utf-8) *1)
=> "some text more, then end!"
```
Reset the buffers, set position on both, apply contiguous.
Result is as expected:
```
(def buff-a (gi/to-byte-buffer "some text "))
=> (var test/buff-a)
(def buff-b (gi/to-byte-buffer "more, then end!"))
=> (var test/buff-b)
(.position buff-a 2)
=> #
(.position buff-b 4)
=> #
(gi/contiguous [buff-a buff-b])
=> #
(gi/decode (gc/string :utf-8) *1)
=> "me text , then end!"
```
Reset buff-a, set position, apply contiguous.
Result is two bytes short, missing from the end:
```
(def buff-a (gi/to-byte-buffer "some text"))
=> (var test/buff-a)
(.position buff-a 2)
=> #
(gi/contiguous [buff-a])
=> #
(gi/decode (gc/string :utf-8) *1)
=> "me te"
```
As long as there is a second buffer in the sequence, this isn't an issue:
```
(def buff-a (gi/to-byte-buffer "some text"))
=> (var test/buff-a)
(.position buff-a 2)
=> #
(gi/contiguous [buff-a (gi/to-byte-buffer "")])
=> #
(gi/decode (gc/string :utf-8) *1)
=> "me text"
```
TL:DR; If using gloss.io/contiguous with non-zero position ByteBuffer sequences you might lose data.
When I get a moment I'll see if I can figure out why and raise a PR.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the single-buffer case at gloss.io/contiguous with a non-zero ByteBuffer position, then inspect the contiguous implementation and its existing tests. Done means the output retains all bytes from the current position through the limit, including when the sequence contains only one buffer, without breaking the multi-buffer cases shown.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- clojure
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100