Skip to content

Commit

Permalink
Reject null in `CharStreams.asWriter(appendable).write(string[, ...…
Browse files Browse the repository at this point in the history
…])`.

This brings its behavior in line with other `Writer` implementations. Unfortunately, the docs don't describe this behavior:

https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/io/Writer.html#write(java.lang.String)

Note also that this behavior differs from the behavior of `Writer.append(null)`, which is to write the four characters "null." That behavior is itself poorly documented, since the docs for `append(CharSequence)` claim that it is equivalent to `out.write(csq.toString())`, which would throw NPE... though at least *that* is contradicted in the `@param` tag. (The difference between `write` and `append` presumably arises from inheriting `append` from the newer `Appendable` API.)

RELNOTES=`io`: Changed `CharStreams.asWriter(appendable).write(string[, ...])` to reject a null `string`.
PiperOrigin-RevId: 370516874
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Apr 26, 2021
1 parent 38112eb commit 50e7ddd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions android/guava/src/com/google/common/io/AppendableWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public void write(int c) throws IOException {
}

@Override
public void write(@NullableDecl String str) throws IOException {
public void write(String str) throws IOException {
checkNotNull(str);
checkNotClosed();
target.append(str);
}

@Override
public void write(@NullableDecl String str, int off, int len) throws IOException {
public void write(String str, int off, int len) throws IOException {
checkNotNull(str);
checkNotClosed();
// tricky: append takes start, end pair...
target.append(str, off, off + len);
Expand Down
6 changes: 4 additions & 2 deletions guava/src/com/google/common/io/AppendableWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public void write(int c) throws IOException {
}

@Override
public void write(@Nullable String str) throws IOException {
public void write(String str) throws IOException {
checkNotNull(str);
checkNotClosed();
target.append(str);
}

@Override
public void write(@Nullable String str, int off, int len) throws IOException {
public void write(String str, int off, int len) throws IOException {
checkNotNull(str);
checkNotClosed();
// tricky: append takes start, end pair...
target.append(str, off, off + len);
Expand Down

0 comments on commit 50e7ddd

Please sign in to comment.