Coverage Summary for Class: LineBuffer (com.google.common.io)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| LineBuffer | 0% (0/1) | 0% (0/4) | 0% (0/28) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.io; 16 17 import com.google.common.annotations.GwtIncompatible; 18 import com.google.errorprone.annotations.CanIgnoreReturnValue; 19 import java.io.IOException; 20 21 /** 22 * Package-protected abstract class that implements the line reading algorithm used by {@link 23 * LineReader}. Line separators are per {@link java.io.BufferedReader}: line feed, carriage return, 24 * or carriage return followed immediately by a linefeed. 25 * 26 * <p>Subclasses must implement {@link #handleLine}, call {@link #add} to pass character data, and 27 * call {@link #finish} at the end of stream. 28 * 29 * @author Chris Nokleberg 30 * @since 1.0 31 */ 32 @GwtIncompatible 33 @ElementTypesAreNonnullByDefault 34 abstract class LineBuffer { 35 /** Holds partial line contents. */ 36 private StringBuilder line = new StringBuilder(); 37 /** Whether a line ending with a CR is pending processing. */ 38 private boolean sawReturn; 39 40 /** 41 * Process additional characters from the stream. When a line separator is found the contents of 42 * the line and the line separator itself are passed to the abstract {@link #handleLine} method. 43 * 44 * @param cbuf the character buffer to process 45 * @param off the offset into the buffer 46 * @param len the number of characters to process 47 * @throws IOException if an I/O error occurs 48 * @see #finish 49 */ 50 protected void add(char[] cbuf, int off, int len) throws IOException { 51 int pos = off; 52 if (sawReturn && len > 0) { 53 // Last call to add ended with a CR; we can handle the line now. 54 if (finishLine(cbuf[pos] == '\n')) { 55 pos++; 56 } 57 } 58 59 int start = pos; 60 for (int end = off + len; pos < end; pos++) { 61 switch (cbuf[pos]) { 62 case '\r': 63 line.append(cbuf, start, pos - start); 64 sawReturn = true; 65 if (pos + 1 < end) { 66 if (finishLine(cbuf[pos + 1] == '\n')) { 67 pos++; 68 } 69 } 70 start = pos + 1; 71 break; 72 73 case '\n': 74 line.append(cbuf, start, pos - start); 75 finishLine(true); 76 start = pos + 1; 77 break; 78 79 default: 80 // do nothing 81 } 82 } 83 line.append(cbuf, start, off + len - start); 84 } 85 86 /** Called when a line is complete. */ 87 @CanIgnoreReturnValue 88 private boolean finishLine(boolean sawNewline) throws IOException { 89 String separator = sawReturn ? (sawNewline ? "\r\n" : "\r") : (sawNewline ? "\n" : ""); 90 handleLine(line.toString(), separator); 91 line = new StringBuilder(); 92 sawReturn = false; 93 return sawNewline; 94 } 95 96 /** 97 * Subclasses must call this method after finishing character processing, in order to ensure that 98 * any unterminated line in the buffer is passed to {@link #handleLine}. 99 * 100 * @throws IOException if an I/O error occurs 101 */ 102 protected void finish() throws IOException { 103 if (sawReturn || line.length() > 0) { 104 finishLine(false); 105 } 106 } 107 108 /** 109 * Called for each line found in the character data passed to {@link #add}. 110 * 111 * @param line a line of text (possibly empty), without any line separators 112 * @param end the line separator; one of {@code "\r"}, {@code "\n"}, {@code "\r\n"}, or {@code ""} 113 * @throws IOException if an I/O error occurs 114 */ 115 protected abstract void handleLine(String line, String end) throws IOException; 116 }