如何将在Java中以字符串表示的ByteBuffer转换为字节数组

3

我是一名Java新手,不太清楚如何完成以下操作:

Scala应用程序在某处将字符串转换为字节:

ByteBuffer.wrap(str.getBytes)

我将此字节数组收集为Java的String,我希望执行与上面的Scala代码相反的操作,因此获取原始字符串(上面的对象str)。
一开始将ByteBuffer转换为String是我唯一的选择,因为我正在从AWS Kinesis流中读取它(难道不是吗?)。Scala代码也不应更改。
示例字符串:
String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";

如何在Java中实现?
编辑:
好的,我会尝试更详细地说明这个过程:
  1. A 3rd party Scala application produces CSV rows which I need to consume
  2. Before storing those rows in an AWS Kinesis stream, the application does the following to each row:

    ByteBuffer.wrap(output.getBytes);
    
  3. I read the data from the stream as a string, and the string could look like the following one:

    String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";
    
  4. I need to restore the contents of the string above into its original, readable, form;

我希望现在已经更清楚了,之前把你们搞糊涂了真是不好意思。

在Java中,类似str.toCharArray.map(_.toByte)这样的代码不起作用吗? - 4lex1v
可能吧,但我不确定你在 map(_.toByte) 部分做了什么。 - Yuval Herziger
3
我如何读取流数据作为字符串?你是将字节数组传递到 String 构造函数中,还是使用类似于 base64 的编码方式? - Mike Strobel
1
我在谷歌上搜索了AWS Kinesis,看起来他们对记录进行了Base64编码。我已经更新了我的答案。 - aioobe
从中学到的另一个教训是:GetShardIteratorResult.getShardIterator() 只返回字符串,而 GetRecordsRequest getRecordsRequest = new GetRecordsRequest(); 以及 getRecords(getRecordsRequest); 则得到所需的 ByteBuffer 类型。 - Yuval Herziger
5个回答

3
如果你想从byte[]转换为String,请尝试使用new String(yourBytes)getBytesString(byte[])都使用默认字符编码。
来自Amazon Kinesis Service API Reference

要放入记录的数据块,在序列化时进行Base64编码。

你需要对字符串进行Base64解码。在Java 8中,代码如下:
byte[] bytes = Base64.getDecoder().decode("AAAAAAAAAAGZ7dFR0XmV23BR........");
str = new String(bytes, "utf-8"));

其他选项:Java中的Base64编码

你能详细说明一下吗?我认为你基本上想要的是getBytes的反向操作? - aioobe
当然可以:我有一个看起来像这样的字符串: "String str = "AAAAAAAAAAGZ7dFR0XmV23BR"。我知道它已经被转换为字节,但是我得到的是字符串类型。我想以可读形式了解那些字节背后的内容。 - Yuval Herziger
但是"AAAAAAAAAAGZ7dFR0XmV23BR"是如何生成的呢?您提到了getBytes,但它返回的是byte[]而不是String。 - aioobe
非常抱歉,我的问题组织得有些混乱。情况是这样的:1. Scala应用程序获取一个字符串,执行“getBytes()”,将其包装在ByteBuffer中。---> 2. 我将此ByteBuffer读取为字符串。--->我想知道最初字符串的内容是什么。在一行代码中,这就是对原始字符串所做的操作:ByteBuffer.wrap(output.getBytes) - Yuval Herziger
我很难理解。你能否提供一个逐步示例来说明每个阶段的字符串/字节数据以及最终期望的结果? - Mike Strobel
显示剩余3条评论

1
我不确定我是否完全理解问题,但你是指这个吗?
String decoded = new String(bytes);

0

抱歉,答案错误。 再次说明,ByteBuffer是Java类,因此它们可能以相同的方式工作。 您需要Java版本..

来自kafka ApiUtils:

def writeShortString(buffer:ByteBuffer,string:String){
   if(String == null){
       buffer.putShort(-1)
   }
   else{
     val encodedString = string.getBytes(“utf-8”)
     if(encodedString.length > Short.MaxValue){
         throw YourException(Your Message)
     else{
        buffer.putShort(encodedString.length.asInstanceOf[Short])
        buffer.put(encodedString)
   }
  }

}


0
public static void main(String[] args){
    String decoded = new String(bytesData);
    String actualString;
    try{
       actualString = new String(bytesData,"UTF-8");
       System.out.printLn("String is" + actualString);
    }catch(UnsupportedEncodingException e){
       e.printstacktrace();
    }
}

你所建议的方法只是将字符串视为其可读形式的内容。原始字符串看起来像这样: String str = "AAAAAAAAAAGZ7dFR0XmV23BR........" - Yuval Herziger

0

对于 Kinesis 数据块:

private CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
decoder.decode(record.getData()).toString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接