apache/pulsar · error · IllegalArgumentException

byte string cannot be empty

Error message

byte string cannot be empty

What it means

ByteUnitUtil.validateSizeString parses human-readable byte-size strings (e.g. '10M', '2G', plain numbers). Passing an empty string throws IllegalArgumentException 'byte string cannot be empty'. It performs no null check, so only the empty-string case throws here.

Source

Thrown at pulsar-cli-utils/src/main/java/org/apache/pulsar/cli/converters/ByteUnitUtil.java:35

 * under the License.
 */
package org.apache.pulsar.cli.converters;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import lombok.experimental.UtilityClass;

@UtilityClass
public class ByteUnitUtil {

    private static Set<Character> sizeUnit = Collections.unmodifiableSet(
            new HashSet<>(Arrays.asList('k', 'K', 'm', 'M', 'g', 'G', 't', 'T')));

    public static long validateSizeString(String byteStr) {
        if (byteStr.isEmpty()) {
            throw new IllegalArgumentException("byte string cannot be empty");
        }

        char last = byteStr.charAt(byteStr.length() - 1);
        String subStr = byteStr.substring(0, byteStr.length() - 1);
        long size;
        try {
            size = sizeUnit.contains(last)
                    ? Long.parseLong(subStr)
                    : Long.parseLong(byteStr);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Invalid size '%s'. Valid formats are: %s",
                    byteStr, "(4096, 100K, 10M, 16G, 2T)"));
        }
        switch (last) {
            case 'k':
            case 'K':
                return size * 1024;

View on GitHub (pinned to 820761864e)

Solutions

  1. Provide a non-empty size string, e.g. '10G' or a plain byte count like '10485760'.
  2. Default the variable in scripts: SIZE=${SIZE:-1G} before invoking the CLI.
  3. Omit the flag entirely if the tool supports a default instead of passing an empty value.
  4. If input comes from a config file, fill in the blank size property.

Example fix

// before
pulsar-admin namespaces set-retention --size "$SIZE"   # SIZE empty
// after
SIZE=${SIZE:-1G}
pulsar-admin namespaces set-retention --size "$SIZE"
Defensive patterns

Strategy: validation

Validate before calling

public static String requireNonEmptySize(String size) {
    if (size == null || size.trim().isEmpty()) {
        throw new IllegalArgumentException("size string is required, e.g. 10G or 10485760");
    }
    return size.trim();
}
// shell: SIZE=${SIZE:-1G} before invoking the CLI

Try / catch

try {
    long bytes = ByteUnitUtil.validateSizeString(sizeStr);
} catch (IllegalArgumentException e) {
    System.err.println("Invalid size argument: " + e.getMessage());
}

Prevention

When it happens

Trigger: Calling validateSizeString("") — typically an empty CLI flag value like --size="" or an empty environment variable interpolated into a size argument.

Common situations: Shell scripts with unset size variables producing empty flags; users hitting enter on a size prompt; config-driven tooling where a size key exists but is blank.

Related errors


AI-assisted analysis of apache/pulsar@820761864e (2026-09-06). Data as JSON: /api/errors/1574ad627bb69425. Report an issue: GitHub.