Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions src/main/java/com/timgroup/statsd/StatsDProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public abstract class StatsDProcessor {

protected abstract class ProcessingTask implements Runnable {
protected StringBuilder builder = new StringBuilder();
char[] charBuffer = new char[maxPacketSizeBytes];
// + 4 so that we can check for buffer overflow without computing encoded length first
final byte[] byteBuffer = new byte[maxPacketSizeBytes + 4];
final Encoder encoder = new Encoder(maxPacketSizeBytes);

public final void run() {
try {
Expand Down Expand Up @@ -133,6 +131,33 @@ protected void processLoop() {
abstract Message getMessage() throws InterruptedException;

protected void writeBuilderToSendBuffer(ByteBuffer sendBuffer) {
encoder.encode(builder, sendBuffer);
}
}

/**
* Encodes messages as UTF-8 into send buffers. Each processing task has its own, because it
* reuses its scratch arrays between messages.
*/
static final class Encoder {
private final int maxPacketSizeBytes;
private char[] charBuffer;
private final byte[] byteBuffer;

Encoder(int maxPacketSizeBytes) {
this.maxPacketSizeBytes = maxPacketSizeBytes;
this.charBuffer = new char[maxPacketSizeBytes];
// + 4 so that we can check for buffer overflow without computing encoded length first
this.byteBuffer = new byte[maxPacketSizeBytes + 4];
}

/**
* Appends the UTF-8 encoding of {@code builder} to {@code sendBuffer}.
*
* @throws BufferOverflowException if the encoding is longer than a packet or than the space
* left in {@code sendBuffer}. Nothing is written in that case.
*/
void encode(StringBuilder builder, ByteBuffer sendBuffer) {
int length = builder.length();
if (length > charBuffer.length) {
charBuffer = new char[length];
Expand Down Expand Up @@ -164,7 +189,7 @@ protected void writeBuilderToSendBuffer(ByteBuffer sendBuffer) {
byteBuffer[blen++] = (byte) (128 | (decoded & 63));
}

if (blen >= maxPacketSizeBytes) {
if (blen > maxPacketSizeBytes) {
throw new BufferOverflowException();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.timgroup.statsd;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;

public class StatsDProcessorEncoderTest {

private static final int MAX_PACKET_SIZE = 64;

private final StatsDProcessor.Encoder encoder = new StatsDProcessor.Encoder(MAX_PACKET_SIZE);
private final ByteBuffer sendBuffer = ByteBuffer.allocate(MAX_PACKET_SIZE);

@Test
public void encodes_message_that_exactly_fills_the_packet() {
assertEncodesExactFit(message("", MAX_PACKET_SIZE));
}

@Test
public void encodes_two_byte_chars_that_exactly_fill_the_packet() {
assertEncodesExactFit(message("é", MAX_PACKET_SIZE - 1));
}

@Test
public void encodes_three_byte_chars_that_exactly_fill_the_packet() {
assertEncodesExactFit(message("€", MAX_PACKET_SIZE - 2));
}

@Test
public void encodes_surrogate_pair_that_exactly_fills_the_packet() {
// Two chars, four bytes.
assertEncodesExactFit(message("😀", MAX_PACKET_SIZE - 2));
}

@Test
public void rejects_message_one_byte_longer_than_the_packet() {
assertRejects(message("", MAX_PACKET_SIZE + 1));
}

@Test
public void rejects_multibyte_message_one_byte_longer_than_the_packet() {
// As many chars as fit in a packet, but 'é' takes two bytes.
assertRejects(message("é", MAX_PACKET_SIZE));
}

@Test
public void rejects_message_longer_than_the_space_left() {
sendBuffer.put(new byte[10]);

assertRejects(message("", MAX_PACKET_SIZE - 9));
}

/** {@code length} chars: {@code head}, then ASCII filler. */
private static String message(String head, int length) {
char[] filler = new char[length - head.length()];
Arrays.fill(filler, 'x');
return head + new String(filler);
}

private void assertEncodesExactFit(String message) {
assertEquals(MAX_PACKET_SIZE, message.getBytes(StandardCharsets.UTF_8).length);

encoder.encode(new StringBuilder(message), sendBuffer);

sendBuffer.flip();
assertEquals(message, StandardCharsets.UTF_8.decode(sendBuffer).toString());
}

private void assertRejects(String message) {
int position = sendBuffer.position();
try {
encoder.encode(new StringBuilder(message), sendBuffer);
fail("expected BufferOverflowException");
} catch (BufferOverflowException expected) {
// Nothing is written on overflow.
assertEquals(position, sendBuffer.position());
}
}
}
Loading