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 Norman Maurer
parent d7c1407d4c
commit 26993b0d9c

View File

@ -41,7 +41,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}.
@ -133,7 +133,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);
}
/**