开发者

java: speed up reading foreign characters

开发者 https://www.devze.com 2022-12-26 01:50 出处:网络
My current code needs to read foreign characters from the web, currently my solution works but it is very slow, since it read char by char using InputStreamReader. Is there anyway to speed it up and a

My current code needs to read foreign characters from the web, currently my solution works but it is very slow, since it read char by char using InputStreamReader. Is there anyway to speed it up and also get the job done?

// Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.get开发者_StackOverflow社区Content();

        StringBuilder contents = new StringBuilder();
        int ch;
        InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
          // FileInputStream file = new InputStream(is);
         while( (ch = isr.read()) != -1)
             contents.append((char)ch);
         String encode = isr.getEncoding();

         return contents.toString();


Wrap your InputStreamReader with a BufferedReader

for the efficient reading of characters, arrays, and lines.

InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
Reader reader = new BufferedReader(isr);
0

精彩评论

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