org.jenkinsci.plugins.mber.LoggingOutputStream.java Source code

Java tutorial

Introduction

Here is the source code for org.jenkinsci.plugins.mber.LoggingOutputStream.java

Source

/*
The Jenkins Mber Plugin is free software distributed under the terms of the MIT
license (http://opensource.org/licenses/mit-license.html) reproduced here:
    
Copyright (c) 2013-2015 Mber
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package org.jenkinsci.plugins.mber;

import java.io.InterruptedIOException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.io.IOUtils;

public class LoggingOutputStream extends OutputStream {
    public interface Listener {
        void logPercentComplete(final int percent);
    }

    private final OutputStream output;
    private final Listener listener;
    private final Timer timer = new Timer();
    private final long expectedBytes;
    private long bytesWritten;

    public LoggingOutputStream(OutputStream output, Listener listener, long expectedBytes) {
        this.output = output;
        this.listener = listener;
        this.expectedBytes = expectedBytes;
        this.bytesWritten = 0;
        initTimer();
    }

    @Override
    public void close() throws IOException {
        this.timer.cancel();
        this.output.close();
        if (this.listener != null) {
            int percent = Math.round(this.bytesWritten * 100 / this.expectedBytes);
            this.listener.logPercentComplete(percent);
        }
    }

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

    @Override
    public void write(byte[] b) throws IOException {
        log(b.length);
        this.output.write(b);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        log(len);
        this.output.write(b, off, len);
    }

    @Override
    public void write(int b) throws IOException {
        log(1);
        this.output.write(b);
    }

    private void log(int length) throws IOException {
        // Users may arbitrarily stop a job while files are being transfered.
        // So we need to peridically check if the job's been canceled.
        if (Thread.interrupted()) {
            IOUtils.closeQuietly(this);
            throw new LoggingInterruptedException("Build was canceled.");
        }
        this.bytesWritten += length;
    }

    private void initTimer() {
        long timeout = 1 * 60 * 1000;
        this.timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (listener != null) {
                    int percent = Math.round(bytesWritten * 100 / expectedBytes);
                    listener.logPercentComplete(percent);
                }
            }
        }, timeout, timeout);
    }
}

// Thrown when the write thread's been interrupted.
// This extends InterruptedIOException so the Apache HTTP client won't consider
// it a network failure and attempt a retry.
class LoggingInterruptedException extends InterruptedIOException {
    public LoggingInterruptedException(final String message) {
        super(message);
    }
}