Im trying to write a ‘large’ string (986 chars) from my server to my client, but i get the following error after reading 16 chars:
not enough readable bytes - need 2, maximum is 0.
It works correctly when sending smaller strings, but with such a amount of characters this error is raised.
We’re using a BigEndianHeapChannelBuffer to receive our data in.
The obj.length() from write and buffer.readUnsignedShort() from read both give the same (correct) number, but the rest it crashes in the for loop.
Netty version is 3.5.10 FINAL
Anybody has any ideas how to fix this? Please ask if you need more info.
Here follow some code snippets:
WRITING TO THE STREAM:
public void addString(String obj)
{
try
{
bodystream.writeShort(obj.length());
bodystream.writeChars(obj);
System.out.println("Server wrote bytes: " + obj.length());
message = message + ";STRING: " + obj;
// bodystream.w(obj);
}
catch(IOException e)
{
}
}
READING FROM THE STREAM:
public String readString()
{
try
{
int len = buffer.readUnsignedShort();
System.out.println("Client can read bytes: " + len);
char[] characters = new char[len];
for(int i = 0; i < len; i++)
characters[i] = buffer.readChar();
return new String(characters);
}
catch(Exception e)
{
System.err.println(e.getMessage());
return "";
}
}
DECODING:
public class NetworkDecoder extends FrameDecoder
{
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
{
try
{
int opcode = buffer.readUnsignedByte();
System.out.println("[In] <- " + opcode);
return new ServerMessage(buffer, opcode);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
You will need to make sure that you have enough data in the buffer before you pass it to your ServerMessage. Also you want to call ChannelBuffer.readBytes(..) to “copy the data, otherwise you will suck up memory.