flashman
← All posts

Base64 streaming chunk and padding debugging

Debug streamed Base64 corruption by preserving three-byte groups, carrying remainders, placing padding only at the end, and verifying decoded byte hashes.

2026-09-08 · 6 min read · Rahul Chitturi

  • base64
  • streaming
  • data-integrity

Encoding each network or file chunk as an independent Base64 string and concatenating the results can corrupt data. Base64 maps three input bytes to four characters, while arbitrary chunk boundaries may leave one or two bytes that belong with the next chunk.

Padding in the middle of a concatenated stream may cause strict decoders to stop, reject the input, or silently ignore the remaining data.

Preserve encoder state

Use a streaming encoder that carries incomplete three-byte groups between writes. Emit padding only when the entire input ends, unless the protocol explicitly frames each chunk as a separate independently decoded value.

  • Distinguish transport chunks from application message boundaries
  • Keep binary bytes separate from text character encodings
  • Specify standard or URL-safe alphabet for the whole stream
  • Reject unexpected whitespace and padding according to the contract

Verify bytes, not visible text

Record original byte length, encoded length, decoded length, and a test-fixture digest. Text previews can conceal replacement characters, newline conversion, or truncation in binary content.

For large payloads, prefer a binary streaming transport when available. Base64 increases transfer size and can create several in-memory copies if the implementation joins every encoded chunk before sending.

A Flashman workflow

Use Base64 with small public byte fixtures, hash to compare round trips, diff to locate the first changed segment, units converter for memory budgets, and JSON formatter when a protocol wraps chunks in messages.

Test chunk sizes around multiples of three, empty and final chunks, backpressure, retries, URL-safe input, strict padding, whitespace, cancellation, and payloads larger than one memory buffer.

Try these tools