我是一名Java新手,不太清楚如何完成以下操作:
Scala应用程序在某处将字符串转换为字节:
ByteBuffer.wrap(str.getBytes)
我将此字节数组收集为Java的
String
,我希望执行与上面的Scala代码相反的操作,因此获取原始字符串(上面的对象str
)。一开始将
ByteBuffer
转换为String
是我唯一的选择,因为我正在从AWS Kinesis流中读取它(难道不是吗?)。Scala代码也不应更改。示例字符串:
String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";
如何在Java中实现?
编辑:
好的,我会尝试更详细地说明这个过程:
- A 3rd party Scala application produces CSV rows which I need to consume
Before storing those rows in an AWS Kinesis stream, the application does the following to each row:
ByteBuffer.wrap(output.getBytes);
I read the data from the stream as a string, and the string could look like the following one:
String str = "AAAAAAAAAAGZ7dFR0XmV23BRuufU+eCekJe6TGGUBBu5WSLIse4ERy9............";
I need to restore the contents of the string above into its original, readable, form;
str.toCharArray.map(_.toByte)
这样的代码不起作用吗? - 4lex1vmap(_.toByte)
部分做了什么。 - Yuval HerzigerString
构造函数中,还是使用类似于 base64 的编码方式? - Mike StrobelGetShardIteratorResult.getShardIterator()
只返回字符串,而GetRecordsRequest getRecordsRequest = new GetRecordsRequest();
以及getRecords(getRecordsRequest);
则得到所需的 ByteBuffer 类型。 - Yuval Herziger