{"record":{"id":"e0e466fd1524d511","repo":"apache/beam","slug":"initial-capacity-0","errorCode":null,"errorMessage":"Initial capacity < 0","messagePattern":"Initial capacity < 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/ByteStringOutputStream.java","lineNumber":73,"sourceCode":"  // Current buffer to which we are writing\n  private byte[] buffer;\n\n  // Location in buffer[] to which we write the next byte.\n  private int bufferPos;\n\n  /** Creates a new output stream with a default capacity. */\n  public ByteStringOutputStream() {\n    this(DEFAULT_CAPACITY);\n  }\n\n  /**\n   * Creates a new output stream with the specified initial capacity.\n   *\n   * @param initialCapacity the initial capacity of the output stream.\n   */\n  public ByteStringOutputStream(int initialCapacity) {\n    if (initialCapacity < 0) {\n      throw new IllegalArgumentException(\"Initial capacity < 0\");\n    }\n    this.buffer = new byte[initialCapacity];\n    this.result = ByteString.EMPTY;\n  }\n\n  @Override\n  public void write(int b) {\n    if (bufferPos == buffer.length) {\n      // We want to increase our total capacity by 50% but not larger than the max chunk size.\n      result = result.concat(UnsafeByteOperations.unsafeWrap(buffer));\n      buffer = new byte[Math.min(Math.max(1, result.size()), MAX_CHUNK_SIZE)];\n      bufferPos = 0;\n    }\n    buffer[bufferPos++] = (byte) b;\n  }\n\n  @Override\n  public void write(byte[] b, int offset, int length) {","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/ByteStringOutputStream.java#L55-L91","documentation":"ByteStringOutputStream's constructor validates its initialCapacity argument and throws IllegalArgumentException if it is negative. The capacity sizes the internal byte buffer, which cannot have negative length.","triggerScenarios":"Calling new ByteStringOutputStream(n) with n < 0, typically when capacity is computed from a formula (e.g. estimated size, subtraction like available - consumed) that yields a negative value.","commonSituations":"Pre-sizing the stream based on an estimated encoded size that was computed as a difference of two sizes; misreading the API as accepting a default/unspecified value (there is an overloaded no-arg constructor for that).","solutions":["Validate the computed capacity is non-negative before constructing, clamping to 0 or a sane minimum","Use the no-arg ByteStringOutputStream() constructor when no meaningful capacity is known","Fix the size-estimation arithmetic that produced the negative number"],"exampleFix":"// before\nint capacity = estimatedSize - headerSize; // can be negative\nByteStringOutputStream out = new ByteStringOutputStream(capacity);\n// after\nint capacity = Math.max(0, estimatedSize - headerSize);\nByteStringOutputStream out = new ByteStringOutputStream(capacity);","handlingStrategy":"validation","validationCode":"if (capacity < 0) {\n  throw new IllegalArgumentException(\"computed capacity is negative: \" + capacity);\n}\nByteStringOutputStream out = new ByteStringOutputStream(capacity);","typeGuard":null,"tryCatchPattern":"try {\n  ByteStringOutputStream out = new ByteStringOutputStream(capacity);\n} catch (IllegalArgumentException e) {\n  ByteStringOutputStream out = new ByteStringOutputStream(); // fall back to default\n}","preventionTips":["Clamp computed sizes with Math.max(0, size)","Prefer the no-arg constructor unless a real estimate exists","Unit-test size-estimation helpers with boundary inputs"],"tags":["java","argument-validation","io"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}