TeamNewPipe/NewPipe · error · EOFException

Truncated stream, missing {} bytes

Error message

Truncated stream, missing {} bytes

What it means

DataReader.primitiveRead tries to read a fixed number of bytes (2, 4, or 8) for a primitive Java type (short/int/long) but the underlying SharpStream returned fewer bytes than requested. The EOFException reports how many bytes are missing (amount - read). It fires from the internal primitive-read path used by readShort/readInt/readLong/readFloat, so any truncated or prematurely closed media stream surfaces here.

Source

Thrown at app/src/main/java/org/schabi/newpipe/streams/DataReader.java:238

                public boolean markSupported() {
                    return false;
                }

            };
        }
        viewSize = size;

        return view;
    }

    private final short[] primitive = new short[LONG_SIZE];

    private void primitiveRead(final int amount) throws IOException {
        final byte[] buffer = new byte[amount];
        final int read = read(buffer, 0, amount);

        if (read != amount) {
            throw new EOFException("Truncated stream, missing "
                    + (amount - read) + " bytes");
        }

        for (int i = 0; i < amount; i++) {
            // the "byte" data type in java is signed and is very annoying
            primitive[i] = (short) (buffer[i] & 0xFF);
        }
    }

    private final byte[] readBuffer = new byte[BUFFER_SIZE];
    private int readOffset;
    private int readCount;

    private boolean fillBuffer() throws IOException {
        if (readCount < 0) {
            return true;
        }
        if (readOffset >= readBuffer.length) {

View on GitHub (pinned to 9e8be09156)

Solutions

  1. Verify the source stream length before parsing; reject inputs whose reported size is smaller than a minimum valid header (e.g. compare against expected atom/element sizes).
  2. Re-fetch or re-download the media file — a truncated stream is almost always a corrupted/incomplete download.
  3. If reading from a pipe/network socket, ensure the producer fully flushes and closes before the consumer begins primitive reads.
  4. Catch EOFException at the parse entry point and report a user-facing 'file is incomplete' message rather than crashing.

Example fix

// before
DataReader dr = new DataReader(source);
long size = dr.readLong(); // throws if source truncated mid-8-bytes

// after — guard with available()/length first
if (!source.isLengthKnown() || source.length() < LONG_SIZE) {
    throw new IOException("source too short to be a valid container");
}
long size = dr.readLong();
Defensive patterns

Strategy: validation

Validate before calling

// Before constructing DataReader, confirm the source has enough bytes for the operation.
if (!source.isLengthKnown() || source.length() < minHeaderBytes) {
    throw new IOException("source too short to parse a valid container");
}
DataReader dr = new DataReader(source);

Type guard

// Java has no opaque 'maybe-truncated' type; encode the contract in a wrapper.
public final class LengthVerifiedStream extends SharpStream {
    private final SharpStream inner;
    private final long minLen;
    public LengthVerifiedStream(SharpStream inner, long minLen) {
        if (inner.isLengthKnown() && inner.length() < minLen)
            throw new IllegalArgumentException("source shorter than " + minLen);
        this.inner = inner; this.minLen = minLen;
    }
    // delegate read()/skip() ...
}

Try / catch

try {
    long v = dr.readLong();
} catch (EOFException e) {
    // truncated primitive read — source is incomplete, do not retry the same stream
    throw new IOException("media stream truncated: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Calling readShort()/readInt()/readLong()/readFloat() on a DataReader whose underlying SharpStream reaches EOF mid-primitive. This occurs when Mp4DashReader or WebMReader reads a box/element header (size, type, encoded length) and the source ends before the full primitive is delivered.

Common situations: An incomplete download whose byte count is short; a network stream interrupted mid-transfer; a cached file truncated by disk-full or a previous failed write; a server serving a Content-Length larger than the actual body. Most often seen on flaky mobile connections where the demuxer starts before the full payload arrives.

Related errors


AI-assisted analysis of TeamNewPipe/NewPipe@9e8be09156 (2026-08-14). Data as JSON: /api/errors/2dff472d27d43943. Report an issue: GitHub.