1/*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.io;
18
19import java.io.IOException;
20
21/**
22 * Package-protected abstract class that implements the line reading
23 * algorithm used by {@link LineReader}. Line separators are per {@link
24 * java.io.BufferedReader}: line feed, carriage return, or carriage
25 * return followed immediately by a linefeed.
26 *
27 * <p>Subclasses must implement {@link #handleLine}, call {@link #add}
28 * to pass character data, and call {@link #finish} at the end of stream.
29 *
30 * @author Chris Nokleberg
31 * @since 1.0
32 */
33abstract class LineBuffer {
34  /** Holds partial line contents. */
35  private StringBuilder line = new StringBuilder();
36  /** Whether a line ending with a CR is pending processing. */
37  private boolean sawReturn;
38
39  /**
40   * Process additional characters from the stream. When a line separator
41   * is found the contents of the line and the line separator itself
42   * 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    }
80    line.append(cbuf, start, off + len - start);
81  }
82
83  /** Called when a line is complete. */
84  private boolean finishLine(boolean sawNewline) throws IOException {
85    handleLine(line.toString(), sawReturn
86        ? (sawNewline ? "\r\n" : "\r")
87        : (sawNewline ? "\n" : ""));
88    line = new StringBuilder();
89    sawReturn = false;
90    return sawNewline;
91  }
92
93  /**
94   * Subclasses must call this method after finishing character processing,
95   * in order to ensure that any unterminated line in the buffer is
96   * passed to {@link #handleLine}.
97   *
98   * @throws IOException if an I/O error occurs
99   */
100  protected void finish() throws IOException {
101    if (sawReturn || line.length() > 0) {
102      finishLine(false);
103    }
104  }
105
106  /**
107   * Called for each line found in the character data passed to
108   * {@link #add}.
109   *
110   * @param line a line of text (possibly empty), without any line separators
111   * @param end the line separator; one of {@code "\r"}, {@code "\n"},
112   *     {@code "\r\n"}, or {@code ""}
113   * @throws IOException if an I/O error occurs
114   */
115  protected abstract void handleLine(String line, String end)
116      throws IOException;
117}
118