mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niklas Therning <nik...@trillian.se>
Subject Re: ByteBuffer to string
Date Wed, 14 Jun 2006 12:10:35 GMT
Arsalan Zaidi wrote:
> Hmmm. I thought of that. I tried to skip it by setting in.position(2) and
> then in.position(1). But I still get an exception.

What exception?

> 
> The funny thing is that the client is not sending a NULL anyway. It's a
> simple String of XML text.

Are you using the same char encoding on the client and server?

> 
> Is there a better way to convert from a byte buffer to a string? Some
> better
> built in method maybe?

Not that I'm aware of. There's also the getPrefixedString() method in
ByteBuffer which can be used if you prefix every string sent with the
length of the string.

> 
> Regards.
> 
> --Arsalan
> 
> On 6/14/06, Niklas Therning <niklas@trillian.se> wrote:
>>
>> Arsalan Zaidi wrote:
>> > Hi.
>> >
>> > The client is sending me a bunch of plain text data. All I want to
>> do at
>> > this stage is print it out.
>> >
>> > In Decoder.java, I have:
>> >
>> > public void decode(IoSession session, ByteBuffer in,
>> ProtocolDecoderOutput
>> > out) throws Exception
>> >    {
>> >        log.entering(this.getClass().getName(), "decode", new Object[]
>> > {session, in, out});
>> >        log.info("in.limit = "+in.limit());
>> >        log.info("in.position = "+in.position());
>> >        log.info("in.capacity = "+in.capacity());
>> >        log.info("in.toString = "+in.toString());
>> >
>> > String temp = in.getString(CharsetUtil.getDefaultCharset
>> ().newDecoder());
>> >        log.info("original temp = "+temp.length());
>> > }
>> >
>> > Which prints out the following. Notice that even though I'm getting
>> data, I
>> > can't print it out. The String temp is always empty. What am I doing
>> wrong?
>> > ...
>>
>> getString(...) reads a NUL terminated string. From the log output it
>> seems like the first byte in the buffer is 00, aka NUL. Hence you'll get
>> an empty string from getString(...).
>>
>> HTH
>> -- 
>> Niklas Therning
>> Software Architect
>> www.spamdrain.net
>>
> 


Mime
View raw message