Java ByteArrayOutputStream Read Line readLine(InputStream in)

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

Description

A simple method to read a complete line from an InputStream.

License

Apache License

Declaration

public static String readLine(InputStream in) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright 2014 WS/Tech? Informatica LTDA.
 * /* ww w.  j av a  2  s  . c  o m*/
 * MHC (ME HTTPS Client) - An alternative J2ME Https Client.
 * 
 * http://www.wstech2.net/mhc/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {
    final static char CR = '\r';
    final static char LF = '\n';

    /**
     * A simple method to read a complete line from an InputStream.
     */
    public static String readLine(InputStream in) throws IOException {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        int c = 0;
        while ((c = in.read()) != -1 && c != LF) {
            if (c != CR) // ignore CR
            {
                bytes.write(c);
            }
        }
        return new String(bytes.toByteArray());
    }
}

Related

  1. readLine()
  2. readLine(final InputStream in)
  3. readLine(InputStream in)
  4. readLine(InputStream in)
  5. readLine(InputStream in)
  6. readLine(InputStream in)
  7. readLine(InputStream in)