Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced LineTransformationOutputStream.Delegating #3959

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @since 1.349
*/
public class ConsoleAnnotationOutputStream<T> extends LineTransformationOutputStream {
private final Writer out;
private final Writer out; // not an OutputStream so cannot use LineTransformationOutputStream.Delegating
private final T context;
private ConsoleAnnotator<T> ann;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Filtering {@link OutputStream} that buffers text by line, so that the derived class
* can perform some manipulation based on the contents of the whole line.
*
* TODO: Mac is supposed to be CR-only. This class needs to handle that.
* <p>Subclass {@link Delegating} in the typical case that you are decorating an underlying stream.
*
* @author Kohsuke Kawaguchi
* @since 1.349
Expand Down Expand Up @@ -110,4 +110,32 @@ protected String trimEOL(String line) {
}

private static final int LF = 0x0A;

/**
* Convenience subclass for cases where you wish to process lines being sent to an underlying stream.
* {@link #eol} will typically {@link OutputStream#write(byte[], int, int)} to {@link #out}.
* Flushing or closing the decorated stream will behave properly.
* @since FIXME
*/
public static abstract class Delegating extends LineTransformationOutputStream {

protected final OutputStream out;

protected Delegating(OutputStream out) {
this.out = out;
}

@Override
public void flush() throws IOException {
out.flush();
}

@Override
public void close() throws IOException {
super.close();
out.close();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Logger;

/**
* Filters out console notes.
*
* @author Kohsuke Kawaguchi
*/
public class PlainTextConsoleOutputStream extends LineTransformationOutputStream {
private final OutputStream out;
public class PlainTextConsoleOutputStream extends LineTransformationOutputStream.Delegating {

/**
*
*/
public PlainTextConsoleOutputStream(OutputStream out) {
this.out = out;
super(out);
}

/**
Expand Down Expand Up @@ -77,17 +75,4 @@ protected void eol(byte[] in, int sz) throws IOException {
out.write(in,written,sz-written);
}

@Override
public void flush() throws IOException {
out.flush();
}

@Override
public void close() throws IOException {
super.close();
out.close();
}


private static final Logger LOGGER = Logger.getLogger(PlainTextConsoleOutputStream.class.getName());
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
*
* @author Kohsuke Kawaguchi
*/
public class MavenConsoleAnnotator extends LineTransformationOutputStream {
private final OutputStream out;
public class MavenConsoleAnnotator extends LineTransformationOutputStream.Delegating {
private final Charset charset;

public MavenConsoleAnnotator(OutputStream out, Charset charset) {
this.out = out;
super(out);
this.charset = charset;
}

Expand Down Expand Up @@ -75,9 +74,4 @@ protected void eol(byte[] b, int len) throws IOException {
out.write(b,0,len);
}

@Override
public void close() throws IOException {
super.close();
out.close();
}
}
4 changes: 2 additions & 2 deletions test/src/test/java/hudson/console/ConsoleLogFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
}

@Override
public OutputStream decorateLogger(final Computer c, final OutputStream out) throws IOException, InterruptedException {
return new LineTransformationOutputStream() {
public OutputStream decorateLogger(final Computer c, OutputStream out) throws IOException, InterruptedException {
return new LineTransformationOutputStream.Delegating(out) {
@Override
protected void eol(byte[] b, int len) throws IOException {
out.write(("[["+c.getName()+"]] ").getBytes());
Expand Down
6 changes: 3 additions & 3 deletions test/src/test/java/jenkins/tasks/SimpleBuildWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public static class WrapperWithLogger extends SimpleBuildWrapper {
private static class UpcaseFilter extends ConsoleLogFilter implements Serializable {
private static final long serialVersionUID = 1;
@SuppressWarnings("rawtypes") // inherited
@Override public OutputStream decorateLogger(AbstractBuild _ignore, final OutputStream logger) throws IOException, InterruptedException {
return new LineTransformationOutputStream() {
@Override public OutputStream decorateLogger(AbstractBuild _ignore, OutputStream logger) throws IOException, InterruptedException {
return new LineTransformationOutputStream.Delegating(logger) {
@Override protected void eol(byte[] b, int len) throws IOException {
logger.write(new String(b, 0, len).toUpperCase(Locale.ROOT).getBytes());
out.write(new String(b, 0, len).toUpperCase(Locale.ROOT).getBytes());
}
};
}
Expand Down