MyCATApache/Mycat-Server · error · IllegalArgumentException
Array has wrong size:
Error message
Array has wrong size:
What it means
BytesTools.toBoolean(byte[]) is the inverse of toBytes(boolean) and therefore requires an array of exactly one byte. The library throws this IllegalArgumentException when the array length is not 1, to prevent misreading longer or empty buffers as booleans.
Solutions
- Slice the array so you pass exactly the 1-byte boolean field: toBoolean(Arrays.copyOfRange(buf, offset, offset + 1)).
- Check b.length == 1 before calling; handle empty arrays explicitly.
- If the source is an encoded string or multi-byte encoding, decode it with the correct codec instead of reading raw bytes as a boolean.
Example fix
// before
boolean flag = BytesTools.toBoolean(rowBytes);
// after
if (rowBytes != null && rowBytes.length == 1) {
boolean flag = BytesTools.toBoolean(rowBytes);
} Defensive patterns
Strategy: type-guard
Validate before calling
boolean isSingleByte(byte[] b) { return b != null && b.length == 1; } Type guard
static boolean isSingleByte(byte[] b) {
return b != null && b.length == 1;
}
// usage: if (isSingleByte(buf)) boolean v = BytesTools.toBoolean(buf); Try / catch
try {
flag = BytesTools.toBoolean(buf);
} catch (IllegalArgumentException e) {
flag = false; // or handle bad-length buffer explicitly
} Prevention
- Slice buffers to the exact field offset before converting scalar types.
- Never pass whole serialized records or strings as raw bytes to scalar converters.
- Check array length for every fixed-width decode (1 for boolean, 8 for long, etc.).
When it happens
Trigger: Calling BytesTools.toBoolean with a zero-length array, or with a multi-byte buffer (e.g. a slice of a larger byte array, a serialized record, or a UTF-8 encoded string).
Common situations: Deserializing a byte stream and passing a whole row/buffer instead of the single-byte boolean field; reading a boolean column from HBase-style storage with wrong offset; empty byte[] returned from a store.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- toString(buf)
- A batchSize of: with batchStatsAt of: will overrun the…
- Both batchStartsAt and batchSize must be positive but got…
- bufferSize must be a power of 2
- bufferSize must be a power of 2
AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11).
Data as JSON: /api/errors/c6cc58d0b2287d57.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/io/mycat/memory/unsafe/utils/BytesTools.java:612
/**
* Convert a boolean to a byte array. True becomes -1
* and false becomes 0.
*
* @param b value
* @return <code>b</code> encoded in a byte array.
*/
public static byte [] toBytes(final boolean b) {
return new byte[] { b ? (byte) -1 : (byte) 0 };
}
/**
* Reverses {@link #toBytes(boolean)}
* @param b array
* @return True or false.
*/
public static boolean toBoolean(final byte [] b) {
if (b.length != 1) {
throw new IllegalArgumentException("Array has wrong size: " + b.length);
}
return b[0] != (byte) 0;
}
/**
* Convert a long value to a byte array using big-endian.
*
* @param val value to convert
* @return the byte array
*/
public static byte[] toBytes(long val) {
byte [] b = new byte[8];
for (int i = 7; i > 0; i--) {
b[i] = (byte) val;
val >>>= 8;
}
b[0] = (byte) val;
return b;View on GitHub (pinned to 65f8d8beb7)