Logcat消息:BufferedInputStream构造函数中使用的默认缓冲区大小。

时间:2011-05-12 07:28:08

标签: java android bufferedinputstream

执行我的项目时,我收到logcat中显示的错误:

  

05-12 12:43:17.268:INFO / global(801):BufferedInputStream构造函数中使用的默认缓冲区大小。如果需要8k缓冲区,最好是明确的。

我的代码如下所示。我传递给commonParser()的数据是我从网络服务获得的长响应。

public void commonParser(String data)
{
    try
    {
        if(data!=null)
        {
            InputStream is = new ByteArrayInputStream(data.getBytes());
            Reader reader = new InputStreamReader(is, "UTF-8");
            InputSource inputSource = new InputSource(reader);
            inputSource.setEncoding("UTF-8");
            SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
            sp.parse(inputSource, this);
        }
    } catch (UnsupportedEncodingException e) {
        System.out.println("Common Parser Unsupported Encoding :: "+e);
    } catch (ParserConfigurationException e) {
        System.out.println("Parse Config error"+e);
    } catch (SAXException e) {
        System.out.println("Sax error "+e);
    } catch (IOException e) {
        System.out.println("IO Error "+e);
    }
}

logcat响应向我建议我使用8k缓冲区大小,但我不知道如何为BufferedInputStream提供更多大小。

2 个答案:

答案 0 :(得分:2)

您可以尝试从 InputStreamReader 更改为 BufferedReader 因为您已经在InputSource上设置了编码。 然后你可以将缓冲区的大小设置为8192(8k这是android建议的) 所以你的代码看起来像     ...

InputStream is = new ByteArrayInputStream(data.getBytes());
// use BufferedInputStream instead of InputStreamReader
BufferedInputStream bis = new BufferedInputStream(is, 8192);
InputSource inputSource = new InputSource(bis);
inputSource.setEncoding("UTF-8");
SAXParser sp = SAXParserFactory.newInstance().newSAXParser();
sp.parse(inputSource, this);

...

答案 1 :(得分:0)

您的代码没有错,消息不是错误。可以将其视为一个FYI,通知您可以指定缓冲区的大小而不是使用默认的8k。如果较小的尺寸可以,那么你可以节省一些全部

的存储空间

如果8KB恰好是您需要的,您可以选择忽略警告,或者您可以使用构造函数调整大小以根据您的需要进行调整 - 尽可能小,尽可能大。