如何将字节数组转换为字符串,并将字符串转换为使用GWT的字节数组? [英] How to convert a byte array to a string, and string to a byte array with GWT?

查看:128
本文介绍了如何将字节数组转换为字符串,并将字符串转换为使用GWT的字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String(byte [] bytes)构造函数和 String.getBytes()方法不是由GWT实现的JRE仿真字符串类。

有人知道一个实现吗?我不想使用 char [] ,但似乎没有其他解决方案。

解决

  public class GwtPlayground 

下面的代码应该可以工作,只需指定每个字符的字节数。实现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+\\\↑+\\\ñ+\\\ü+C);

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

字符串roundTrip = getString(utfBytes,2);
logger.info(roundTrip =+ roundTrip);

$ b $ 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)));
}
返回到返回;

$ b $ 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]));
}
}
返回新的字符串(字符);
}
}

正如@Per Wiklander指出的那样,真正支持UTF-8。这是一个真正的UTF-8解码器,从C 这里

  private static class UTF8Decoder 
{
final byte [] the_input;
int the_index,the_length;

保护UTF8Decoder(byte [] bytes)
{
super();
this.the_input = bytes;
this.the_index = 0;
this.the_length = bytes.length;
}


/ *
获取下一个字节。如果没有更多字节,它将返回UTF8_END。
* /
int get()
{
int c;
c = the_input [the_index]& 0xFF的;
the_index + = 1;
return c;
}


/ *
获取下一个继续字节的6位有效负载。
如果不是连续字节,则返回UTF8_ERROR。
* /
int cont()
{
int c = get(); ((c& 0xC0)== 0x80)
return(c& 0x3F);
else
抛出新的IllegalArgumentException(未能通过严格的UTF-8);


CharSequence getStringUTF8()
{
StringBuilder sb = new StringBuilder(the_input.length); //分配最大大小
while(the_index< the_length)
{
int c; / *字符的第一个字节* /
int r; / *结果* /

c = get();
/ *
如果((c& 0x80)== 0)

sb),则零延续(0至127)
* /
。 append((char)c);


一个延续(128到2047)
* /
else if((c& 0xE0)== 0xC0)
{
int c1 = cont();如果(c1> = 0)
{
r =((c& 0x1F)<< 6)|
, C1;
if(r> = 128)
sb.append((char)r);
else
抛出new IllegalArgumentException();



两个延续(2048到55295和57344到65535)
* /
else if((c& 0xF0) == 0xE0)
{
int c1 = cont();
int c2 = cont(); ((c1 | c2)> = 0)
{
r =((c& 0x0F)<= 12) (c1 <6)| C2; (r> = 2048&&(r< 55296 || r> 57343))
sb.append((char)r);
else
抛出new IllegalArgumentException();



三个延续(65536到1114111)
* /
else if((c& 0xF8)== 0xF0)
{
int c1 = cont();
int c2 = cont();
int c3 = cont(); ((c1 | c2 | c3)> = 0)
bb((char)((((c& 0x0F)<< 18)|(c1 << ; 12)|(c2 << 6)| c3)+ 65536)); // TODO这可能不起作用,因为它被转换为char
}
else
抛出新的IllegalArgumentException(失败的严格UTF8解析);
}
return sb;
}
}


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.

解决方案

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);
    }
}

As @Per Wiklander pointed out, this doesn't truely support UTF-8. Here is a true UTF-8 decoder ported from C here

private static class UTF8Decoder
{
    final byte[] the_input;
    int the_index, the_length;

    protected UTF8Decoder( byte[] bytes )
    {
        super();
        this.the_input = bytes;
        this.the_index = 0;
        this.the_length = bytes.length;
    }


    /*
    Get the next byte. It returns UTF8_END if there are no more bytes.
    */
    int get()
    {
        int c;
        c = the_input[the_index] & 0xFF;
        the_index += 1;
        return c;
    }


    /*
        Get the 6-bit payload of the next continuation byte.
        Return UTF8_ERROR if it is not a contination byte.
    */
    int cont()
    {
        int c = get();
        if( (c & 0xC0) == 0x80 )
            return (c & 0x3F);
        else
            throw new IllegalArgumentException( "Failed to pass strict UTF-8" );
    }

    CharSequence getStringUTF8()
    {
        StringBuilder sb = new StringBuilder( the_input.length ); // allocate a maximum size
        while( the_index < the_length )
        {
            int c; /* the first byte of the character */
            int r; /* the result */

            c = get();
            /*
                Zero continuation (0 to 127)
            */
            if( (c & 0x80) == 0 )
            {
                sb.append( (char) c );
            }
            /*
                One continuation (128 to 2047)
            */
            else if( (c & 0xE0) == 0xC0 )
            {
                int c1 = cont();
                if( c1 >= 0 )
                {
                    r = ((c & 0x1F) << 6) | c1;
                    if( r >= 128 )
                        sb.append( (char) r );
                    else
                        throw new IllegalArgumentException();
                }
            }
            /*
            Two continuation (2048 to 55295 and 57344 to 65535)
            */
            else if( (c & 0xF0) == 0xE0 )
            {
                int c1 = cont();
                int c2 = cont();
                if( (c1 | c2) >= 0 )
                {
                    r = ((c & 0x0F) << 12) | (c1 << 6) | c2;
                    if( r >= 2048 && (r < 55296 || r > 57343) )
                        sb.append( (char) r );
                    else
                        throw new IllegalArgumentException();
                }
            }
            /*
            Three continuation (65536 to 1114111)
            */
            else if( (c & 0xF8) == 0xF0 )
            {
                int c1 = cont();
                int c2 = cont();
                int c3 = cont();
                if( (c1 | c2 | c3) >= 0 )
                    sb.append( (char) ((((c & 0x0F) << 18) | (c1 << 12) | (c2 << 6) | c3) + 65536) ); // TODO this might not work as it is being cast to a char
            }
            else
                throw new IllegalArgumentException( "Failed strict UTF8 parsing" );
        }
        return sb;
    }
}

这篇关于如何将字节数组转换为字符串,并将字符串转换为使用GWT的字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆