gpt4 book ai didi

java - BufferedInputStream 被读取到最后,但是 "discards?"数据

转载 作者:行者123 更新时间:2023-12-01 13:36:48 26 4
gpt4 key购买 nike

我有一个要调试的 BufferedInputStream。为此,我使用此方法(Log.d 和 Log.wtf 只是 Android 特定的日志记录工具,除此之外其行为不应与纯 Java 不同):

    public static final String toString(BufferedInputStream is) {
String ret = "";
int readBytes = 0;

is.mark(Integer.MAX_VALUE);

if (is != null) {
Writer writer = new StringWriter();

char[] buffer = new char[1024];
try {
Reader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"));
}
catch (UnsupportedEncodingException e) {
Log.wtf("NX4", "Should never happen!", e);
}
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
readBytes += n;
}
}
catch (IOException e) {
Log.wtf("NX4", "Should never happen!", e);
}
finally {
try {
is.reset();
}
catch (IOException e) {
Log.wtf("NX4", "Should never happen!", e);
}
}
ret = writer.toString();
}

Log.d("NX4", "Read bytes: " + readBytes);

return ret;
}

当我运行时,例如 this online XML file against which I'm testing right now ,同时我手动将文件下载到桌面,以便比较上述方法的输出和原始文件,发生了我完全无法理解的事情:

  1. 该方法正确读取 5664 字节(来自 Log.d(...) 调用的信息)。
  2. 原始文件有 5664 个字符,这符合第一个事实。
  3. 上述方法的输出长度只有4254个字符!

我认为这可能与奇怪的间距问题有关,我不知道为什么但正在发生,不用说我不知道​​如何停止:

Comparison of the original file and the output of the method

编辑添加了 BufferedInputStream 创建片段。

    URL source = new URL(srcString);
URLConnection urlConnection = source.openConnection();
urlConnection.connect();
in = new BufferedInputStream(urlConnection.getInputStream());

最佳答案

也许不良内容是来自任何控制台的复制和粘贴结果。检查这个:URLConnection cannot retrive complete Html

关于java - BufferedInputStream 被读取到最后,但是 "discards?"数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21214972/

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