Here you can find the source of readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)
Parameter | Description |
---|---|
in | input stream |
buff | buffer to put bytes, can be null |
acceptEOF | if true, EOF will be treated as a valid new line sequence (with or without CR) |
requireCR | require a carriage return character (0x0C) to be before the newline character |
public static int readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; public class Main { /**// ww w . j a v a 2 s . co m * Read and buffer bytes until end of line is detected (the newline sequence is not included).<br> * if {@code buff} is {@code null} no bytes are buffered but the end of line is still searched for.<br> * This method does not deal with decoding characters to avoid buffering unused bytes. * @param in input stream * @param buff buffer to put bytes, can be null * @param acceptEOF if true, EOF will be treated as a valid new line sequence (with or without CR) * @param requireCR require a carriage return character (0x0C) to be before the newline character * @return how many bytes were placed (or would have been placed) in buff */ public static int readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR) throws IOException { int count = 0, curr, cr = 0; while (true) { curr = in.read(); if (curr == -1) { if (acceptEOF) return count; throw new EOFException(); } if (curr == 10) { if (!requireCR || cr == 1) return count; count++; if (buff != null) buff.put((byte) 10); } else if (curr == 13) { if (cr == 0) cr = 1; else { count++; if (buff != null) buff.put((byte) 13); } } else { count++; if (cr == 1) count++; if (buff != null) { if (cr == 1) buff.put((byte) 13); buff.put((byte) curr); } } } } }