I used now a custom iterator:
@RequiredArgsConstructor
public class ChunkedByteStringIterator implements Iterator<ByteString> {
private final byte[] bytes;
private final int chunks;
private int offset = 0;
@Override
public boolean hasNext() {
return bytes != null && bytes.length > offset;
}
@Override
public ByteString next() {
int currentOffset = offset;
offset += chunks;
if (currentOffset + chunks >= bytes.length) {
return ByteString.fromArrayUnsafe(bytes, currentOffset, bytes.length - currentOffset);
}
return ByteString.fromArrayUnsafe(bytes, currentOffset, chunks);
}
}
The given byte array is effectively immutable.
in a stream, it can be used like that:
Source.fromIterator(() -> new ChunkedByteStringIterator(..)).via(anyBusiness())
What do you think about that?