Java InputStream Read Line readLineString(InputStream is)

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

Description

Read the stream until a CRLF (13-10)

License

Open Source License

Declaration

public static String readLineString(InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/*/*from  ww w .ja v a2 s  .c o  m*/
Copyright 2005-2015, Olivier PETRUCCI.
    
This file is part of Areca.
    
Areca is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
Areca is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Areca; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
*/

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

public class Main {
    /**
     * Read the stream until a CRLF (13-10)
     */
    public static String readLineString(InputStream is) throws IOException {
        String ret = "";

        while (true) {
            byte[] tmp = readLineBA(is, 4 * 1024);
            if (tmp == null) {
                break;
            }
            ret += new String(tmp);

            if (tmp[tmp.length - 1] == 10 && tmp[tmp.length - 2] == 13) {
                break;
            }
        }

        return ret;
    }

    /**
     * Read the stream until a CRLF (13-10) is reached OR the buffer's limit is reached.
     * <BR>The actual number of bytes read (or -1 if the end of stream has been reached) is returned.
     */
    public static byte[] readLineBA(InputStream is, int size) throws IOException {
        byte[] tmp = new byte[size];
        int nb = readLine(is, tmp);
        if (nb == -1) {
            return null;
        } else {
            if (nb == size) {
                return tmp;
            } else {
                byte[] nArray = new byte[nb];
                for (int i = 0; i < nb; i++) {
                    nArray[i] = tmp[i];
                }
                return nArray;
            }
        }
    }

    /**
     * Read the stream until a CRLF (13-10) is reached OR the buffer's limit is reached.
     * <BR>The actual number of bytes read (or -1 if the end of stream has been reached) is returned.
     */
    public static int readLine(InputStream is, byte[] buff) throws IOException {
        byte b = 0, pb;
        int v;
        int offset = 0;
        while (true) {
            pb = b;
            v = is.read();
            if (v == -1) {
                if (offset == 0) {
                    // End of stream reached
                    return -1;
                } else {
                    // End of stream reached
                    return offset;
                }
            } else {
                b = (byte) v;
                buff[offset++] = b;

                // CRLF or buffer limit reached
                if ((pb == 13 && b == 10) || offset == buff.length) {
                    return offset;
                }
            }
        }
    }
}

Related

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