package com.github.brainlag.nsq.frames;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class MessageFrame extends NSQFrame {
	private long timestamp;
	private int attempts;
	private byte[] messageId = new byte[16];
	private byte[] messageBody;
	
	@Override
	public void setData(byte[] bytes) {
		//parse the bytes
		super.setData(bytes);

        ByteBuf buf = Unpooled.wrappedBuffer(bytes);
		timestamp = buf.readLong();
		attempts = buf.readShort();
		buf.readBytes(messageId);
		if (buf.hasArray()) {
			messageBody = buf.readBytes(buf.readableBytes()).array();
		} else {
			byte[] array = new byte[buf.readableBytes()];
			buf.readBytes(array);
			messageBody = array;
		}
	}
	
	public long getTimestamp() {
		return timestamp;
	}

	public int getAttempts() {
		return attempts;
	}

	public byte[] getMessageId() {
		return messageId;
	}

	public byte[] getMessageBody() {
		return messageBody;
	}

}

@@ -506,33 +506,16 @@
 ;%0A%09%09
-if (buf.hasArray()) %7B%0A%09%09%09
+ByteBuf 
 mess
@@ -521,16 +521,19 @@
 sageBody
+Buf
  = buf.r
@@ -561,16 +561,84 @@
 Bytes())
+;%0A%09%09if (messageBodyBuf.hasArray()) %7B%0A%09%09%09messageBody = messageBodyBuf
 .array()
@@ -677,17 +677,28 @@
 ew byte%5B
-b
+messageBodyB
 uf.reada
@@ -713,17 +713,28 @@
 ()%5D;%0A%09%09%09
-b
+messageBodyB
 uf.readB

Fix odd behaviour on reading a ByteBuf in MessageFrame.