简体   繁体   English

读取BufferedInputStream直到结束,但“丢弃?”数据

[英]BufferedInputStream is read until the end but “discards?” data

I have a BufferedInputStream which I want to debug. 我有一个要调试的BufferedInputStream。 For such purpose I use this method (Log.d and Log.wtf are just Android-specific logging tools, other than that the behaviour should not be different from pure Java): 为此,我使用此方法(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;
}

When I run with for example, this online XML file against which I'm testing right now , and at the same time I download the file manually to my desktop in order to compare both the output of the above method and the original file, something happens that I can not understand at all: 例如,当我使用该在线XML文件运行时,我正在对其进行测试 ,与此同时,我将该文件手动下载到桌面上,以便比较上述方法的输出和原始文件,发生了我根本无法理解的事情:

  1. The method properly reads 5664 bytes (info from the Log.d(...) call). 该方法正确读取5664个字节(来自Log.d(...)调用的信息)。
  2. The original file has 5664 characters, which makes sense with the first fact. 原始文件有5664个字符,从第一个事实开始就可以理解。
  3. The output of the above method has a length of only 4254 characters! 上面方法的输出长度只有4254个字符!

I think this could be somehow related to weird spacing issues which I don't know why but are happening, and it's needless to say that I have no idea on how to stop: 我认为这可能与怪异的间距问题有关,我不知道为什么会发生这种奇怪的间距问题,而且正在发生,不用说我不知道​​如何停止:

原始文件与方法输出的比较

EDIT Added BufferedInputStream creation snippet. 编辑添加了BufferedInputStream创建代码段。

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

Maybe the bad content is a copy and paste result from any console. 不良内容可能是任何控制台的复制和粘贴结果。 Check this: URLConnection cannot retrive complete Html 请检查以下内容: URLConnection无法检索完整的HTML

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

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