{"record":{"id":"6078ea9de15ef133","repo":"zxing/zxing","slug":"data-to-large-for-user-specified-layer","errorCode":null,"errorMessage":"Data to large for user specified layer","messagePattern":"Data to large for user specified layer","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/google/zxing/aztec/encoder/Encoder.java","lineNumber":145,"sourceCode":"    int totalSizeBits = bits.getSize() + eccBits;\n    boolean compact;\n    int layers;\n    int totalBitsInLayer;\n    int wordSize;\n    BitArray stuffedBits;\n    if (userSpecifiedLayers != DEFAULT_AZTEC_LAYERS) {\n      compact = userSpecifiedLayers < 0;\n      layers = Math.abs(userSpecifiedLayers);\n      if (layers > (compact ? MAX_NB_BITS_COMPACT : MAX_NB_BITS)) {\n        throw new IllegalArgumentException(\n            String.format(\"Illegal value %s for layers\", userSpecifiedLayers));\n      }\n      totalBitsInLayer = totalBitsInLayer(layers, compact);\n      wordSize = WORD_SIZE[layers];\n      int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);\n      stuffedBits = stuffBits(bits, wordSize);\n      if (stuffedBits.getSize() + eccBits > usableBitsInLayers) {\n        throw new IllegalArgumentException(\"Data to large for user specified layer\");\n      }\n      if (compact && stuffedBits.getSize() > wordSize * 64) {\n        // Compact format only allows 64 data words, though C4 can hold more words than that\n        throw new IllegalArgumentException(\"Data to large for user specified layer\");\n      }\n    } else {\n      wordSize = 0;\n      stuffedBits = null;\n      // We look at the possible table sizes in the order Compact1, Compact2, Compact3,\n      // Compact4, Normal4,...  Normal(i) for i < 4 isn't typically used since Compact(i+1)\n      // is the same size, but has more data.\n      for (int i = 0; ; i++) {\n        if (i > MAX_NB_BITS) {\n          throw new IllegalArgumentException(\"Data too large for an Aztec code\");\n        }\n        compact = i <= 3;\n        layers = compact ? i + 1 : i;\n        totalBitsInLayer = totalBitsInLayer(layers, compact);","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/zxing/zxing/blob/19aa2d8254410e161f04dc3c928e68d5e90233c2/core/src/main/java/com/google/zxing/aztec/encoder/Encoder.java#L127-L163","documentation":"Thrown by Encoder.encode when userSpecifiedLayers is set (non-zero) and within range, but the actual data (stuffed bits + ECC bits) exceeds the usable capacity of the chosen layer size. The user pinned a specific symbol size that is too small for the payload.","triggerScenarios":"With userSpecifiedLayers set to a valid layer count, stuffBits(bits, wordSize).getSize() + eccBits exceeds the usable bits in that layer (totalBitsInLayer minus alignment padding). The data payload is too large for the manually-selected symbol size.","commonSituations":"Manually specifying a small number of layers (e.g., 1 or 2) for data that needs more capacity. Increasing minECCPercent (error correction) consumes more bits, pushing the total over capacity. Not accounting for ECC overhead when choosing a layer count.","solutions":["Pass userSpecifiedLayers = 0 (DEFAULT_AZTEC_LAYERS) to let the encoder auto-select the smallest sufficient layer count.","If you must pin layers, increase the layer count to a larger value.","Reduce minECCPercent to free up space for data bits (tradeoff: less error correction).","Shorten the input data to fit the chosen symbol size."],"exampleFix":"// before\n// Pinned to 3 compact layers, but data is too large\nAztecCode code = Encoder.encode(largeData, 33, -3, charset);\n\n// after\n// Auto-select layers to fit the data\nAztecCode code = Encoder.encode(largeData, 33, Encoder.DEFAULT_AZTEC_LAYERS, charset);\n\n// or increase layers\nAztecCode code = Encoder.encode(largeData, 33, 5, charset); // full-size, 5 layers","handlingStrategy":"validation","validationCode":"// Pre-estimate whether data fits in specified layers (approximate)\nint dataBytes = data.length;\nboolean compact = userSpecifiedLayers < 0;\nint layers = Math.abs(userSpecifiedLayers);\nint estBits = dataBytes * 8; // rough estimate\nint eccBits = estBits * minECCPercent / 100 + 11;\nint totalEst = estBits + eccBits;\n// Compare against totalBitsInLayer; if close, use auto-sizing instead\n// Safest: use DEFAULT_AZTEC_LAYERS (0) to auto-select.","typeGuard":"boolean likelyFitsLayers(byte[] data, int minECC, int layers) {\n  // Heuristic: auto-sizing is always safe; manual pinning risks overflow\n  return layers == 0;\n}","tryCatchPattern":"try {\n  AztecCode code = Encoder.encode(data, minECC, userSpecifiedLayers, charset);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"user specified layer\")) {\n    // Fall back to auto-sizing\n    code = Encoder.encode(data, minECC, Encoder.DEFAULT_AZTEC_LAYERS, charset);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Prefer DEFAULT_AZTEC_LAYERS (0) for auto-sizing unless symbol dimensions must be fixed.","If pinning layers, test with representative data sizes to ensure capacity.","Account for ECC overhead when estimating required layers.","Catch IllegalArgumentException and fall back to auto-sizing."],"tags":["aztec","encode","capacity","illegal-argument","layers"],"backgroundTag":null,"analyzedSha":"19aa2d8254410e161f04dc3c928e68d5e90233c2","analyzedAt":"2026-08-14T03:07:53.428Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}