001package org.unix4j.processor;
002
003import org.unix4j.command.ExitValueException;
004import org.unix4j.io.Input;
005import org.unix4j.line.Line;
006
007/**
008 * A line processor for a single input
009 */
010public class InputLineProcessor implements LineProcessor {
011        private final InputProcessor processor;
012        private final LineProcessor output;
013    private final Input input;
014
015        public InputLineProcessor(Input input, InputProcessor processor, LineProcessor output) {
016                this.input = input;
017                this.processor = processor;
018                this.output = output;
019        }
020
021        @Override
022        public boolean processLine(Line line) {
023                return false;// we want no input, we have it already
024        }
025
026        @Override
027        public void finish() {
028        try {
029            processor.begin(input, output);
030            for (final Line line : input) {
031                if (!processor.processLine(input, line, output)) {
032                    break;// wants no more lines
033                }
034            }
035            processor.finish(input, output);
036            input.close();
037        } catch (ExitValueException e) {
038            e.setInput(input);
039            throw e;
040        }
041        }
042}