Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@MukjepScarlet
Copy link
Contributor

@MukjepScarlet MukjepScarlet commented Oct 23, 2025

Purpose

Streams.writerForAppendable allows us to wrap an Appendable into a Writer. In the old implementation, the wrapped Writer does nothing on invocation of flush and close. Imagine we wrapped an object which implements both Appendable and Closeable, this might confuse the user.

Description

If the wrapped object implements Flushable or Closeable, the corresponding function will be delegated to the Writer.

Checklist

  • New code follows the Google Java Style Guide
    This is automatically checked by mvn verify, but can also be checked on its own using mvn spotless:check.
    Style violations can be fixed using mvn spotless:apply; this can be done in a separate commit to verify that it did not cause undesired changes.
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very reasonable. I ran the change against all of Google's internal tests and did not see any problems.

@eamonnmcmanus eamonnmcmanus merged commit 72537db into google:main Oct 26, 2025
15 checks passed
@MukjepScarlet MukjepScarlet deleted the appendable-writer branch October 27, 2025 03:11
@MukjepScarlet
Copy link
Contributor Author

In Java I rarely see a class which implements both Appendable and one of Flushable/Closable while it's not a Writer. Maybe this case happens in pure Kotlin libs more commonly.

@Marcono1234
Copy link
Contributor

In Java I rarely see a class which implements both Appendable and one of Flushable/Closable while it's not a Writer

Yes, you are probably right. One such case is java.io.PrintStream though, so this change here could be a problem if a user provides System.out or System.err and the stream is now closed due to these changes.

However, it seems for all cases where Streams#writerForAppendable is used internally in Gson, the AppendableWriter is not actually 'leaked' to the user1, and Gson itself apparently does not call close() or flush() on it either. So maybe this change is not actually noticeable by users and does not have any negative side effect.

And I agree that this is a reasonable change.

Footnotes

  1. Unless they implement a custom type adapter which calls jsonWriter.close(), though that is rather unlikely.

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.

3 participants