Skip to content

Commit

Permalink
pinpoint-apm#1838 Buffer API enhancement
Browse files Browse the repository at this point in the history
 - add remaining()
 - add hasRemaining()
 - deprecated limit()
  • Loading branch information
emeroad committed Jun 10, 2016
1 parent e91f6f9 commit 9dfbe6e
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.navercorp.pinpoint.common.buffer;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
Expand Down Expand Up @@ -166,9 +167,20 @@ public interface Buffer {

byte[] getInternalBuffer();

ByteBuffer wrapByteBuffer();

void setOffset(int offset);

int getOffset();

/**
* @deprecated Since 1.6.0. Use {@link Buffer#remaining()}
*/
@Deprecated
int limit();

int remaining();

boolean hasRemaining();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.common.util.BytesUtils;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
* @author emeroad
Expand Down Expand Up @@ -545,6 +546,11 @@ public byte[] copyBuffer() {
return copy;
}

@Override
public ByteBuffer wrapByteBuffer() {
return ByteBuffer.wrap(this.buffer, 0, offset);
}

/**
* return internal buffer
* @return
Expand All @@ -564,8 +570,22 @@ public int getOffset() {
return offset;
}

/**
* @deprecated Since 1.6.0. Use {@link Buffer#remaining()}
*/
@Deprecated
@Override
public int limit() {
return remaining();
}

@Override
public int remaining() {
return buffer.length - offset;
}

@Override
public boolean hasRemaining() {
return offset < buffer.length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.common.buffer;

import java.nio.ByteBuffer;

/**
* @author emeroad
*/
Expand All @@ -40,9 +42,24 @@ public OffsetAutomaticBuffer(final byte[] buffer, final int offset) {

@Override
public byte[] getBuffer() {
final int bufferSize = offset - startOffset;
final byte[] copy = new byte[bufferSize];
System.arraycopy(buffer, startOffset, copy, 0, bufferSize);
if (startOffset == 0 && offset == buffer.length) {
return this.buffer;
} else {
return copyBuffer();
}
}

@Override
public byte[] copyBuffer() {
final int length = offset - startOffset;
final byte[] copy = new byte[length];
System.arraycopy(buffer, startOffset, copy, 0, length);
return copy;
}

@Override
public ByteBuffer wrapByteBuffer() {
final int length = offset - startOffset;
return ByteBuffer.wrap(this.buffer, startOffset, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.navercorp.pinpoint.common.buffer;

import java.nio.ByteBuffer;

/**
* @author emeroad
*/
Expand All @@ -40,9 +42,24 @@ public OffsetFixedBuffer(final byte[] buffer, final int offset) {

@Override
public byte[] getBuffer() {
final int bufferSize = offset - startOffset;
final byte[] copy = new byte[bufferSize];
System.arraycopy(buffer, startOffset, copy, 0, bufferSize);
if (startOffset == 0 && offset == buffer.length) {
return this.buffer;
} else {
return copyBuffer();
}
}

@Override
public byte[] copyBuffer() {
final int length = offset - startOffset;
final byte[] copy = new byte[length];
System.arraycopy(buffer, startOffset, copy, 0, length);
return copy;
}

@Override
public ByteBuffer wrapByteBuffer() {
final int length = offset - startOffset;
return ByteBuffer.wrap(this.buffer, startOffset, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Random;
Expand Down Expand Up @@ -568,6 +569,17 @@ public void testGetBuffer() throws Exception {
Assert.assertEquals(buffer.getBuffer().length, 4);
}

@Test
public void testWrapByteBuffer() throws Exception {
FixedBuffer buffer = new FixedBuffer(8);
buffer.put(1);
buffer.put(2);

final ByteBuffer byteBuffer = buffer.wrapByteBuffer();
Assert.assertEquals(byteBuffer.getInt(), 1);
Assert.assertEquals(byteBuffer.getInt(), 2);
}

@Test
public void testSliceGetBuffer() throws Exception {
Buffer buffer = new FixedBuffer(5);
Expand Down Expand Up @@ -604,4 +616,28 @@ public void testGetOffset() throws Exception {
Assert.assertEquals(buffer.getOffset(), 4);

}


@Test
public void test_remaining() throws Exception {
final byte[] bytes = new byte[BytesUtils.INT_BYTE_LENGTH];
Buffer buffer = new FixedBuffer(bytes);
Assert.assertEquals(buffer.remaining(), 4);
Assert.assertTrue(buffer.hasRemaining());

buffer.put(1234);
Assert.assertEquals(buffer.remaining(), 0);
Assert.assertFalse(buffer.hasRemaining());

buffer.setOffset(0);
buffer.put((short)12);
Assert.assertEquals(buffer.remaining(), 2);
Assert.assertTrue(buffer.hasRemaining());

buffer.put((byte)1);
Assert.assertEquals(buffer.remaining(), 1);
Assert.assertTrue(buffer.hasRemaining());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import org.junit.Test;

import com.navercorp.pinpoint.common.buffer.Buffer;
import com.navercorp.pinpoint.common.buffer.FixedBuffer;
import com.navercorp.pinpoint.common.buffer.OffsetAutomaticBuffer;
import java.nio.ByteBuffer;

/**
* @author emeroad
Expand All @@ -40,4 +38,28 @@ public void testGetBuffer() throws Exception {
int value = read.readInt();
Assert.assertEquals(putValue, value);
}

@Test
public void testCopyBuffer() throws Exception {
final int putValue = 10;
Buffer buffer = new OffsetAutomaticBuffer(new byte[10], 2);
buffer.put(putValue);
byte[] intBuffer = buffer.copyBuffer();
Assert.assertEquals(intBuffer.length, 4);

Buffer read = new FixedBuffer(intBuffer);
int value = read.readInt();
Assert.assertEquals(putValue, value);
}

@Test
public void testWrapByteBuffer() throws Exception {
Buffer buffer = new OffsetAutomaticBuffer(new byte[10], 2);
buffer.put(1);
buffer.put(2);

ByteBuffer byteBuffer = buffer.wrapByteBuffer();
Assert.assertEquals(1, byteBuffer.getInt());
Assert.assertEquals(2, byteBuffer.getInt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

import org.junit.Test;

import com.navercorp.pinpoint.common.buffer.Buffer;
import com.navercorp.pinpoint.common.buffer.FixedBuffer;
import com.navercorp.pinpoint.common.buffer.OffsetFixedBuffer;
import java.nio.ByteBuffer;

/**
* @author emeroad
Expand Down Expand Up @@ -56,4 +54,29 @@ public void testGetBuffer() throws Exception {
int value = read.readInt();
Assert.assertEquals(putValue, value);
}

@Test
public void testCopyBuffer() throws Exception {
final int putValue = 10;
Buffer buffer = new OffsetFixedBuffer(new byte[10], 2);
buffer.put(putValue);
byte[] intBuffer = buffer.copyBuffer();
Assert.assertEquals(intBuffer.length, 4);

Buffer read = new FixedBuffer(intBuffer);
int value = read.readInt();
Assert.assertEquals(putValue, value);
}

@Test
public void testWrapByteBuffer() throws Exception {
Buffer buffer = new OffsetFixedBuffer(new byte[10], 2);
buffer.put(1);
buffer.put(2);

ByteBuffer byteBuffer = buffer.wrapByteBuffer();
Assert.assertEquals(1, byteBuffer.getInt());
Assert.assertEquals(2, byteBuffer.getInt());
}

}

0 comments on commit 9dfbe6e

Please sign in to comment.