Skip to content

Commit 50e7ddd

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Reject null in CharStreams.asWriter(appendable).write(string[, ...]).
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
1 parent 38112eb commit 50e7ddd

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

android/guava/src/com/google/common/io/AppendableWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public void write(int c) throws IOException {
6868
}
6969

7070
@Override
71-
public void write(@NullableDecl String str) throws IOException {
71+
public void write(String str) throws IOException {
72+
checkNotNull(str);
7273
checkNotClosed();
7374
target.append(str);
7475
}
7576

7677
@Override
77-
public void write(@NullableDecl String str, int off, int len) throws IOException {
78+
public void write(String str, int off, int len) throws IOException {
79+
checkNotNull(str);
7880
checkNotClosed();
7981
// tricky: append takes start, end pair...
8082
target.append(str, off, off + len);

guava/src/com/google/common/io/AppendableWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ public void write(int c) throws IOException {
6868
}
6969

7070
@Override
71-
public void write(@Nullable String str) throws IOException {
71+
public void write(String str) throws IOException {
72+
checkNotNull(str);
7273
checkNotClosed();
7374
target.append(str);
7475
}
7576

7677
@Override
77-
public void write(@Nullable String str, int off, int len) throws IOException {
78+
public void write(String str, int off, int len) throws IOException {
79+
checkNotNull(str);
7880
checkNotClosed();
7981
// tricky: append takes start, end pair...
8082
target.append(str, off, off + len);

0 commit comments

Comments
 (0)