与string.getBytes()混淆

时间:2015-05-27 08:30:23

标签: java mongodb bson

我有一个字节数组String strObj = new String(byteObj) System.out.println(byteObj.length) System.out.println(strObj.getBytes().length) ,由BSON序列化。

152

结果为154152 154 [-104, 0, 0, 0, 4, 116, 105, 116, 108, 101, 0, 80, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 3, 0, 0, 0, 105, 115, 0, 2, 50, 0, 2, 0, 0, 0, 97, 0, 2, 51, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 52, 0, 2, 0, 0, 0, 97, 0, 2, 53, 0, 3, 0, 0, 0, 105, 115, 0, 2, 54, 0, 6, 0, 0, 0, 116, 105, 116, 108, 101, 0, 0, 4, 99, 111, 110, 116, 101, 110, 116, 0, 51, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 2, 0, 0, 0, 97, 0, 2, 50, 0, 8, 0, 0, 0, 99, 111, 110, 116, 101, 110, 116, 0, 2, 51, 0, 3, 0, 0, 0, 105, 115, 0, 0, 0] [-17, -65, -67, 0, 0, 0, 4, 116, 105, 116, 108, 101, 0, 80, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 3, 0, 0, 0, 105, 115, 0, 2, 50, 0, 2, 0, 0, 0, 97, 0, 2, 51, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 52, 0, 2, 0, 0, 0, 97, 0, 2, 53, 0, 3, 0, 0, 0, 105, 115, 0, 2, 54, 0, 6, 0, 0, 0, 116, 105, 116, 108, 101, 0, 0, 4, 99, 111, 110, 116, 101, 110, 116, 0, 51, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 2, 0, 0, 0, 97, 0, 2, 50, 0, 8, 0, 0, 0, 99, 111, 110, 116, 101, 110, 116, 0, 2, 51, 0, 3, 0, 0, 0, 105, 115, 0, 0, 0] 。而这两个字节数组并不相同。如何从字符串中恢复原始的bson字节数组?

更新

    BSONObject ob = new BasicBSONObject()
            .append("title", Arrays.asList(new String[]{"this", "is", "a", "this", "a", "is", "title"}))
            .append("content", Arrays.asList(new String[]{"this", "a", "content", "is"}));


    byte[] ahaha = BSON.encode(ob);
    BSON.decode(ahaha);

    // BSON.decode(new String(ahaha).getBytes());

    byte[] strByte = new String(ahaha).getBytes();

    System.out.println(ahaha.length + "\t" + strByte.length);
    System.out.println(Arrays.toString(ahaha));
    System.out.println(Arrays.toString(strByte));

首先是BSON字节数组。

更新2: 测试代码

TextBlock

有关将二进制数据转换为字符串的解决方案,请参阅fdopen(),反之亦然。

2 个答案:

答案 0 :(得分:1)

区别的原因是将字节转换为String。请注意,第一个字节是负数。以下是Javadoc的解释:

新String的长度是字符集的函数,因此可能不等于字节数组的长度。 当未在指定给定字节在默认字符集中无效时,此构造函数的行为未指定。

当需要对解码过程进行更多控制时,应使用CharsetDecoder类。

答案 1 :(得分:0)

我无法重现这个问题。以下代码返回相同的长度(152),字节相同:

byte[] bs = {-104, 0, 0, 0, 4, 116, 105, 116, 108, 101, 0, 80, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 3, 0, 0, 0, 105, 115, 0, 2, 50, 0, 2, 0, 0, 0, 97, 0, 2, 51, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 52, 0, 2, 0, 0, 0, 97, 0, 2, 53, 0, 3, 0, 0, 0, 105, 115, 0, 2, 54, 0, 6, 0, 0, 0, 116, 105, 116, 108, 101, 0, 0, 4, 99, 111, 110, 116, 101, 110, 116, 0, 51, 0, 0, 0, 2, 48, 0, 5, 0, 0, 0, 116, 104, 105, 115, 0, 2, 49, 0, 2, 0, 0, 0, 97, 0, 2, 50, 0, 8, 0, 0, 0, 99, 111, 110, 116, 101, 110, 116, 0, 2, 51, 0, 3, 0, 0, 0, 105, 115, 0, 0, 0};

System.out.println(new String(bs).getBytes().length);
System.out.println(bs.length);