mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Arsalan Zaidi" <arsalan.za...@gmail.com>
Subject Re: ByteBuffer to string
Date Wed, 14 Jun 2006 18:38:15 GMT
OK, I solved this :-)

There's a length field before the string, total 4 bytes long. So if I do
something like this:

in.position(4);
String temp = in.getString(CharsetUtil.getDefaultCharset().newDecoder());

and print 'temp', it works. The funny bit is, if I say:

String temp = in.getPrefixedString(4, CharsetUtil.getDefaultCharset
().newDecoder());

I get exceptions :-)

Anyway, I'll use what works for now

--Arsalan

On 6/14/06, Niklas Therning <niklas@trillian.se> wrote:
>
> 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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message