Reputation: 1066
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:
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.
URL source = new URL(srcString);
URLConnection urlConnection = source.openConnection();
urlConnection.connect();
in = new BufferedInputStream(urlConnection.getInputStream());
Upvotes: 1
Views: 464
Reputation: 1234
Maybe the bad content is a copy and paste result from any console. Check this: URLConnection cannot retrive complete Html
Upvotes: 1