gpt4 book ai didi

java - 新字符串(字节)在 GWT 客户端中导致 "Maximum call stack size exceeded"

转载 作者:行者123 更新时间:2023-12-01 11:07:01 24 4
gpt4 key购买 nike

以下内容适用于短字符串

result = new String(bytes);

我有一个大约 140000 字节的 byte[],当我尝试使用上面的代码从中创建一个 String 时,浏览器会抛出异常 超过最大调用堆栈大小。我正在使用 GWT 2.6.0。

我相信创建大型数组时可能会抛出此异常,但我找不到在 GWT 中从 byte[] 创建字符串的替代方法。我可以使用任何建议或解决方法来使其正常工作吗?

下面是 JavaScript 异常和调用堆栈。

Uncaught RangeError: Maximum call stack size exceeded
valueOf_86 @ String.java:630
utf8ToString @ String.java:210
_String_0 @ String.java:322
_String_1 @ String.java:237
handleHttpResponse_7 @ BaseRequest.java:38
onResponseReceived @ GwtNet.java:56
$fireOnResponseReceived @ Request.java:315
onReadyStateChange_1 @ RequestBuilder.java:42
(anonymous function) @ XMLHttpRequest.java:402
apply_24 @ Impl.java:58
entry0 @ Impl.java:385
(anonymous function) @ Impl.java:91

最佳答案

我无法找到一个简单的方法来解决这个问题,所以我最终编写了一个方法来将 UTF-8 字节数组切成 block 并逐 block 解码。如果有人知道更好的方法,请发布新答案。

/**
* Decoding very long strings in GWT causes exceptions, so anything that could be decoding a large string in
* GWT must use this method instead
* @param bytes The array of UTF-8 bytes
* @return String representation of the array
* @throws UnsupportedEncodingException
*/
public static String getUTF8String(byte[] bytes) throws UnsupportedEncodingException {
StringBuilder stringBuilder = new StringBuilder();
int charLength;
int lastDecodeOffset = 0;
int charCount = 0;

int offset = 0;
while (offset < bytes.length) {
if ((bytes[offset] & 0b10000000) == 0b00000000) charLength = 1;
else if ((bytes[offset] & 0b11100000) == 0b11000000) charLength = 2;
else if ((bytes[offset] & 0b11110000) == 0b11100000) charLength = 3;
else if ((bytes[offset] & 0b11111000) == 0b11110000) charLength = 4;
else if ((bytes[offset] & 0b11111100) == 0b11111000) charLength = 5;
else if ((bytes[offset] & 0b11111110) == 0b11111100) charLength = 6;
else throw new UnsupportedEncodingException("Invalid UTF-8 data");

if (offset + charLength > bytes.length) {
throw new UnsupportedEncodingException("Invalid UTF-8 data");
}

offset += charLength;
charCount++;

if (charCount > 2048 || offset >= bytes.length) {
stringBuilder.append(new String(bytes, lastDecodeOffset, offset - lastDecodeOffset, "UTF-8"));
lastDecodeOffset = offset;
charCount = 0;
}
}

return stringBuilder.toString();
}

关于java - 新字符串(字节)在 GWT 客户端中导致 "Maximum call stack size exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32844235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com