check the javadoc and implementation of ByteBuffer.getString(int,
CharsetDecoder):
fieldSize = the --maximum-- number of bytes to read
public String getString( int fieldSize, CharsetDecoder decoder )
throws CharacterCodingException
{
checkFieldSize( fieldSize );
if( fieldSize == 0 )
{
return "";
}
if( !hasRemaining() )
{
return "";
}
...
}
As you can see, when there is no data available it just returns an emty string.
I would check in.remaining() before reading the header
Maarten
On 1/17/07, Nicolas FROMENT <[EMAIL PROTECTED]> wrote:
Maarten Bosteels a écrit :
> 1) how is length defined ?
> Is it a result of reading the header ?
> Is it an instance attribute ?
lenght is an instance attribute and is read in readheader()
>
> 2) what happens when there is not enough data to read a header ?
> in that case you should also return MessageDecoderResult.NEED_DATA;
> No ?
Yes, the message is considered as non decodable. This is handled in the
decodable() method.
public MessageDecoderResult decodable(IoSession session, ByteBuffer in)
{
// Return NEED_DATA if the whole header is not read yet.
if (!readHeader)
{
if (in.remaining() < Constants.HEADER_LEN)
{
return MessageDecoderResult.NEED_DATA;
}
else
{
return MessageDecoderResult.OK;
}
}
else
{
return MessageDecoderResult.OK;
}
}
>
> 3) please, show us the code for readHeader(ByteBuffer in)
>
No problem
private void readHeader(ByteBuffer in)
{
String header;
try
{
header = in.getString(Constants.HEADER_LEN, decoder);
String[] sArray = header.split(",", 2);
timeout = Short.parseShort(sArray[0]);
type = Short.parseShort(sArray[1]);
length = Integer.parseInt(sArray[2]);
}
catch (CharacterCodingException e)
{
}
catch (NumberFormatException e)
{
}
}