{"record":{"id":"d65ca2fd4a37abad","repo":"apache/beam","slug":"invalid-length-length","errorCode":null,"errorMessage":"invalid length \" + length","messagePattern":"invalid length \" \\+ length","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ByteArrayCoder.java","lineNumber":105,"sourceCode":"      } else {\n        outStream.write(value);\n      }\n    }\n  }\n\n  @Override\n  public byte[] decode(InputStream inStream) throws IOException, CoderException {\n    return decode(inStream, Context.NESTED);\n  }\n\n  @Override\n  public byte[] decode(InputStream inStream, Context context) throws IOException, CoderException {\n    if (context.isWholeStream) {\n      return StreamUtils.getBytesWithoutClosing(inStream);\n    } else {\n      int length = VarInt.decodeInt(inStream);\n      if (length < 0) {\n        throw new IOException(\"invalid length \" + length);\n      }\n      byte[] value = new byte[length];\n      ByteStreams.readFully(inStream, value);\n      return value;\n    }\n  }\n\n  @Override\n  public void verifyDeterministic() {}\n\n  /**\n   * {@inheritDoc}\n   *\n   * @return objects that are equal if the two arrays contain the same bytes.\n   */\n  @Override\n  public Object structuralValue(byte[] value) {\n    return new StructuralByteArray(value);","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/ByteArrayCoder.java#L87-L123","documentation":"ByteArrayCoder.decode throws this IOException when the VarInt length prefix read from the input stream is negative. A negative length means the encoded byte array is corrupt, truncated, or was written by an incompatible coder, so decoding cannot proceed.","triggerScenarios":"Calling ByteArrayCoder.decode(inStream, context) with a non-whole-stream Context on a stream whose VarInt length prefix decodes to a negative value (truncated or misaligned data).","commonSituations":"Corrupt or truncated pipeline data, reading from the wrong stream offset, decoding a stream written with a different coder or Beam version, sharded/concatenated encoded blobs read at the wrong boundary.","solutions":["Verify the input stream position is at the start of a ByteArrayCoder-encoded element, not mid-record","Ensure the same coder and Context were used on the writer side","Recover by re-reading from a checkpoint or re-generating the corrupted input data","Wrap decode in try-catch for IOException/CoderException and treat the record as corrupt rather than crashing the pipeline"],"exampleFix":"// before\nbyte[] value = coder.decode(inStream, Context.NOT_WHOLE_STREAM);\n// after\ntry {\n  byte[] value = coder.decode(inStream, Context.NOT_WHOLE_STREAM);\n} catch (IOException e) {\n  LOG.warn(\"Skipping corrupt byte[] record\", e);\n  return null; // skip / dead-letter the record\n}","handlingStrategy":"try-catch","validationCode":"// Cannot validate without consuming the stream; log position before decoding.\nlong pos = (inStream instanceof FileInputStream) ? ((FileInputStream) inStream).getChannel().position() : -1;","typeGuard":null,"tryCatchPattern":"try { byte[] v = ByteArrayCoder.of().decode(in, Context.NOT_WHOLE_STREAM); } catch (IOException | CoderException e) { /* corrupt input: skip or dead-letter */ }","preventionTips":["Always decode with the same coder and Context used to encode","Never truncate or concatenate encoded blobs without length framing","Log stream offsets to help diagnose corrupt records"],"tags":["java","beam","decoding","corrupt-data"],"backgroundTag":"corrupt-decoded-input","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}