{"record":{"id":"ab08ef4246ef6f93","repo":"apache/beam","slug":"expected-0-or-1-got-d","errorCode":null,"errorMessage":"Expected 0 or 1, got %d","messagePattern":"Expected 0 or 1, got (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BooleanCoder.java","lineNumber":48,"sourceCode":"  /** Returns the singleton instance of {@link BooleanCoder}. */\n  public static BooleanCoder of() {\n    return INSTANCE;\n  }\n\n  @Override\n  public void encode(Boolean value, OutputStream os) throws IOException {\n    BYTE_CODER.encode(value ? (byte) 1 : 0, os);\n  }\n\n  @Override\n  public Boolean decode(InputStream is) throws IOException {\n    Byte value = BYTE_CODER.decode(is);\n    if (value == 0) {\n      return false;\n    } else if (value == 1) {\n      return true;\n    }\n    throw new IOException(String.format(\"Expected 0 or 1, got %d\", value));\n  }\n\n  @Override\n  public boolean consistentWithEquals() {\n    return true;\n  }\n\n  @Override\n  public boolean isRegisterByteSizeObserverCheap(Boolean value) {\n    return true;\n  }\n\n  @Override\n  protected long getEncodedElementByteSize(Boolean value) throws Exception {\n    return 1;\n  }\n}\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/BooleanCoder.java#L30-L66","documentation":"BooleanCoder.decode() reads one byte and maps 0 to false and 1 to true; any other byte value is a corrupt/invalid encoding, so it throws an IOException with \"Expected 0 or 1, got %d\". This indicates corrupted or incorrectly produced serialized data rather than bad user input.","triggerScenarios":"Decoding a byte stream where the boolean byte is not exactly 0x00 or 0x01 - e.g. data written by a different (non-Beam) serializer, bit-packing bugs, or offset misalignment in a stream.","commonSituations":"Mixing custom serialization output with Beam coders; manual stream reads with wrong offsets (reading a bitfield or 2-byte value instead of 1 byte); data corruption in transit or at rest; version changes in record layout.","solutions":["Verify data was written with BooleanCoder/BYTE_CODER (0/1 bytes), not raw Java or another format.","Check stream alignment: ensure preceding fields are read with the same coder that wrote them.","Re-generate the corrupted data if it was produced by a buggy writer.","If interoping with other systems, agree on a single byte representation (0x00/0x01) for booleans."],"exampleFix":"// before (writer packs booleans as bits)\nout.writeBit(boolValue);\n// after (writer uses a full byte)\nout.write(boolValue ? 1 : 0);","handlingStrategy":"try-catch","validationCode":"// validate bytes before decoding\nif (!payload.every(b -> b == 0 || b == 1)) throw new IllegalStateException(\"Non-boolean byte in payload\");","typeGuard":null,"tryCatchPattern":"try {\n  Boolean b = BooleanCoder.of().decode(in);\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"Expected 0 or 1\")) {\n    LOG.error(\"Corrupted boolean encoding; data not written by BooleanCoder\", e);\n  }\n  throw e;\n}","preventionTips":["Always round-trip data through the same coder that wrote it","Check stream alignment/offsets when manually reading coder output","Add checksums or round-trip tests when interoping with non-Beam serializers","Pin serialization format across pipeline versions"],"tags":["java","apache-beam","coder","decode","corrupt-data"],"backgroundTag":"unexpected-response-shape","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"}