• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fast way to convert a primitve byte array to a string

 
Mike Dooley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a program which converts a primitive (byte[]) byte array to a String and the other way around.
Actual i use the String constructor
sample:
byte[] b;
String s = new String(b);
or byte[] b = s.getBytes();
Its one of the hotspots in the program and therefore i search for a faster way to convert this byte arrays to a String.
Mike
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt you're going to get this particular conversion to go any faster than it does in the String contructor, or getBytes(). If indeed it's a bottleneck that you can benefit from removing, then perhaps your best course of action would be to find a way to redefine your processing so that either the byte[] or the String is unnecessary. It's hard to say how to do this without knowing more about what you're doing here. But one possibility is to switch from traditional IO to NIO, and replace the byte[] with a ByteBuffer which can be converted to a CharBuffer using a CharsetDecoder. Note that CharBuffer implements CharSequence, which means that in many cases you can treat it like a String without actually taking the time to convert it to a String. E.g. you can use a regex Pattern to search it for something. Even if you do need to convert to a standard String, doing it with NIO may well be quicker than standard IO. The key is that ByteBuffer and CharBuffer can make use of system memory outside the JVM, which allows much better native platform optimizations. Bringing data into a byte[] or String is a comparatively slow process, and bringing data into a byte[] and a String is even slower. So try to eliminate the need for both; failing that, try to eliminate the need for at least one.
Incidentally, what's a typical length for the Strings you're dealing with? Are you creating lots and lots of little strings, or a few really big strings? Or lots of big strings? If it's lots of little strings, and they're all coming from the same source (e.g. one big file) then you might benefit from doing your conversions in bulk. E.g. read everything into one big byte[] or ByteBuffer, then convert it all to one String or CharBuffer, and then break the data into smaller parts as necessary using substring() or subSequence().
If those suggestions don't help, post some more details about where the data is coming from, what you're doing to it, and where it's going.
 
Mike Dooley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The conversion from byte[] to String and the other way around is used for network serializiation and thats the central point in the application. The application gets its information from the server in a byte[] and must convert this to String, because the information is displayed in the gui. And on a new request the client has his information as String and must convert this String to a byte[].
You suggest nio. Oh yes i often think about this API, but in this project is presently not possible, because i am limited to the features of 1.3.1. And its not possible to go to 1.4.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35716
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sre the bottleneck is the byte[]/String conversion rather than the network transfer? Usually network transfer has more overhead.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jeanne on that point. I'd recommend rechecking your profiling data.
 
Mike Dooley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i agree to you that the bottleneck is the network load(very fast network). But actual its not possible to decrease the size of network transfer therefore i am looking for other performance improvements. And one could be a faster transformation of the byte[] to a String.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!