简体   繁体   English

BlackBerry UTF-8 InputStreamReader出现套接字问题

[英]BlackBerry UTF-8 InputStreamReader on Socket issue

I'm trying to read the response from a server using a socket and the information is UTF-8 encoded. 我正在尝试使用套接字从服务器读取响应,并且该信息是UTF-8编码的。 I'm wrapping the InputStream from the socket in an InputStreamReader with the encoding set to "UTF-8". 我将来自套接字的InputStream包装在InputStreamReader中,并将编码设置为“ UTF-8”。

For some reason it seems like only part of the response is read and then the reading just hangs for about a minute or two and then it finishes. 由于某种原因,似乎只读取了部分响应,然后读取仅挂起一两分钟,然后结束。 If I set the encoding on the InputStreamReader to "ISO-8859-1" then I can read all of the data right away, but obviously not all of the characters are displayed correctly. 如果将InputStreamReader上的编码设置为“ ISO-8859-1”,则可以立即读取所有数据,但显然并非所有字符都可以正确显示。

Code looks something like the following 代码如下所示

socketConn = (SocketConnection)Connector.open(url);
InputStreamReader is = new InputStreamReader(socketConn.openInputStream(), "UTF-8");

Then I read through the headers and the content. 然后我通读标题和内容。 The content is chunked and I read the line with the size of each chunk (convert to decimal from hex) to know how much to read. 内容被分块,我读取了每个块的大小(从十六进制转换为十进制)的行,以了解要读取的内容。

I'm not understanding the difference in reading with the two encodings and the effect it can have because it works without issue with ISO-8859-1 and it works eventually with UTF-8, there is just the long delay. 我不了解两种编码在读取上的区别及其可能产生的效果,因为它与ISO-8859-1毫无问题,并且最终与UTF-8兼容,只是延迟很长。

It's hard to get the reason of the delay. 很难找到延迟的原因。

You may try another way of getting the data from the network: 您可以尝试另一种从网络获取数据的方法:

byte[] data = IOUtilities.streamToBytes(socketConn.openInputStream());

I believe the above should be passed without delay. 我相信以上内容应立即通过。 Then having got the bytes from network you can start data processing. 然后从网络获取字节,就可以开始数据处理。 Note you can always get a String from bytes representing a string in UTF-8 encoding: 请注意,您始终可以从以UTF-8编码表示字符串的字节中获取String

String stringInUTF8 = new String(bytes, "UTF-8");

UPDATE : see the second comment to this post. 更新 :请参阅此帖子的第二条评论。

I was already removing the chunk sizes on the fly so I ended up doing something somewhat similar to the IOUtilities answer. 我已经在动态删除数据块大小,因此最终做了一些类似于IOUtilities答案的操作。 Instead of using an InputStreamReader I just used the InputStream. 我没有使用InputStreamReader,而是使用了InputStream。 InputStream has a read method that can fill an array of bytes, so for each chunk the code looks something like this InputStream具有可以填充字节数组的read方法,因此对于每个块,代码看起来都像这样

byte[] buf = new buf[size];
is.read(buf);
return new String(buf, "UTF-8");

This seems to work, doesn't cause any delays and I can remove the extra information about the chunks on the fly. 这似乎可行,不会造成任何延迟,我可以即时删除有关块的额外信息。

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

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