开发者

Using an InputStream for Logging and then XML parsing

What I want to do is log the output from an inputstream that I go using

org.apache.http.HttpEntity entity = response.getEntity();
org.apache.http.HttpResponse content =entity.getContent();


            //Print the result to the screen for debugging
            //puroposes
            if(Logging.DEBUG) {
                InputStream content =entity.getContent();

      开发者_如何学运维          int i;
                StringBuilder b = new StringBuilder();
                while( (i=content.read()) != -1 ) {
                    b.append((char)i);
                }

                Log.d(TAG, b.toString());
            }

Now after I have finished logging, I want to use the exact same stream through an XML parser. The problem is that it tells me that the steam has already been used.

I tried to the use mark() and reset() calls before and after debugging but it didn't work.


It depends whether the inputstream that is returned supports it. The default implementation in the InputStream class does nothing, as described in the API. So you can't be sure whether the returned Stream actually supports it. To be sure of this, you should wrap it in a BufferedInputStream, which does supports these methods.


In general mark() and reset() won't work on an arbitrary InputStream. They only work on subclasses like FileInputStream where the underlying data source supports these operations.

For something like a SocketInputStream or a console InputStream, your only option will be to read and buffer the entire stream contents somewhere; e.g. in memory or by writing it to a temporary file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜