Skip to content

Commit

Permalink
Replace usage of ChannelBuffer with ByteBuf in NetFlowV9HeaderTest (#29)
Browse files Browse the repository at this point in the history
This occurrence of ChannelBuffer was missed in the original Netty 4 PR (#28)
  • Loading branch information
joschi authored and bernd committed Jan 18, 2018
1 parent 5249012 commit d03f677
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
29 changes: 18 additions & 11 deletions src/main/java/org/graylog/plugins/netflow/v9/NetFlowV9Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package org.graylog.plugins.netflow.v9;

import com.google.auto.value.AutoValue;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

@AutoValue
public abstract class NetFlowV9Header {
Expand Down Expand Up @@ -45,14 +47,19 @@ public static NetFlowV9Header create(int version, int count, long sysUptime, lon
return new AutoValue_NetFlowV9Header(version, count, sysUptime, unixSecs, sequence, sourceId);
}

public ChannelBuffer encode() {
final ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(20);
buffer.writeShort(version());
buffer.writeShort(count());
buffer.writeInt(Math.toIntExact(sysUptime()));
buffer.writeInt(Math.toIntExact(unixSecs()));
buffer.writeInt(Math.toIntExact(sequence()));
buffer.writeInt(Math.toIntExact(sourceId()));
return buffer;
public String prettyHexDump() {
final ByteBuf buffer = Unpooled.buffer(20);
try {
buffer.writeShort(version());
buffer.writeShort(count());
buffer.writeInt(Math.toIntExact(sysUptime()));
buffer.writeInt(Math.toIntExact(unixSecs()));
buffer.writeInt(Math.toIntExact(sequence()));
buffer.writeInt(Math.toIntExact(sourceId()));

return ByteBufUtil.prettyHexDump(buffer);
} finally {
ReferenceCountUtil.release(buffer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static RawNetFlowV9Packet create(NetFlowV9Header header, int dataLength,
@Override
public String toString() {
StringBuilder sb = new StringBuilder("\n");
sb.append(ByteBufUtil.prettyHexDump(Unpooled.wrappedBuffer(header().encode().toByteBuffer()))).append("\n");
sb.append(header().prettyHexDump()).append("\n");
sb.append("\nTemplates:\n");
templates().forEach((integer, byteBuf) -> {
sb.append("\n").append(integer).append(":\n").append(ByteBufUtil.prettyHexDump(Unpooled.wrappedBuffer(byteBuf)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.graylog.plugins.netflow.v9;

import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class NetFlowV9HeaderTest {
@Test
public void prettyHexDump() {
final NetFlowV9Header header = NetFlowV9Header.create(5, 23, 42L, 1000L, 1L, 1L);
assertThat(header.prettyHexDump()).isNotEmpty();
}
}

0 comments on commit d03f677

Please sign in to comment.