Skip to content

Commit

Permalink
Don't create byte[] by default and have min size of 32 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanemerson committed Feb 10, 2025
1 parent c62004a commit 373b084
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.infinispan.protostream.impl;

import java.io.OutputStream;
import java.nio.ByteBuffer;

import net.jcip.annotations.NotThreadSafe;

@NotThreadSafe
public class RawByteArrayOutputStreamImpl extends OutputStream implements org.infinispan.protostream.RawByteArrayOutputStream {

static final int MIN_SIZE = 32;

static final int DEFAULT_DOUBLING_SIZE = 4 * 1024 * 1024; // 4MB

byte[] buf;
int pos = 0;

public RawByteArrayOutputStreamImpl() {
this(32);
}

public RawByteArrayOutputStreamImpl(int capacity) {
Expand All @@ -19,9 +24,6 @@ public RawByteArrayOutputStreamImpl(int capacity) {
this.buf = new byte[capacity];
}

byte[] buf;
int pos = 0;

@Override
public void write(int b) {
int newpos = pos + 1;
Expand Down Expand Up @@ -52,7 +54,7 @@ public void write(byte[] b, int off, int len) {
@Override
public void ensureCapacity(int capacity) {
if (buf == null) {
buf = new byte[capacity];
buf = new byte[Math.max(MIN_SIZE, capacity)];
} else if (capacity > buf.length) {
byte[] newbuf = new byte[getNewBufferSize(buf.length, capacity)];
System.arraycopy(buf, 0, newbuf, 0, pos);
Expand Down Expand Up @@ -81,4 +83,11 @@ public void setPosition(int position) {
public byte[] getRawBuffer() {
return buf;
}

@Override
public ByteBuffer getByteBuffer() {
return buf == null ?
ByteBuffer.wrap(new byte[0]) :
ByteBuffer.wrap(buf, 0, getPosition());
}
}

0 comments on commit 373b084

Please sign in to comment.