public int foreach(BitMap position, BytePieceSetter setter) {
        if (position == BitMap.ALL) {
            foreach(0, objCount, setter);
            return objCount;
        }
        DirectBitMapIterator posIterator = position.iterator();
        int count = 0;
        BytePiece bytes = new BytePiece();
        int index;

        switch (version) {
            case Version.VERSION_0_ID:
                while ((index = posIterator.nextSetBit()) != BitSetIterator.NO_MORE) {
                    if (index >= objCount) {
                        break;
                    }

                    long startAddr, endAddr;
                    if (maxObjLen == 0) {
                        startAddr = 0;
                        endAddr = 0;
                    } else {
                        long indexAddr = dataAddr + (index << 3) + 4;
                        int str_offset = (objCount << 3) + 4;
                        startAddr = MemoryUtil.getInt(indexAddr) + dataAddr + str_offset;
                        endAddr = MemoryUtil.getInt(indexAddr + 4) + dataAddr + str_offset;
                    }

                    bytes.addr = startAddr;
                    bytes.len = (int) (endAddr - startAddr);
                    setter.set(index, bytes);

                    count++;
                }
                break;
            default:
                while ((index = posIterator.nextSetBit()) != BitSetIterator.NO_MORE) {
                    if (index >= objCount) {
                        break;
                    }

                    long startAddr, endAddr;

                    long indexAddr = dataAddr + (index << 2);
                    int str_offset = (objCount + 1) << 2;
                    startAddr = MemoryUtil.getInt(indexAddr) + dataAddr + str_offset;
                    endAddr = MemoryUtil.getInt(indexAddr + 4) + dataAddr + str_offset;

                    bytes.addr = startAddr;
                    bytes.len = (int) (endAddr - startAddr);
                    setter.set(index, bytes);

                    count++;
                }
        }

        return count;
    }
Note that this method could update the content of dpn.
Make sure to call this method before you actually save the dpn instance to storage.
indexr-segment/src/main/java/io/indexr/segment/pack/DataPack.java