The String(byte[] bytes) constructor and String.getBytes() method are not implemented by GWT JRE emulation String class.

Does anybody know of an implementation? I do not want to use char[], But it seems like there is no other solution.

share|improve this question

50% accept rate
What are you trying to achieve? Where are you getting byte[] from? – Peter Knego Jan 3 '11 at 14:14
I had implemented a space efficient serialization protocol for swing clients, I am trying to adopt this protocol for gwt clients.. – Gursel Koca Jan 3 '11 at 14:25
what is the character encoding of your byte array or do you want the conversion to be flexible? – LINEMAN78 Jan 4 '11 at 23:20
if I can support utf-8, it would be okay. – Gursel Koca Jan 4 '11 at 23:23
feedback

2 Answers

Good question. I didn't realize it before.

as far as I know there is only 2 main method that convert byte array to String

  1. You mentioned it
  2. The fantastic way with java.io package that you can't use it on client-side

Here is mine implementation. I think it may be helpful to you

public static String convertByteArrayToString(byte[] byteArray) {
    String s = "";

    for (int i = 0; i < byteArray.length; i++) {
        s += (char) (byteArray[i]);
    }

    return s;
}

You can test it :

byte[] byteArray = new byte[] { 87, 79, 87, 46, 46, 46 };

System.out.println(convertByteArrayToString(byteArray));
System.out.println(new String(byteArray));
share|improve this answer
your method works only for asci characters not for unicode characters.. I will suggest you to read joelonsoftware.com/articles/Unicode.html – Gursel Koca Jan 3 '11 at 18:16
feedback

The following code should work, just specify the number of bytes per character.

public class GwtPlayground implements EntryPoint
{
    static final Logger logger = Logger.getLogger("");

    @Override
    public void onModuleLoad()
    {
        VerticalPanel loggerArea = new VerticalPanel();
        logger.addHandler(new HasWidgetsLogHandler(loggerArea));
        RootPanel.get().add(loggerArea);

        String original = new String("A" + "\uffea" + "\u00f1" + "\u00fc" + "C");

        logger.info("original = " + original);
        byte[] utfBytes = getBytes(original, 2);

        String roundTrip = getString(utfBytes, 2);
        logger.info("roundTrip = " + roundTrip);
    }

    public static byte[] getBytes(String string, int bytesPerChar)
    {
        char[] chars = string.toCharArray();
        byte[] toReturn = new byte[chars.length * bytesPerChar];
        for (int i = 0; i < chars.length; i++)
        {
            for (int j = 0; j < bytesPerChar; j++)
                toReturn[i * bytesPerChar + j] = (byte) (chars[i] >>> (8 * (bytesPerChar - 1 - j)));
        }
        return toReturn;
    }

    public static String getString(byte[] bytes, int bytesPerChar)
    {
        char[] chars = new char[bytes.length / bytesPerChar];
        for (int i = 0; i < chars.length; i++)
        {
            for (int j = 0; j < bytesPerChar; j++)
            {
                int shift = (bytesPerChar - 1 - j) * 8;
                chars[i] |= (0x000000FF << shift) & (((int) bytes[i * bytesPerChar + j]) << shift);
            }
        }
        return new String(chars);
    }
}
share|improve this answer
Well, the problem is we dont know how may bytes is needed to encode a character with utf-8.. If I use utf-16 , it is okay, we know that every character is represented with 2 bytes.. – Gursel Koca Jan 5 '11 at 7:44
UTF-8 by definition is 1 byte per character. Hence the 8 for 8 bits and the 16 for 16 bits. That is why I made the number of bytes variable. – LINEMAN78 Jan 5 '11 at 9:27
@LINEMAN78 That is only correct for characters that map to ascii. I'm quoting Joel Spolsky here: "In UTF-8, every code point from 0-127 is stored in a single byte. Only code points 128 and above are stored using 2, 3, in fact, up to 6 bytes." joelonsoftware.com/articles/Unicode.html – Per Wiklander Jul 23 at 21:56
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.