Java InputStream Read Line readLinesUntil(InputStream br, String ln)

Here you can find the source of readLinesUntil(InputStream br, String ln)

Description

read Lines Until

License

Apache License

Declaration

public static final String readLinesUntil(InputStream br, String ln)
        throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static final String readLinesUntil(InputStream br, String ln)
            throws Exception {
        return readLinesUntil(br, ln, null);
    }/*from  w w w.j a v a 2s.  com*/

    public static final String readLinesUntil(InputStream br, String ln,
            String charset) throws Exception {
        StringBuffer result = new StringBuffer();
        String s = readLine(br, charset);
        while ((s != null) && !s.equals(ln)) {
            result.append(s);
            s = readLine(br, charset);
            if ((s != null) && !s.equals(ln))
                result.append("\r\n");
        }
        return result.toString();
    }

    public static final String readLine(InputStream in) throws IOException {
        return readLine(in, null);
    }

    public static final String readLine(InputStream in, String charset)
            throws IOException {
        int rd = in.read();
        if (rd == -1)
            return null;
        byte r = (byte) rd;
        int i = 0;
        int l = 50;
        byte[] buf = new byte[l];
        while (r != '\n') {
            if (i >= l - 1) {
                l += 50;
                byte[] old = buf;
                buf = new byte[l];
                System.arraycopy(old, 0, buf, 0, old.length);
            }
            if (r != '\r')
                buf[i++] = r;
            rd = in.read();
            if (rd == -1)
                break;
            r = (byte) rd;
        }
        if (charset == null) {
            return new String(buf, 0, i);
        }
        return new String(buf, 0, i, charset);
    }
}

Related

  1. readLineSet(InputStream stream)
  2. readLinesStream(InputStream is, boolean trim)
  3. readLineString(InputStream is)
  4. readLineStringBuilder(InputStream is)
  5. readLineStringConcat(InputStream is)