nostra13/Android-Universal-Image-Loader · error · IllegalArgumentException

capacity <= 0

Error message

capacity <= 0

What it means

StrictLineReader's constructor throws IllegalArgumentException when capacity is negative (message text says 'capacity <= 0', and the javadoc says negative or zero, but the actual check is capacity < 0 — zero is accepted and merely allocates an empty buffer). StrictLineReader is the journal parser used inside DiskLruCache, so externally you normally never construct it yourself.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/StrictLineReader.java:90

	}

	/**
	 * Constructs a new {@code LineReader} with the specified capacity and charset.
	 *
	 * @param in the {@code InputStream} to read data from.
	 * @param capacity the capacity of the buffer.
	 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
	 * supported.
	 * @throws NullPointerException if {@code in} or {@code charset} is null.
	 * @throws IllegalArgumentException if {@code capacity} is negative or zero
	 * or the specified charset is not supported.
	 */
	public StrictLineReader(InputStream in, int capacity, Charset charset) {
		if (in == null || charset == null) {
			throw new NullPointerException();
		}
		if (capacity < 0) {
			throw new IllegalArgumentException("capacity <= 0");
		}
		if (!(charset.equals(Util.US_ASCII))) {
			throw new IllegalArgumentException("Unsupported encoding");
		}

		this.in = in;
		this.charset = charset;
		buf = new byte[capacity];
	}

	/**
	 * Closes the reader by closing the underlying {@code InputStream} and
	 * marking this reader as closed.
	 *
	 * @throws IOException for errors when closing the underlying {@code InputStream}.
	 */
	public void close() throws IOException {
		synchronized (in) {

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Pass a positive buffer capacity, e.g. 8192.
  2. Clamp computed capacities: Math.max(1, computed).
  3. Prefer BufferedReader for general-purpose line reading; StrictLineReader is purpose-built for the DiskLruCache journal.

Example fix

// before
new StrictLineReader(in, remaining - headerLen, Util.US_ASCII); // negative when file too short

// after
new StrictLineReader(in, Math.max(1, remaining - headerLen), Util.US_ASCII);
Defensive patterns

Strategy: validation

Validate before calling

int capacityArg = Math.max(1, capacity);
new StrictLineReader(in, capacityArg, Util.US_ASCII);

Prevention

When it happens

Trigger: Direct construction new StrictLineReader(in, negativeCapacity, Util.US_ASCII). In-library usage passes a fixed positive buffer size, so this only fires from custom code reusing this utility class.

Common situations: Copying the ext StrictLineReader utility into other projects and feeding it a computed buffer size that can be negative (e.g. size - header overhead underflowing); passing a constant of -1 as an 'unspecified' sentinel.

Related errors


AI-assisted analysis of nostra13/Android-Universal-Image-Loader@ba33ec64d0 (2026-08-14). Data as JSON: /api/errors/417eecf4ab770c2e. Report an issue: GitHub.