Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2007 The Guava Authors
   * 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
  * 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.
Package-protected abstract class that implements the line reading algorithm used by LineReader. Line separators are per line feed, carriage return, or carriage return followed immediately by a linefeed.

Subclasses must implement handleLine(java.lang.String,java.lang.String), call add(char[],int,int) to pass character data, and call finish() at the end of stream.

Chris Nokleberg
 abstract class LineBuffer {
Holds partial line contents.
   private StringBuilder line = new StringBuilder();
Whether a line ending with a CR is pending processing.
   private boolean sawReturn;

Process additional characters from the stream. When a line separator is found the contents of the line and the line separator itself are passed to the abstract handleLine(java.lang.String,java.lang.String) method.

cbuf the character buffer to process
off the offset into the buffer
len the number of characters to process
Throws: if an I/O error occurs
See also:
   protected void add(char[] cbufint offint lenthrows IOException {
     int pos = off;
     if ( && len > 0) {
       // Last call to add ended with a CR; we can handle the line now.
       if (finishLine(cbuf[pos] == '\n')) {
     int start = pos;
     for (int end = off + lenpos < endpos++) {
       switch (cbuf[pos]) {
         case '\r':
           .append(cbufstartpos - start);
            = true;
           if (pos + 1 < end) {
             if (finishLine(cbuf[pos + 1] == '\n')) {
           start = pos + 1;
         case '\n':
           .append(cbufstartpos - start);
           start = pos + 1;
           // do nothing
     .append(cbufstartoff + len - start);

Called when a line is complete.
   private boolean finishLine(boolean sawNewlinethrows IOException {
         ? (sawNewline ? "\r\n" : "\r")
         : (sawNewline ? "\n" : ""));
      = new StringBuilder();
      = false;
     return sawNewline;

Subclasses must call this method after finishing character processing, in order to ensure that any unterminated line in the buffer is passed to handleLine(java.lang.String,java.lang.String).

Throws: if an I/O error occurs
  protected void finish() throws IOException {
    if ( || .length() > 0) {

Called for each line found in the character data passed to add(char[],int,int).

line a line of text (possibly empty), without any line separators
end the line separator; one of "\r", "\n", "\r\n", or ""
Throws: if an I/O error occurs
  protected abstract void handleLine(String lineString end)
      throws IOException;
New to GrepCode? Check out our FAQ X