{"record":{"id":"76bc555d02e96fa2","repo":"pxb1988/dex2jar","slug":"file-too-small-to-be-a-dex-zip-76bc55","errorCode":null,"errorMessage":"File too small to be a dex/zip","messagePattern":"File too small to be a dex/zip","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"dex-reader/src/main/java/com/googlecode/d2j/reader/zip/ZipUtil.java","lineNumber":75,"sourceCode":"    public static byte[] readDex(Path file) throws IOException {\n        return readDex(Files.readAllBytes(file));\n    }\n\n    public static byte[] readDex(InputStream in) throws IOException {\n        return readDex(toByteArray(in));\n    }\n\n    /**\n     * read the dex file from byte array, if the byte array is a zip stream, it will return the content of classes.dex\n     * in the zip stream.\n     * \n     * @param data\n     * @return the content of classes.dex\n     * @throws IOException\n     */\n    public static byte[] readDex(byte[] data) throws IOException {\n        if (data.length < 3) {\n            throw new IOException(\"File too small to be a dex/zip\");\n        }\n        if (\"dex\".equals(new String(data, 0, 3, StandardCharsets.ISO_8859_1))) {// dex\n            return data;\n        } else if (\"PK\".equals(new String(data, 0, 2, StandardCharsets.ISO_8859_1))) {// ZIP\n            try (ZipFile zipFile = new ZipFile(data)) {\n                ZipEntry classes = zipFile.findFirstEntry(\"classes.dex\");\n                if (classes != null) {\n                    return toByteArray(zipFile.getInputStream(classes));\n                } else {\n                    throw new IOException(\"Can not find classes.dex in zip file\");\n                }\n            }\n        }\n        throw new IOException(\"the src file not a .dex or zip file\");\n    }\n}\n","sourceCodeStart":57,"sourceCodeEnd":92,"githubUrl":"https://github.com/pxb1988/dex2jar/blob/b5bda4fb4935ae8b3869b422454ae3b3896c7bc1/dex-reader/src/main/java/com/googlecode/d2j/reader/zip/ZipUtil.java#L57-L92","documentation":"ZipUtil.readDex(byte[]) throws IOException when the input byte array is shorter than 3 bytes, since at least 3 bytes are required to test the 'dex' magic or 'PK' zip prefix before extracting classes.dex. The data cannot be a dex or zip, so it is rejected up front.","triggerScenarios":"Calling ZipUtil.readDex(data) with an empty or <3-byte array, typically from an empty file, truncated read, or a failed fetch that produced no bytes.","commonSituations":"Reading a 0-byte APK from a failed download; passing a stream read that returned -1/empty; handing the wrong buffer to readDex.","solutions":["Verify the source file exists and its size > 0 before reading it into bytes.","Re-download/re-extract the APK.","Ensure the byte array is fully populated from the input stream.","Validate data.length before calling readDex."],"exampleFix":"// before\nbyte[] dex = ZipUtil.readDex(data);\n// after\nif (data == null || data.length < 3) {\n    throw new IllegalArgumentException(\"input too small: \" + (data == null ? -1 : data.length));\n}\nbyte[] dex = ZipUtil.readDex(data);","handlingStrategy":"validation","validationCode":"if (data == null || data.length < 3) {\n    throw new IllegalArgumentException(\"cannot readDex from \" + (data == null ? \"null\" : data.length + \" bytes\"));\n}","typeGuard":null,"tryCatchPattern":"try {\n    byte[] dex = ZipUtil.readDex(data);\n} catch (IOException e) {\n    LOG.error(\"readDex failed on undersized input: \" + e.getMessage());\n}","preventionTips":["Verify file size > 0 before loading bytes","Check stream read completion (bytes read == file length)","Guard the call site with a magic-bytes check"],"tags":["dex","android","zip","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"b5bda4fb4935ae8b3869b422454ae3b3896c7bc1","analyzedAt":"2026-09-08T00:44:01.258Z","contentChangedAt":"2026-09-08T00:44:01.258Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}