Skip to content

Read the full stream in FileUtils.imageToBase64ByStream#4057

Open
vasiliy-mikhailov wants to merge 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/imagetobase64-full-read
Open

Read the full stream in FileUtils.imageToBase64ByStream#4057
vasiliy-mikhailov wants to merge 1 commit into
binarywang:developfrom
vasiliy-mikhailov:fix/imagetobase64-full-read

Conversation

@vasiliy-mikhailov

Copy link
Copy Markdown

imageToBase64ByStream sizes its buffer from in.available() — only an estimate of the bytes immediately available, not the total — and then calls in.read(data) once, ignoring the returned count. Any stream that does not return all its bytes in a single read (buffered or network-backed streams are the common case) leaves the buffer partially filled with trailing zero bytes, producing a truncated/corrupted base64 string.

This reads the stream fully via IOUtils.toByteArray(in) (commons-io, already imported and used elsewhere in this file). Fills the previously-empty testImageToBase64ByStream with a regression test using a stream that returns at most 16 bytes per read — it fails before the change (truncated output) and passes after.

The method sized its buffer from in.available() (only an estimate, not the total) and called in.read(data) once while ignoring the returned count, so any stream that does not return all bytes in a single read (buffered/network streams) left the buffer partially filled with trailing zeros, producing truncated/corrupted base64. Read the stream fully via IOUtils.toByteArray (commons-io, already imported). Fills the existing empty testImageToBase64ByStream with a regression test.
@binarywang

Copy link
Copy Markdown
Owner

@codex augiee review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: afec4e64e0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants