{"record":{"id":"a02ff547bc577365","repo":"skylot/jadx","slug":"failed-to-read-input-stream-to-bytes-array","errorCode":null,"errorMessage":"Failed to read input stream to bytes array","messagePattern":"Failed to read input stream to bytes array","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"jadx-core/src/main/java/jadx/api/plugins/utils/CommonFileUtils.java","lineNumber":69,"sourceCode":"\t\t\tLOG.warn(\"Failed to delete file: {}\", file, e);\n\t\t\treturn false;\n\t\t}\n\t}\n\n\tpublic static byte[] loadBytes(InputStream input) throws IOException {\n\t\treturn loadBytes(null, input);\n\t}\n\n\tpublic static byte[] loadBytes(byte[] dataPrefix, InputStream in) throws IOException {\n\t\tint estimateSize = dataPrefix == null ? in.available() : dataPrefix.length + in.available();\n\t\ttry (ByteArrayOutputStream out = new ByteArrayOutputStream(estimateSize)) {\n\t\t\tif (dataPrefix != null) {\n\t\t\t\tout.write(dataPrefix);\n\t\t\t}\n\t\t\tcopyStream(in, out);\n\t\t\treturn out.toByteArray();\n\t\t} catch (Exception e) {\n\t\t\tthrow new IOException(\"Failed to read input stream to bytes array\", e);\n\t\t}\n\t}\n\n\tpublic static void copyStream(InputStream input, OutputStream output) throws IOException {\n\t\tbyte[] buffer = new byte[8192];\n\t\twhile (true) {\n\t\t\tint count = input.read(buffer);\n\t\t\tif (count == -1) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\toutput.write(buffer, 0, count);\n\t\t}\n\t}\n\n\t@Nullable\n\tpublic static String getFileExtension(String fileName) {\n\t\tint dotIndex = fileName.lastIndexOf('.');\n\t\tif (dotIndex == -1) {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/skylot/jadx/blob/e738a26571d02919f01df40de93bc9a44dee4e18/jadx-core/src/main/java/jadx/api/plugins/utils/CommonFileUtils.java#L51-L87","documentation":"Thrown by CommonFileUtils.loadBytes when reading an InputStream into a byte array fails. The method estimates the initial buffer size from in.available(), writes an optional data prefix, then copies the stream into a ByteArrayOutputStream. Any exception during the copy (including NPE when dataPrefix is null on the overload that accepts it) is wrapped in an IOException with this message.","triggerScenarios":"Calling loadBytes with a stream that throws during read (closed stream, corrupted source, network-backed stream that disconnects). A NPE can occur in the two-argument overload if dataPrefix is null, since it calls dataPrefix.length without a null check (only the one-argument overload delegates correctly). The catch also covers ByteArrayOutputStream write failures (extremely rare, only on OutOfMemoryError for very large inputs).","commonSituations":"Reading a zip entry stream that has already been consumed or closed. Loading bytes from a very large input that exhausts JVM heap (ByteArrayOutOfBoundsException or OutOfMemoryError). Passing null as dataPrefix to the two-argument loadBytes overload — use loadBytes(InputStream) instead.","solutions":["Use the single-argument loadBytes(InputStream) overload when there is no data prefix to avoid the NPE in the two-argument variant.","Verify the stream is open and positioned at the start before calling loadBytes.","For very large inputs, avoid loading entirely into memory — process the stream incrementally instead.","Inspect getCause() to determine if the failure is in stream reading (IOException) or memory allocation (OutOfMemoryError)."],"exampleFix":"// before (potential NPE if dataPrefix is null)\nbyte[] data = CommonFileUtils.loadBytes(null, inputStream);\n\n// after (use single-arg overload)\nbyte[] data = CommonFileUtils.loadBytes(inputStream);","handlingStrategy":"validation","validationCode":"// For the two-argument overload, ensure dataPrefix is never null\nif (dataPrefix == null) {\n    return CommonFileUtils.loadBytes(inputStream); // use single-arg overload\n}\nreturn CommonFileUtils.loadBytes(dataPrefix, inputStream);","typeGuard":null,"tryCatchPattern":"byte[] data;\ntry {\n    data = CommonFileUtils.loadBytes(inputStream);\n} catch (IOException e) {\n    LOG.error(\"Failed to read stream to bytes: {}\", e.getCause().getMessage());\n    data = null;\n}","preventionTips":["Use the single-argument loadBytes(InputStream) overload when there is no data prefix to avoid NPE in the two-argument variant.","Verify the stream is open and not already consumed before calling loadBytes.","For very large inputs, avoid loading entirely into memory — process incrementally to prevent OutOfMemoryError."],"tags":["io","stream","bytes"],"backgroundTag":null,"analyzedSha":"e738a26571d02919f01df40de93bc9a44dee4e18","analyzedAt":"2026-08-14T00:10:24.238Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}