aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/util/io/LineFlushingOutputStream.java
blob: a5a10cf16c3e05361ea9a2d19042f68e6ebff951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2014 Google Inc. All rights reserved.
//
// 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.devtools.build.lib.util.io;

import java.io.IOException;
import java.io.OutputStream;

/**
 * This stream maintains a buffer, which it flushes upon encountering bytes
 * that might be new line characters. This stream implements {@link #close()}
 * as {@link #flush()}.
 */
abstract class LineFlushingOutputStream extends OutputStream {

  static final int BUFFER_LENGTH = 8192;
  protected static byte NEWLINE = '\n';

  /**
   * The buffer containing the characters that have not been flushed yet.
   */
  protected final byte[] buffer = new byte[BUFFER_LENGTH];

  /**
   * The length of the buffer that's actually used.
   */
  protected int len = 0;

  @Override
  public synchronized void write(byte[] b, int off, int inlen)
      throws IOException {
    if (len == BUFFER_LENGTH) {
      flush();
    }
    int charsInLine = 0;
    while(inlen > charsInLine) {
      boolean sawNewline = (b[off + charsInLine] == NEWLINE);
      charsInLine++;
      if (sawNewline || len + charsInLine == BUFFER_LENGTH) {
        System.arraycopy(b, off, buffer, len, charsInLine);
        len += charsInLine;
        off += charsInLine;
        inlen -= charsInLine;
        flush();
        charsInLine = 0;
      }
    }
    System.arraycopy(b, off, buffer, len, charsInLine);
    len += charsInLine;
  }

  @Override
  public void write(int byteAsInt) throws IOException {
    byte b = (byte) byteAsInt; // make sure we work with bytes in comparisons
    write(new byte[] {b}, 0, 1);
  }

  /**
   * Close is implemented as {@link #flush()}. Client code must close the
   * underlying output stream itself in case that's desired.
   */
  @Override
  public synchronized void close() throws IOException {
    flush();
  }

  @Override
  public final synchronized void flush() throws IOException {
    flushingHook(); // The point of using a hook is to make it synchronized.
  }

  /**
   * The implementing class must define this method, which must at least flush
   * the bytes in {@code buffer[0] - buffer[len - 1]}, and reset {@code len=0}.
   *
   * Don't forget to synchronized the implementation of this method on whatever
   * underlying object it writes to!
   */
  protected abstract void flushingHook() throws IOException;

}