Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.apache.hadoop.ozone.common;

import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.util.List;
Expand Down Expand Up @@ -64,6 +66,34 @@ static ChunkBuffer wrap(List<ByteBuffer> buffers) {
return new ChunkBufferImplWithByteBufferList(buffers);
}

default void checkArgument(byte[] b, int offset, int length) {
Objects.requireNonNull(b, "b == null");
Preconditions.checkArgument(offset >= 0 && length >= 0,
"offset = %s, length = %s", offset, length);
Preconditions.checkArgument(length <= b.length - offset,
"length = %s out of range for array.length = %s, offset = %s",
length, b.length, offset);
if (length > remaining()) {
final BufferOverflowException boe = new BufferOverflowException();
boe.initCause(new IllegalArgumentException(
"Failed to put since length = " + length
+ " > this.remaining() = " + remaining()));
throw boe;
}
}

default void checkArgument(ByteBuffer that) {
Objects.requireNonNull(that, "that == null");
final int thatRemaining = that.remaining();
if (thatRemaining > remaining()) {
final BufferOverflowException boe = new BufferOverflowException();
boe.initCause(new IllegalArgumentException(
"Failed to put since that.remaining() = " + thatRemaining
+ " > this.remaining() = " + remaining()));
throw boe;
}
}

/** Similar to {@link ByteBuffer#position()}. */
int position();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public ChunkBuffer put(ByteBuffer b) {
return this;
}

@Override
public ChunkBuffer put(byte[] b, int offset, int length) {
checkArgument(b, offset, length);
buffer.put(b, offset, length);
return this;
}

@Override
public ChunkBuffer put(byte[] b) {
return put(b, 0, b.length);
}

@Override
public ChunkBuffer put(byte b) {
buffer.put(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.util.ArrayList;
Expand Down Expand Up @@ -135,15 +134,8 @@ public ChunkBuffer clear() {

@Override
public ChunkBuffer put(ByteBuffer that) {
final int thisRemaining = remaining();
checkArgument(that);
int thatRemaining = that.remaining();
if (thatRemaining > thisRemaining) {
final BufferOverflowException boe = new BufferOverflowException();
boe.initCause(new IllegalArgumentException(
"Failed to put since that.remaining() = " + thatRemaining
+ " > this.remaining() = " + thisRemaining));
throw boe;
}

while (thatRemaining > 0) {
final ByteBuffer b = current();
Expand All @@ -157,6 +149,27 @@ public ChunkBuffer put(ByteBuffer that) {
return this;
}

@Override
public ChunkBuffer put(byte[] b, int offset, int length) {
checkArgument(b, offset, length);

final int end = offset + length;
int off = offset;
while (off < end) {
final ByteBuffer cur = current();
final int bytes = Math.min(cur.remaining(), end - off);
cur.put(b, off, bytes);
off += bytes;
advanceCurrent();
}
return this;
}

@Override
public ChunkBuffer put(byte[] b) {
return put(b, 0, b.length);
}

@Override
public ChunkBuffer duplicate(int newPosition, int newLimit) {
Preconditions.checkArgument(newPosition >= 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.base.Preconditions;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.channels.GatheringByteChannel;
import java.util.ArrayList;
Expand Down Expand Up @@ -212,13 +211,7 @@ public ChunkBuffer clear() {

@Override
public ChunkBuffer put(ByteBuffer that) {
if (that.remaining() > this.remaining()) {
final BufferOverflowException boe = new BufferOverflowException();
boe.initCause(new IllegalArgumentException(
"Failed to put since that.remaining() = " + that.remaining()
+ " > this.remaining() = " + this.remaining()));
throw boe;
}
checkArgument(that);

final int thatLimit = that.limit();
for (int p = position(); that.position() < thatLimit;) {
Expand All @@ -231,6 +224,26 @@ public ChunkBuffer put(ByteBuffer that) {
return this;
}

@Override
public ChunkBuffer put(byte[] b, int offset, int length) {
checkArgument(b, offset, length);

final int end = offset + length;
for (int p = position(); offset < end;) {
final ByteBuffer buf = getAndAllocateAtPosition(p);
final int min = Math.min(buf.remaining(), end - offset);
buf.put(b, offset, min);
offset += min;
p += min;
}
return this;
}

@Override
public ChunkBuffer put(byte[] b) {
return put(b, 0, b.length);
}

@Override
public ChunkBuffer duplicate(int newPosition, int newLimit) {
Preconditions.checkArgument(newPosition >= 0);
Expand Down
Loading