{"record":{"id":"84e076c17070e0e7","repo":"TeamNewPipe/NewPipe","slug":"expected-found","errorCode":null,"errorMessage":"expected {} found {}","messagePattern":"expected (.+?) found (.+?)","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java","lineNumber":281,"sourceCode":"    }\n\n    private Box readBox() throws IOException {\n        final Box b = new Box();\n        b.offset = stream.position();\n        b.size = stream.readUnsignedInt();\n        b.type = stream.readInt();\n\n        if (b.size == 1) {\n            b.size = stream.readLong();\n        }\n\n        return b;\n    }\n\n    private Box readBox(final int expected) throws IOException {\n        final Box b = readBox();\n        if (b.type != expected) {\n            throw new NoSuchElementException(\"expected \" + boxName(expected)\n                    + \" found \" + boxName(b));\n        }\n        return b;\n    }\n\n    private byte[] readFullBox(final Box ref) throws IOException {\n        // full box reading is limited to 2 GiB, and should be enough\n        final int size = (int) ref.size;\n\n        final ByteBuffer buffer = ByteBuffer.allocate(size);\n        buffer.putInt(size);\n        buffer.putInt(ref.type);\n\n        final int read = size - 8;\n\n        if (stream.read(buffer.array(), 8, read) != read) {\n            throw new EOFException(String.format(\"EOF reached in box: type=%s offset=%s size=%s\",\n                    boxName(ref.type), ref.offset, ref.size));","sourceCodeStart":263,"sourceCodeEnd":299,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/streams/Mp4DashReader.java#L263-L299","documentation":"Mp4DashReader.readBox(expected) reads a box and compares its 4-byte type against the expected atom constant. If they differ it throws NoSuchElementException naming both expected and found atoms. This is a structural guard used during ftyp parsing and full-box reads where the next box must be a specific type; a mismatch means the file layout diverged from what the parser assumes.","triggerScenarios":"Calling readBox(expected) when the stream position points at a different box type than anticipated — e.g. expecting ATOM_FTYP but finding another atom, or any ordered-box-read where the next box is not the assumed type. Happens when a box is unexpectedly absent/extra/reordered.","commonSituations":"A non-conformant MP4 where boxes are in an unusual order; an extra metadata box inserted by a tagging tool before ftyp; a truncated file whose remaining bytes decode to an unexpected atom type; an encrypted/DRM'd file with altered structure.","solutions":["Re-download/re-mux; the box sequence does not match what this strict reader expects.","Use a more tolerant MP4 inspector (mp4info/MP4Box) to identify which box is misplaced and re-emit the file correctly.","If you only need specific boxes, use untilBox() (which skips unknown boxes) instead of readBox(expected).","Catch NoSuchElementException at the parse boundary and report the structural mismatch."],"exampleFix":"// before — strict read that fails on an unexpected leading box\nBox b = readBox(ATOM_FTYP); // throws if a non-ftyp box leads\n\n// after — skip until the expected box is found\nBox b = untilBox(null, ATOM_FTYP);\nif (b == null) throw new IOException(\"ftyp box missing entirely\");","handlingStrategy":"try-catch","validationCode":"// Prefer untilBox() to skip unknown boxes instead of readBox(expected).\n// Before a strict read, scan forward to the expected box:\nBox b = untilBox(parent, ATOM_FTYP);\nif (b == null) throw new IOException(\"required box ftyp not present\");","typeGuard":"// A box-type narrowing helper:\npublic static Box expectBox(Box b, int expected) throws NoSuchElementException {\n    if (b.type != expected)\n        throw new NoSuchElementException(\"expected \"+boxName(expected)+\" found \"+boxName(b));\n    return b;\n}","tryCatchPattern":"try {\n    Box b = readBox(ATOM_FTYP);\n} catch (NoSuchElementException e) {\n    if (e.getMessage().startsWith(\"expected\")) {\n        // unexpected leading box — try skipping one box and retry, or reject the file\n        Log.w(TAG, \"box order unexpected: \" + e.getMessage());\n    } else throw e;\n}","preventionTips":["Use untilBox() for tolerant scanning; reserve readBox(expected) for invariants.","Re-mux non-conformant files with ffmpeg/MP4Box.","Validate box order with mp4info before strict parsing.","Do not assume a specific box leads the file without sniffing."],"tags":["mp4","container","format-validation","java"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}