Java InputStream Read Line readLineStringBuilder(InputStream is)

Here you can find the source of readLineStringBuilder(InputStream is)

Description

read Line String Builder

License

Open Source License

Declaration

private static String readLineStringBuilder(InputStream is) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.InputStream;

public class Main {
    private static String readLineStringBuilder(InputStream is) throws Exception {
        // FIXME con StringBuilder OutOfMemoryError sul NAS di Telecom
        String res = null;//w  w  w.j a va  2 s.  co  m
        StringBuilder sb = new StringBuilder(192); // most of the lines are shorter than 192 chars
        int temp = is.read();
        while (temp != 0x0D) {
            if (sb.capacity() >= 4000) {
                //            System.out.println("InternetUltil.readLineStringBuilder: sb.toString() " + sb.toString());
                //            System.out.println("InternetUltil.readLineStringBuilder: sb.length() " + sb.length() + " sb.capacity() " + sb.capacity());
                sb.setLength(0);
                sb = null;
                return null; // reset the sb to avoid the out of memory exception
            }
            sb.append((char) temp);
            temp = is.read();
        }
        is.read(); // skip (char)0x0A

        //      if(sb.length() > 1000){
        //         System.out.println("InternetUltil.readLineStringBuilder: sb.toString() " + sb.toString());
        //         System.out.println("InternetUltil.readLineStringBuilder: sb.length() " + sb.length() + " sb.capacity() " + sb.capacity());
        //      }
        res = sb.toString();
        sb = null;
        return res;
    }
}

Related

  1. readLines(InputStream stream, String charset)
  2. readLinesCommon(InputStream in)
  3. readLineSet(InputStream stream)
  4. readLinesStream(InputStream is, boolean trim)
  5. readLineString(InputStream is)
  6. readLineStringConcat(InputStream is)
  7. readLinesUntil(InputStream br, String ln)