apache/hadoop · error · EOFException

Cannot seek to a negative offset

Error message

Cannot seek to a negative offset

What it means

skip(offset) on ByteBufferInputStream computes newPos = position() + offset. If newPos is negative it throws EOFException('Cannot seek to a negative offset'). Unlike java.io.InputStream.skip, negative skips are not silently ignored.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/ByteBufferInputStream.java:107

  private void checkOpenState() {
    Preconditions.checkState(isOpen(),
        FSExceptionMessages.STREAM_IS_CLOSED);
  }

  public synchronized int read() throws IOException {
    if (available() > 0) {
      return byteBuffer.get() & 0xFF;
    } else {
      return -1;
    }
  }

  @Override
  public synchronized long skip(long offset) throws IOException {
    verifyOpen();
    long newPos = position() + offset;
    if (newPos < 0) {
      throw new EOFException(FSExceptionMessages.NEGATIVE_SEEK);
    }
    if (newPos > size) {
      throw new EOFException(FSExceptionMessages.CANNOT_SEEK_PAST_EOF);
    }
    byteBuffer.position((int) newPos);
    return newPos;
  }

  @Override
  public synchronized int available() {
    checkOpenState();
    return byteBuffer.remaining();
  }

  /**
   * Get the current buffer position.
   * @return the buffer position
   */

View on GitHub (pinned to 2add963021)

Solutions

  1. Clamp before skipping: long toSkip = Math.max(0, target - in.position())
  2. Go to the start via position(0)-style repositioning rather than a large negative skip
  3. Validate target >= 0 before computing any skip delta

Example fix

// before
long toSkip = targetPos - in.position(); // may be very negative
in.skip(toSkip); // EOFException

// after
long toSkip = Math.max(0, targetPos - in.position());
if (toSkip > 0) {
  in.skip(toSkip);
}
Defensive patterns

Strategy: validation

Validate before calling

long target = Math.max(0, requestedPos);
long toSkip = target - in.position();
if (toSkip > 0) {
  in.skip(toSkip);
}

Try / catch

Catch EOFException from skip(); a 'Cannot seek to a negative offset' message means the offset arithmetic is wrong upstream — fix the caller, do not swallow.

Prevention

When it happens

Trigger: Calling skip with a negative offset larger than the current position (position 3, skip(-10)); computing offsets as (target - current) where target can be below zero.

Common situations: Backward-seeking logic ported from RandomAccessFile; integer underflow when offset = target - position with target < position; cursors from external indexes that go below zero.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/dd00cf6e7f28b2c8. Report an issue: GitHub.