简体   繁体   English

将字节数组正确解码为char数组

[英]Proper decoding of byte array to char array

In making password handling more secure by eliminating storage in Strings (which end up on the heap). 通过消除字符串存储(最终存储在堆中)来使密码处理更加安全。 I have the following existing code: 我有以下现有代码:

String pw = new String(buffer, 0, len, "UTF-32LE");

I came up with: 我想出了:

Charset charSet = Charset.forName("UTF-32LE");
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer, 0, len);
CharBuffer charBuffer = charSet.decode(byteBuffer);
charArray = new char[charBuffer.length()];
for (int i = 0; i < charBuffer.length(); ++i)
{
    charArray[i] = charBuffer.charAt(i);
}

Note that we support many different languages, so I'm not quite sure how best to thoroughly test this approach. 请注意,我们支持许多不同的语言,因此我不确定如何最好地全面测试这种方法。

  1. Is this correct? 这个对吗? Are there caveats to this approach? 这种方法有警告吗?
  2. Is this the best approach or am I missing something simpler? 这是最好的方法还是我缺少一些简单的方法?

Thanks for any feedback or advice. 感谢您的任何反馈或建议。

I am not sure what are you trying to achieve. 我不确定您要达到什么目标。 At first I had thought you want to get rid of data being stored on the heap but then I saw array of chars. 起初,我以为您想摆脱存储在堆中的数据,但是后来我看到了字符数组。 In java every array is an object and every object is stored on the heap. 在Java中,每个数组都是一个对象,每个对象都存储在堆中。 Reference variables can land on the stack but they are only handlers not the object itself. 参考变量可以放在堆栈上,但它们只是处理程序,而不是对象本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM