skylot/jadx · error · IOException

Failed to read input stream to bytes array

Error message

Failed to read input stream to bytes array

What it means

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.

Source

Thrown at jadx-core/src/main/java/jadx/api/plugins/utils/CommonFileUtils.java:69

			LOG.warn("Failed to delete file: {}", file, e);
			return false;
		}
	}

	public static byte[] loadBytes(InputStream input) throws IOException {
		return loadBytes(null, input);
	}

	public static byte[] loadBytes(byte[] dataPrefix, InputStream in) throws IOException {
		int estimateSize = dataPrefix == null ? in.available() : dataPrefix.length + in.available();
		try (ByteArrayOutputStream out = new ByteArrayOutputStream(estimateSize)) {
			if (dataPrefix != null) {
				out.write(dataPrefix);
			}
			copyStream(in, out);
			return out.toByteArray();
		} catch (Exception e) {
			throw new IOException("Failed to read input stream to bytes array", e);
		}
	}

	public static void copyStream(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[8192];
		while (true) {
			int count = input.read(buffer);
			if (count == -1) {
				break;
			}
			output.write(buffer, 0, count);
		}
	}

	@Nullable
	public static String getFileExtension(String fileName) {
		int dotIndex = fileName.lastIndexOf('.');
		if (dotIndex == -1) {

View on GitHub (pinned to e738a26571)

Solutions

  1. Use the single-argument loadBytes(InputStream) overload when there is no data prefix to avoid the NPE in the two-argument variant.
  2. Verify the stream is open and positioned at the start before calling loadBytes.
  3. For very large inputs, avoid loading entirely into memory — process the stream incrementally instead.
  4. Inspect getCause() to determine if the failure is in stream reading (IOException) or memory allocation (OutOfMemoryError).

Example fix

// before (potential NPE if dataPrefix is null)
byte[] data = CommonFileUtils.loadBytes(null, inputStream);

// after (use single-arg overload)
byte[] data = CommonFileUtils.loadBytes(inputStream);
Defensive patterns

Strategy: validation

Validate before calling

// For the two-argument overload, ensure dataPrefix is never null
if (dataPrefix == null) {
    return CommonFileUtils.loadBytes(inputStream); // use single-arg overload
}
return CommonFileUtils.loadBytes(dataPrefix, inputStream);

Try / catch

byte[] data;
try {
    data = CommonFileUtils.loadBytes(inputStream);
} catch (IOException e) {
    LOG.error("Failed to read stream to bytes: {}", e.getCause().getMessage());
    data = null;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of skylot/jadx@e738a26571 (2026-08-14). Data as JSON: /api/errors/a02ff547bc577365. Report an issue: GitHub.