BufferedInputStream is read until the end but "discards?" data

I have a BufferedInputStream which I want to debug. 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):

    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:

  1. The method properly reads 5664 bytes (info from the Log.d(...) call).
  2. The original file has 5664 characters, which makes sense with the first fact.
  3. The output of the above method has a length of only 4254 characters!

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:

Comparison of the original file and the output of the method

EDIT Added BufferedInputStream creation snippet.

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

Upvotes: 1

Views: 464

Answers (1)

Aris2World
Aris2World

Reputation: 1234

Maybe the bad content is a copy and paste result from any console. Check this: URLConnection cannot retrive complete Html

Upvotes: 1

Related Questions