Don't allocate extra byte for each MessageFramer
diff --git a/core/src/main/java/io/grpc/internal/MessageFramer.java b/core/src/main/java/io/grpc/internal/MessageFramer.java
index 1b0df18..f7c206e 100644
--- a/core/src/main/java/io/grpc/internal/MessageFramer.java
+++ b/core/src/main/java/io/grpc/internal/MessageFramer.java
@@ -331,14 +331,21 @@
* {@link OutputStream}.
*/
private final class BufferChainOutputStream extends OutputStream {
-
- private final byte[] singleByte = new byte[1];
private final List<WritableBuffer> bufferList = new ArrayList<WritableBuffer>();
private WritableBuffer current;
+ /**
+ * This is slow, don't call it. If you care about write overhead, use a BufferedOutputStream.
+ * Better yet, you can use your own single byte buffer and called
+ * {@link #write(byte[], int, int)}.
+ */
@Override
- public void write(int b) {
- singleByte[0] = (byte) b;
+ public void write(int b) throws IOException {
+ if (current != null && current.writableBytes() > 0) {
+ current.write((byte)b);
+ return;
+ }
+ byte[] singleByte = new byte[]{(byte)b};
write(singleByte, 0, 1);
}
diff --git a/core/src/main/java/io/grpc/internal/WritableBuffer.java b/core/src/main/java/io/grpc/internal/WritableBuffer.java
index 964adc9..49eb01a 100644
--- a/core/src/main/java/io/grpc/internal/WritableBuffer.java
+++ b/core/src/main/java/io/grpc/internal/WritableBuffer.java
@@ -51,6 +51,11 @@
void write(byte[] src, int srcIndex, int length);
/**
+ * Appends a single byte to the buffer. This is slow so don't call it.
+ */
+ void write(byte b);
+
+ /**
* Returns the number of bytes one can write to the buffer.
*/
int writableBytes();
diff --git a/core/src/test/java/io/grpc/internal/MessageFramerTest.java b/core/src/test/java/io/grpc/internal/MessageFramerTest.java
index 5bbfee9..5d27121 100644
--- a/core/src/test/java/io/grpc/internal/MessageFramerTest.java
+++ b/core/src/test/java/io/grpc/internal/MessageFramerTest.java
@@ -371,6 +371,11 @@
}
@Override
+ public void write(byte b) {
+ data[writeIdx++] = b;
+ }
+
+ @Override
public int writableBytes() {
return data.length - writeIdx;
}
diff --git a/netty/src/main/java/io/grpc/netty/NettyWritableBuffer.java b/netty/src/main/java/io/grpc/netty/NettyWritableBuffer.java
index e92b640..30a97a1 100644
--- a/netty/src/main/java/io/grpc/netty/NettyWritableBuffer.java
+++ b/netty/src/main/java/io/grpc/netty/NettyWritableBuffer.java
@@ -51,6 +51,11 @@
}
@Override
+ public void write(byte b) {
+ bytebuf.writeByte(b);
+ }
+
+ @Override
public int writableBytes() {
return bytebuf.writableBytes();
}
diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBuffer.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBuffer.java
index cef39bd..7bf3fa0 100644
--- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBuffer.java
+++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpWritableBuffer.java
@@ -32,6 +32,7 @@
package io.grpc.okhttp;
import io.grpc.internal.WritableBuffer;
+
import okio.Buffer;
class OkHttpWritableBuffer implements WritableBuffer {
@@ -53,6 +54,13 @@
}
@Override
+ public void write(byte b) {
+ buffer.writeByte(b);
+ writableBytes -= 1;
+ readableBytes += 1;
+ }
+
+ @Override
public int writableBytes() {
return writableBytes;
}