Skip to content

Commit

Permalink
Merge pull request rosjava#72 from jubeira/fix/channel_buffer
Browse files Browse the repository at this point in the history
Using ChannelBuffer for int8[] types
  • Loading branch information
Juan Ignacio Ubeira authored Oct 1, 2018
2 parents fe41a5d + a5cbea6 commit c5e124f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public byte getInt8(String name) {
}

@Override
public byte[] getInt8Array(String name) {
return (byte[]) messageFields.getFieldValue(name);
public ChannelBuffer getInt8Array(String name) {
return (ChannelBuffer) messageFields.getFieldValue(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public interface RawMessage extends Message {

byte getInt8(String name);

byte[] getInt8Array(String name);
ChannelBuffer getInt8Array(String name);

<T extends Message> T getMessage(String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.base.Preconditions;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.ros.internal.message.MessageBuffers;

import java.nio.ByteOrder;
Expand Down Expand Up @@ -52,9 +53,15 @@ public ChannelBuffer getValue() {

@Override
public void setValue(Object value) {
Preconditions.checkArgument(((ChannelBuffer) value).order() == ByteOrder.LITTLE_ENDIAN);
Preconditions.checkArgument(size < 0 || ((ChannelBuffer) value).readableBytes() == size);
this.value = (ChannelBuffer) value;
ChannelBuffer channelBufferValue = null;
if (value instanceof byte[]) {
channelBufferValue = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, byte[].class.cast(value));
} else if (value instanceof ChannelBuffer) {
channelBufferValue = ChannelBuffer.class.cast(value);
}
Preconditions.checkArgument(channelBufferValue.order() == ByteOrder.LITTLE_ENDIAN);
Preconditions.checkArgument(size < 0 || channelBufferValue.readableBytes() == size);
this.value = channelBufferValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Byte getDefaultValue() {

@Override
public Field newVariableList(String name, int size) {
return ByteArrayField.newVariable(this, name, size);
return ChannelBufferField.newVariable(this, name, size);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@

import com.google.common.collect.Lists;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.junit.Before;
import org.junit.Test;
import org.ros.internal.message.topic.TopicDefinitionResourceProvider;
import org.ros.message.MessageFactory;

import java.nio.ByteOrder;

/**
* @author [email protected] (Damon Kohler)
*/
Expand Down Expand Up @@ -118,8 +122,9 @@ public void testConstantString() {
public void testInt8List() {
topicDefinitionResourceProvider.add("foo/foo", "int8[] data");
RawMessage rawMessage = messageFactory.newFromType("foo/foo");
byte[] data = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
rawMessage.setInt8Array("data", data);
byte[] rawData = new byte[] { (byte) 1, (byte) 2, (byte) 3 };
ChannelBuffer data = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, rawData);
rawMessage.setInt8Array("data", rawData);
assertEquals(data, rawMessage.getInt8Array("data"));
}
}

0 comments on commit c5e124f

Please sign in to comment.