|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.gradle; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.OutputStream; |
| 24 | + |
| 25 | +/** |
| 26 | + * Writes data passed to this stream as log messages. |
| 27 | + * |
| 28 | + * The stream will be flushed whenever a newline is detected. |
| 29 | + * Allows setting an optional prefix before each line of output. |
| 30 | + */ |
| 31 | +public abstract class LoggingOutputStream extends OutputStream { |
| 32 | + /** The starting length of the buffer */ |
| 33 | + private static final int DEFAULT_BUFFER_LENGTH = 4096; |
| 34 | + |
| 35 | + /** The buffer of bytes sent to the stream */ |
| 36 | + private byte[] buffer = new byte[DEFAULT_BUFFER_LENGTH]; |
| 37 | + |
| 38 | + /** Offset of the start of unwritten data in the buffer */ |
| 39 | + private int start = 0; |
| 40 | + |
| 41 | + /** Offset of the end (semi-open) of unwritten data in the buffer */ |
| 42 | + private int end = 0; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void write(final int b) throws IOException { |
| 46 | + if (b == 0) return; |
| 47 | + if (b == '\n') { |
| 48 | + // always flush with newlines instead of adding to the buffer |
| 49 | + flush(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (end == buffer.length) { |
| 54 | + if (start != 0) { |
| 55 | + // first try shifting the used buffer back to the beginning to make space |
| 56 | + System.arraycopy(buffer, start, buffer, 0, end - start); |
| 57 | + } else { |
| 58 | + // otherwise extend the buffer |
| 59 | + final int newBufferLength = buffer.length + DEFAULT_BUFFER_LENGTH; |
| 60 | + final byte[] newBuffer = new byte[newBufferLength]; |
| 61 | + System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); |
| 62 | + buffer = newBuffer; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + buffer[end++] = (byte) b; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void flush() { |
| 71 | + if (end == start) return; |
| 72 | + logLine(new String(buffer, start, end - start)); |
| 73 | + start = end; |
| 74 | + } |
| 75 | + |
| 76 | + protected abstract void logLine(String line); |
| 77 | +} |
0 commit comments