TeamNewPipe/NewPipe · error · IOException

Invalid encoded length

Error message

Invalid encoded length

What it means

WebMReader.readEncodedNumber reads the EBML variable-length integer by scanning the first byte for the leading 1-bit length marker (the position of the first set bit indicates how many bytes the number spans). If after 8 bytes no marker bit is found (all-zero bytes), the encoding is invalid and IOException 'Invalid encoded length' is thrown. Every EBML element ID and size uses this VINT encoding.

Source

Thrown at app/src/main/java/org/schabi/newpipe/streams/WebMReader.java:167

                    mask >>= size;

                    long number = value & mask;

                    for (int i = 1; i < size; i++) {
                        value = stream.read();
                        number <<= 8;
                        number |= value;
                    }

                    return number;
                }

                mask >>= 1;
                size++;
            }
        }

        throw new IOException("Invalid encoded length");
    }

    private Element readElement() throws IOException {
        final Element elem = new Element();
        elem.offset = stream.position();
        elem.type = (int) readEncodedNumber();
        elem.contentSize = readEncodedNumber();
        elem.size = elem.contentSize + stream.position() - elem.offset;

        return elem;
    }

    private Element readElement(final int expected) throws IOException {
        final Element elem = readElement();
        if (expected != 0 && elem.type != expected) {
            throw new NoSuchElementException("expected " + elementID(expected)
                    + " found " + elementID(elem.type));
        }

View on GitHub (pinned to 9e8be09156)

Solutions

  1. Re-download or re-verify the file; the EBML structure is corrupt at the current offset.
  2. Confirm the input is genuinely a WebM/Matroska file before parsing (check EBML magic 0x1A45DFA3).
  3. If you control muxing, ensure no all-zero VINT bytes are emitted and element sizes are correctly encoded.
  4. Catch IOException and report a corruption offset (stream.position()) for diagnosis.

Example fix

// before — no validation, parser desyncs into zero bytes
new WebMReader(mysteryStream).parse(); // IOException: Invalid encoded length

// after — verify EBML magic before parsing
byte[] magic = new byte[4];
mysteryStream.read(magic);
if ((magic[0]&0xFF)!=0x1A||(magic[1]&0xFF)!=0x45||(magic[2]&0xFF)!=0xDF||(magic[3]&0xFF)!=0xA3) {
    throw new IOException("not a WebM/Matroska file");
}
Defensive patterns

Strategy: validation

Validate before calling

// Verify EBML magic before parsing to avoid desyncing into zero bytes.
byte[] magic = new byte[4];
source.read(magic);
if (!hasEbmlMagic(magic)) throw new IOException("not a WebM/Matroska file");
// Also ensure the file is not just zero-padding.

Type guard

public static boolean isValidVintLeadingByte(byte b) {
    return (b & 0xFF) != 0; // a valid VINT lead byte has at least one set bit
}

Try / catch

try {
    reader.parse();
} catch (IOException e) {
    if (e.getMessage().contains("Invalid encoded length")) {
        // parser desynced into garbage/zeroes — re-download or re-mux
        Log.w(TAG, "EBML VINT corrupt at " + e.getMessage());
        redownloadOrRemux();
    } else throw e;
}

Prevention

When it happens

Trigger: Reading an EBML element ID or size where the leading byte(s) are all zero (no length marker). Happens when the stream is misaligned, contains zero-padding, or the data is not WebM/EBML at the current position.

Common situations: The parser is desynchronized after a prior corrupt element and now reads padding/garbage as a VINT; a file with unknown-size elements not handled; a stream that is not actually WebM but was passed as one; a truncated header full of zero bytes.

Related errors


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