Lazily construct contained DataOutputStream in ByteBufOutputStream (#10507)

Motivation

This is used solely for the DataOutput#writeUTF8() method, which may
often not be used.

Modifications

Lazily construct the contained DataOutputStream in ByteBufOutputStream.

Result

Saves an allocation in some common cases
This commit is contained in:
Nick Hill 2020-08-28 00:22:58 -07:00 committed by GitHub
parent 8e8f01e01d
commit b908c82b7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,7 +40,7 @@ public class ByteBufOutputStream extends OutputStream implements DataOutput {
private final ByteBuf buffer;
private final int startIndex;
private final DataOutputStream utf8out = new DataOutputStream(this);
private DataOutputStream utf8out; // lazily-instantiated
/**
* Creates a new stream which writes data to the specified {@code buffer}.
@ -131,7 +131,11 @@ public class ByteBufOutputStream extends OutputStream implements DataOutput {
@Override
public void writeUTF(String s) throws IOException {
utf8out.writeUTF(s);
DataOutputStream out = utf8out;
if (out == null) {
utf8out = out = new DataOutputStream(this);
}
out.writeUTF(s);
}
/**