CarGuo/GSYVideoPlayer · error · IllegalArgumentException

Max size must be positive number!

Error message

Max size must be positive number!

What it means

IllegalArgumentException thrown by the TotalSizeLruDiskUsage constructor (TotalSizeLruDiskUsage.java:14-18) when maxSize <= 0. TotalSizeLruDiskUsage is the default LRU trimming strategy of HttpProxyCacheServer, capping total cache bytes; the Builder instantiates it in build() at HttpProxyCacheServer.java:402 via maxCacheSize(long), or with DEFAULT_MAX_SIZE (512 MB, HttpProxyCacheServer.java:357) when no size is set. Like error 21 it is fail-fast configuration validation — a non-positive byte budget is invalid.

Source

Thrown at gsyVideoPlayer-proxy_cache/src/main/java/com/danikula/videocache/file/TotalSizeLruDiskUsage.java:16

package com.danikula.videocache.file;

import java.io.File;

/**
 * {@link DiskUsage} that uses LRU (Least Recently Used) strategy and trims cache size to max size if needed.
 *
 * @author Alexey Danilov (danikula@gmail.com).
 */
public class TotalSizeLruDiskUsage extends LruDiskUsage {

    private final long maxSize;

    public TotalSizeLruDiskUsage(long maxSize) {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("Max size must be positive number!");
        }
        this.maxSize = maxSize;
    }

    @Override
    protected boolean accept(File file, long totalSize, int totalCount) {
        return totalSize <= maxSize;
    }
}

View on GitHub (pinned to e5d74d3aa9)

Solutions

  1. Pass a positive byte size to maxCacheSize(), e.g. maxCacheSize(200L * 1024 * 1024) — note the L to avoid int overflow.
  2. If 'unlimited' was intended, remove the maxCacheSize call; the Builder defaults to DEFAULT_MAX_SIZE = 512 MB.
  3. Clamp computed sizes: long size = Math.max(1L, computedCacheBytes); builder.maxCacheSize(size).
  4. When deriving from free disk space, check StatFs/StorageUtils availability first and fall back to a fixed constant (e.g. 256 MB) when the reported free space is 0.

Example fix

// before: int overflow -> negative -> IllegalArgumentException
new HttpProxyCacheServer.Builder(context)
        .maxCacheSize(200 * 1024 * 1024) // int math overflows for values >= 2GB, and 0/neg throws
        .build();

// after: long literal with clamp
long maxSize = Math.max(1L, 200L * 1024 * 1024);
new HttpProxyCacheServer.Builder(context)
        .maxCacheSize(maxSize)
        .build();
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling maxCacheSize()
long requestedBytes = computeCacheBudgetBytes(context); // any external source
if (requestedBytes <= 0) {
    throw new IllegalStateException("video cache maxCacheSize must be > 0 bytes, got " + requestedBytes);
}
HttpProxyCacheServer server = new HttpProxyCacheServer.Builder(context)
        .maxCacheSize(requestedBytes)
        .build();

Try / catch

try {
    server = builder.maxCacheSize(maxBytes).build();
} catch (IllegalArgumentException e) {
    if ("Max size must be positive number!".equals(e.getMessage())) {
        server = builder.build(); // fall back to builder default (512 MB)
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: Calling new HttpProxyCacheServer.Builder(context).maxCacheSize(0) or a negative long, then .build() — build() runs new TotalSizeLruDiskUsage(maxSize) at HttpProxyCacheServer.java:402 and throws. Also direct construction new TotalSizeLruDiskUsage(size) with 0/negative, typically from a computed value such as (long) (freeDiskBytes * ratio) that evaluates to 0 when freeDiskBytes is 0 or the ratio is misconfigured.

Common situations: Cache size derived from available disk space (StatFs / StorageUtils) that returns 0 on inaccessible storage or is computed with integer arithmetic that truncates to 0 (e.g. 200 * 1024 * 1024 in an int overflow becoming negative); a remote-config max-size placeholder of 0; developers writing maxCacheSize(0) expecting 'no limit' (the default of 512 MB applies instead only if the call is omitted); MB-vs-bytes confusion leading to values like maxCacheSize(200) still being valid but far smaller than intended, then '0' when adjusted with the wrong multiplier.

Related errors


AI-assisted analysis of CarGuo/GSYVideoPlayer@e5d74d3aa9 (2026-08-14). Data as JSON: /api/errors/ed1a82b18cc12a71. Report an issue: GitHub.