Java ByteBuffer Put readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)

Here you can find the source of readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)

Description

Read and buffer bytes until end of line is detected (the newline sequence is not included).
if buff is null no bytes are buffered but the end of line is still searched for.
This method does not deal with decoding characters to avoid buffering unused bytes.

License

Open Source License

Parameter

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

Return

how many bytes were placed (or would have been placed) in buff

Declaration

public static int readNextLine(InputStream in, ByteBuffer buff, boolean acceptEOF, boolean requireCR)
        throws IOException 

Method Source Code


//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);
                }
            }
        }
    }
}

Related

  1. readByteVector8(ByteBuffer input)
  2. readFully(InputStream in, ByteBuffer out)
  3. readFully(InputStream is, ByteBuffer buf)
  4. readFully(InputStream is, ByteBuffer buffer, int nrBytes)
  5. readInto(ByteBuffer buf, InputStream inputStream)
  6. readPackedLong(ByteBuffer input)
  7. readToByteBuffer(InputStream inStream)
  8. serializeNullableString(ByteBuffer outputBuffer, String value)
  9. serializeObject(Object obj, ByteBuffer buffer, ByteArrayOutputStream baos, boolean markEndOfHeader)