This discussion is archived
7 Replies Latest reply: Oct 21, 2008 8:53 AM by 807589 RSS

quick way to copy byte buffer to string

807589 Newbie
Currently Being Moderated
A trivial way
String out = "";
for(int i=0; i<cbuf.limit(); i++) {
out += cbuf.get(i);
}
is a bottleneck in my program when the byte size is a moderate 70kb. There must be a faster way doing it. I would appreciate any suggestions.
  • 1. Re: quick way to copy byte buffer to string
    796440 Guru
    Currently Being Moderated
    You haven't said what type cbuf is, and limit() is not a method of any Java class I'm familiar with, so I can't say whether fetching with get(i) is optimal or not. However, rather than building up a String in the loop, use a StringBuilder or StringBuffer.

    [http://java.sun.com/javase/6/docs/api/java/lang/package-frame.html]
  • 2. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    If cbuf is a CharBuffer:
    String s = cbuf.toString();
    if cbuf is a ByteBuffer (as your subject line claims?)
    String s = cbuf.asCharBuffer().toString();
    Moral: The [API |http://java.sun.com/javase/6/docs/api/] is your boo.
  • 3. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    Ok, my bad. Yes this was the CharBuffer. Actually I screwed up in utf encoding while sending all this crap over the sockets. But this piece just slowed everything down in addition. Still, let me formulate it as a general question. I encode a String into utf8 byte[] array. Then I write it to socket using ByteBuffer. Then I read it from socket using ByteBuffer. Then I decode it back to String. In the process, what should be the correct way to convert byte buffer into byte[] and char buffer into string. Is it array() and toString() methods???
  • 4. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    What happens when you try that?
  • 5. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    toString() is irrelevant in the code, I use it just to look at the results. Though when I display it in TextArea it looks like the encoding is broken. May be I just do smthing wrong. The array() does break the code. It works only if I replace it with
    int n = buf.limit();
    byte[] data = new byte[n];
    for(int i=0; i<n; i++) {
    data[i] = buf.get(i);
    }
    return data;
  • 6. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    The array() method is optional -- is that what you are alluding to?

    There is also a bulk get method, but it might not be noticeably faster, if that is your concern.

    As for the "encoding" being broken, are you encoding and decoding consistently, hmm?
  • 7. Re: quick way to copy byte buffer to string
    807589 Newbie
    Currently Being Moderated
    To be more specific I am playing with something like a basic web proxy. That is, the browser loads pages not directly from server but through this piece of java code. The current problem is that some pages do load some dont. The loading process seems just to get freezed at some moment. I guess that this could be for all kind of reasons. One of them I think was this problem with conversion. Anyway, I guess I'll need to explore it more. Thanks for the help. But any suggestions which directions to look at would be very welcome