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
   *
   * 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.
  */
 
 package com.google.common.io;
 
Package-protected abstract class that implements the line reading algorithm used by LineReader. Line separators are per java.io.BufferedReader: 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.

Author(s):
Chris Nokleberg
Since:
1.0
 
 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.

Parameters:
cbuf the character buffer to process
off the offset into the buffer
len the number of characters to process
Throws:
java.io.IOException if an I/O error occurs
See also:
finish()
 
   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')) {
         pos++;
       }
     }
 
     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')) {
               pos++;
             }
           }
           start = pos + 1;
           break;
 
         case '\n':
           .append(cbufstartpos - start);
           finishLine(true);
           start = pos + 1;
           break;
       }
     }
     .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:
java.io.IOException if an I/O error occurs
 
  protected void finish() throws IOException {
    if ( || .length() > 0) {
      finishLine(false);
    }
  }

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

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