Reputation: 103
I need to create a Bytebuffer like object in Ruby. Here is my example in JAVA
int ADD_BYTES_FOR_MSG_LENGTH = 4;
Integer messageBodySize = new Integer(messageBody.getBytes().length);
byte[] messageHeader = ByteBuffer.allocate(ADD_BYTES_FOR_MSG_LENGTH).putInt(messageBodySize).array();
messageToSend = new byte[messageHeader.length + messageBodySize];
System.arraycopy(messageHeader, 0, messageToSend, 0, messageHeader.length);
System.arraycopy(messageBody.getBytes(), 0, messageToSend, messageHeader.length, messageBodySize);
I create a message starting with 4 bytes where is located size of the message body part, and then the actual message. I have no idea how to do this in Ruby, so please help.
Upvotes: 2
Views: 1156
Reputation: 42953
I did this for Steam Condenser using an extended version of StringIO
.
Although I don't use any put*
methods, these could be easily implemented like for example:
class StringIO
def put_int(i)
write [i].pack('v')
end
end
You may have a look at the code, the documentation of the original class and my extensions.
Upvotes: 4