package com.anrisoftware.globalpom.exec.pipeoutputs;

import java.io.InputStream;
import java.io.OutputStream;

import javax.inject.Inject;

import org.apache.commons.lang3.builder.ToStringBuilder;

import com.anrisoftware.globalpom.exec.api.CommandOutput;
import com.google.inject.assistedinject.Assisted;

public class PipeCommandOutput implements CommandOutput {

    private static final int BUFFER_SIZE = 0;

    @Inject
    private PipeCommandOutputFactory outputFactory;

    private InputStream stream;

    private OutputStream output;

    /**
     * @see PipeCommandOutputFactory#create(OutputStream)
     */
    @Inject
    PipeCommandOutput(@Assisted OutputStream output) {
        this.output = output;
    }

    @Override
    public void setInput(InputStream stream) {
        this.stream = stream;
    }

    /**
     * Sets the stream that is filled with the piped to the output of the
     * command.
     * 
     * @param stream
     *            the {@link OutputStream}.
     */
    public void setPipe(OutputStream stream) {
        this.output = stream;
    }

    @Override
    public PipeCommandOutput clone() {
        PipeCommandOutput commandOutput = outputFactory.create(output);
        commandOutput.setInput(stream);
        return commandOutput;
    }

    @Override
    public CommandOutput call() throws Exception {
        byte[] buffer = new byte[BUFFER_SIZE];
        int b;
        while ((b = stream.read()) != -1) {
            output.write(b);
        }
        return this;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).toString();
    }
}

@@ -301,16 +301,162 @@
 isted;%0A%0A
+/**%0A * Pipes the read data from the output of the command to a stream.%0A * %0A * @author Erwin Mueller, erwin.mueller@deventm.org%0A * @since 1.11%0A */%0A
 public c
@@ -510,55 +510,8 @@
  %7B%0A%0A
-    private static final int BUFFER_SIZE = 0;%0A%0A
     
@@ -638,16 +638,45 @@
 utput;%0A%0A
+    private int bufferSize;%0A%0A
     /**%0A
@@ -834,24 +834,56 @@
 t = output;%0A
+        this.bufferSize = 1024;%0A
     %7D%0A%0A    @
@@ -1230,32 +1230,222 @@
  stream;%0A    %7D%0A%0A
+    /**%0A     * Sets the buffer size.%0A     * %0A     * @param size%0A     *            the size in bytes.%0A     */%0A    public void setBufferSize(int size) %7B%0A        this.bufferSize = size;%0A    %7D%0A%0A
     @Override%0A  
@@ -1732,54 +1732,73 @@
 yte%5B
-BUFFER_SIZE%5D;%0A        int b;%0A        while ((b
+bufferSize%5D;%0A        for (int s = stream.read(buffer); s != -1; s
  = s
@@ -1812,16 +1812,15 @@
 ead(
-)) != -1
+buffer)
 ) %7B%0A
@@ -1845,16 +1845,55 @@
 .write(b
+uffer, 0, s);%0A            output.flush(
 );%0A     

Set buffer for pipe output.