public void seek(long position) throws IOException {
        final int block = MathUtils.checkedDownCast(position / segmentSize);
        final int positionInBlock = (int) (position % segmentSize);

        if (position < 0
                || block >= numBlocksTotal
                || (block == numBlocksTotal - 1 && positionInBlock > sizeOfLastBlock)) {
            throw new IllegalArgumentException("Position is out of range");
        }

        clear();
        if (reader != null) {
            reader.close();
        }

        reader = ioManager.createBlockChannelReader(channelId);

        if (block > 0) {
            reader.seekToPosition(((long) block) * segmentSize);
        }

        this.numBlocksRemaining = this.numBlocksTotal - block;
        this.numRequestsRemaining = numBlocksRemaining;

        for (int i = 0; i < memory.size(); i++) {
            sendReadRequest(memory.get(i));
        }

        numBlocksRemaining--;
        seekInput(
                reader.getNextReturnedBlock(),
                positionInBlock,
                numBlocksRemaining == 0 ? sizeOfLastBlock : segmentSize);
    }
@Test
    void testSeek() throws Exception {
        final int PAGE_SIZE = 16 * 1024;
        final int NUM_RECORDS = 120000;
        // integers across 7.x pages (7 pages = 114.688 bytes, 8 pages = 131.072 bytes)

        try (IOManager ioManager = new IOManagerAsync()) {
            MemoryManager memMan =
                    MemoryManagerBuilder.newBuilder()
                            .setMemorySize(4 * PAGE_SIZE)
                            .setPageSize(PAGE_SIZE)
                            .build();
            List<MemorySegment> memory = new ArrayList<MemorySegment>();
            memMan.allocatePages(new DummyInvokable(), memory, 4);

            FileIOChannel.ID channel = ioManager.createChannel();
            BlockChannelWriter<MemorySegment> writer = ioManager.createBlockChannelWriter(channel);
            FileChannelOutputView out =
                    new FileChannelOutputView(writer, memMan, memory, memMan.getPageSize());

            // write some integers across 7.5 pages (7 pages = 114.688 bytes, 8 pages = 131.072
            // bytes)
            for (int i = 0; i < NUM_RECORDS; i += 4) {
                out.writeInt(i);
            }
            // close for the first time, make sure all memory returns
            out.close();
            assertThat(memMan.verifyEmpty()).isTrue();

            memMan.allocatePages(new DummyInvokable(), memory, 4);
            SeekableFileChannelInputView in =
                    new SeekableFileChannelInputView(
                            ioManager, channel, memMan, memory, out.getBytesInLatestSegment());

            // read first, complete
            for (int i = 0; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to the middle of the 3rd page
            int i = 2 * PAGE_SIZE + PAGE_SIZE / 4;
            in.seek(i);
            for (; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to the end
            i = 120000 - 4;
            in.seek(i);
            for (; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to the beginning
            i = 0;
            in.seek(i);
            for (; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to after a page
            i = PAGE_SIZE;
            in.seek(i);
            for (; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to after a page
            i = 3 * PAGE_SIZE;
            in.seek(i);
            for (; i < NUM_RECORDS; i += 4) {
                assertThat(in.readInt()).isEqualTo(i);
            }
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek to the end
            i = NUM_RECORDS;
            in.seek(i);
            assertThatThrownBy(in::readInt)
                    .withFailMessage("should throw EOF exception")
                    .isInstanceOf(EOFException.class);

            // seek out of bounds
            assertThatThrownBy(() -> in.seek(-10))
                    .withFailMessage("should throw an exception")
                    .isInstanceOf(IllegalArgumentException.class);
            assertThatThrownBy(() -> in.seek(NUM_RECORDS + 1))
                    .withFailMessage("should throw an exception")
                    .isInstanceOf(IllegalArgumentException.class);
        }
    }