【问题标题】:Logcat message: Default buffer size used in BufferedInputStream constructor.Logcat 消息:BufferedInputStream 构造函数中使用的默认缓冲区大小。
【发布时间】:2011-05-12 07:28:07
【问题描述】:

在执行我的项目时,我在 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 提供更多大小。

【问题讨论】:

标签: java android bufferedinputstream


【解决方案1】:

您可以尝试从 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);

...

【讨论】:

    【解决方案2】:

    您的代码没有任何问题,消息不是错误。将其视为一个 FYI,通知您可以指定缓冲区的大小,而不是使用默认的 8k。如果较小的大小就可以了,那么您可以节省一些内存空间

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

    【讨论】:

      猜你喜欢
      • 2012-04-05
      • 2013-11-02
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      相关资源
      最近更新 更多