Skip to content

SimpleStreamReader cannot read multibyte characters on buffer boundaryΒ #2353

Closed

Description

@gbrdead

When reading character by character using UTF-8, SimpleStreamReader returns -1 (EOF) when it encounters an emoticon. The problem exists even when reading a lot of characters at once but is harder to reproduce (the four bytes of the emoticon must get misaligned with the buffer).

Activity

  1. gbrdead commented on Jun 29, 2025

    @gbrdead
    Author

    input.txt

    The following code:

    		try (Reader r = new SimpleStreamReader(new FileInputStream("input.txt"), StandardCharsets.UTF_8))
    		{
    			int c;
    			while ((c = r.read()) != -1)
    			{
    				System.out.print((char)c);
    			}
    		}
    

    does not create the same output as the input. I cannot simplify the example for the moment.

  2. changed the title [-]SimpleStreamReader cannot read emoticons[/-] [+]SimpleStreamReader cannot read multibyte characters on buffer boundary[/+] on Jun 30, 2025
  3. added
    bugA confirmed bug, that we should fix
    fixedAn {bug|improvement} that has been {fixed|implemented}
    on Jun 30, 2025
  4. added this to the 1.21.2 milestone on Jun 30, 2025
  5. jhy commented on Jun 30, 2025

    @jhy
    Owner

    Good find, thanks!

    I updated it so that when in this condition (a surrogate pair is straddling the buffer, and the requested len is not enough to read both characters), returns read length of zero. And handle that in the CharacterReader.

    This saves having to track the last emitted high/low surrogate etc.

    Note that reads of length 1 then may return 0 characters, and that r.read() is not implemented and won't work for surrogates correctly.

  6. added a commit that references this issue on Jun 30, 2025
    3b97e96
  7. jhy commented on Jul 4, 2025

    @jhy
    Owner

    @gbrdead can you validate this?

  8. gbrdead commented on Jul 5, 2025

    @gbrdead
    Author

    Yes, it works for all the example data I have.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA confirmed bug, that we should fixfixedAn {bug|improvement} that has been {fixed|implemented}

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions