7 Replies Latest reply: Oct 30, 2007 4:20 PM by 807603 RSS

    String.getBytes() function

    807603
      I am looking to pass byte arrays over a socket and since the application I am developing is rather high performance, I would like to do it as efficiently as possible

      My problem is that in java.lang.String class the getBytes() function accepts a String as a the parameter and returns a byte array, which means everytime i want to convert a string to a byte array it will create a new byte array.

      What I would prefer would be to pass the getBytes function a String AND a byte array, then have it place the string into the byte array and return nothing (void)

      There appears to be a method that does this but it is depreciated, so I was wondering if anyone knew what is going on behind the scenes so I could write my own or if there is something that I am missing

      Thanks!
        • 1. Re: String.getBytes() function
          807603
          Micro improvements are often not worth it. Until you've profiled your app and determined a bottleneck, I wouldn't worry about this.

          On the other hand, it is a simple problem to solve. I would use String's getChars method, which lets you supply a char array.
          • 2. Re: String.getBytes() function
            807603
            BigDaddyCain wrote:
            I am looking to pass byte arrays over a socket and since the application I am developing is rather high performance, I would like to do it as efficiently as possible

            My problem is that in java.lang.String class the getBytes() function accepts a String as a the parameter and returns a byte array, which means everytime i want to convert a string to a byte array it will create a new byte array.

            What I would prefer would be to pass the getBytes function a String AND a byte array, then have it place the string into the byte array and return nothing (void)

            There appears to be a method that does this but it is depreciated, so I was wondering if anyone knew what is going on behind the scenes so I could write my own or if there is something that I am missing

            Thanks!
            You don't want to do this, it would be a huge waste of time. Converting a String to bytes involves using character encodings to map chars to bytes. If you really want to see how they do it, have a look in src.zip, it came with your java install. In the end though, all you'd have accomplished is changing this:
            String aString = "hello";
            byte[] bytes = aString.getBytes();
            to this:
            String aString = "hello";
            byte[] bytes = new byte[<how would you know what length to give it?];
            myGetBytes(bytes, aString);
            Just use the version you're supplied with.
            • 3. Re: String.getBytes() function
              807603
              The reason is that I am not trying to create a single byte array for each string but rather, i plan to use a byte array to hold a number of different messages, variables etc.

              so a sample byte array message may contain [int, int, double, long, long, string, string]

              So I would like to pass in a large byte array, say 1000 bytes and place each one in the appropriate place, ideally, i would like a method where i could pass a string, a byte array and an integer (byte, short whatever) to tell it where to start encoding the string

              I know this is a micro improvement of sorts, but i am working on this with a team and as this is my part (the communication between two remote portions of the app) I would like to ensure that it is as optimized as possible

              The reason is that I am not trying to create a single byte array for each string but rather, i plan to use a byte array to hold a number of different messages, variables etc.

              so a sample byte array message may contain [int, int, double, long, long, string, string]

              So I would like to pass in a large byte array, say 1000 bytes and place each one in the appropriate place, ideally, i would like a method where i could pass a string, a byte array and an integer (byte, short whatever) to tell it where to start encoding the string

              I know this is a micro improvement of sorts, but i am working on this with a team and as this is my part (the communication between two remote portions of the app) I would like to ensure that it is as optimized as possible
              • 4. Re: String.getBytes() function
                807603
                BigDaddyCain wrote:
                So I would like to pass in a large byte array, say 1000 bytes and place each one in the appropriate place, ideally, i would like a method where i could pass a string, a byte array and an integer (byte, short whatever) to tell it where to start encoding the string
                That's fine, but you don't need to reimplement String.getBytes to do that. Just get the bytes from the String, then copy those bytes into your larger array at the correct location.

                Be sure to pay attention to encoding issues. When you convert a String to bytes, a specific encoding is used. If you don't specify it with the getBytes(String) method, then the default is used. If you convert the bytes back into a String using a different encoding you can garble your characters. My recommendation is to use UTF-8 on both sides, and you'll be fine.
                • 5. Re: String.getBytes() function
                  807603
                  Thanks, I guess I see where you are coming from... I'll leave it the way it is for now and change it if it does turn out to be a bottleneck or memory hog

                  Any suggestions for doubles?

                  As of now I am using the Double.doubleToRawLongBits(), converting it to a long and then to a byte array, any suggestions for something quicker or slightly more elegant I could do?
                  • 6. Re: String.getBytes() function
                    807603
                    1) If you use DataOutputSteam and DataInputStream then most of your conversion problems go away.
                    2) If you are going to use String.getBytes() then explicitly specify the encoding otherwise you will always be worried about making sure the default character encoding is the same all on both client(s) and server.
                    • 7. Re: String.getBytes() function
                      807603
                      If you are putting different sorts of data (like doubles) into a byte array, and you want to reuse this byte array, you should definitely check out java.nio.ByteBuffer.