Java ByteArrayOutputStream Read Line readLine(InputStream is)

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

Description

read Line

License

Artistic License

Declaration

public static String readLine(InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w w w.  ja  v a 2  s  .  c  o  m*/
 * Copyright (C) 2011 Inqwell Ltd
 *
 * You may distribute under the terms of the Artistic License, as specified in
 * the README file.
 */

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

public class Main {
    public static String readLine(InputStream is) throws IOException {
        int byteRead;
        int offset = 0;
        int priorByte = -1;
        String ret = null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream(80);

        while (true) {
            byteRead = is.read();

            if (byteRead < 0) {
                bos.reset();
                bos.close();
                throw new IOException("readline: Premature EOF");
            }

            //if (byteRead == '\n' && priorByte == '\r')
            if (byteRead == '\n') {
                ret = bos.toString();
                bos.reset();
                bos.close();
                return ret;
            }

            if (byteRead != '\r')
                bos.write(byteRead);

            priorByte = byteRead;
        }
    }
}

Related

  1. readLine(InputStream input)
  2. readLine(InputStream inputStream)
  3. readLine(InputStream inputStream)
  4. readLine(InputStream inputstream)
  5. readLine(InputStream inputStream)
  6. readLine(Reader ir)