001package org.unix4j.unix.head;
002
003import org.unix4j.context.ExecutionContext;
004import org.unix4j.line.Line;
005import org.unix4j.line.SimpleLine;
006import org.unix4j.processor.LineProcessor;
007
008final class HeadCharsProcessor extends AbstractHeadProcessor {
009        HeadCharsProcessor(HeadCommand command, ExecutionContext context, LineProcessor output) {
010                super(command, context, output);
011        }
012
013        @Override
014        public boolean processLine(Line line) {
015                final long before = counter.getCount();
016                if (before < count) {
017                        final long after = counter.increment(line.length());
018                        final boolean more;
019                        if (after < count) {
020                                 more = getOutput().processLine(line);
021                        } else {
022                                final int len = (int)(count-before);
023                                final Line cutLine = SimpleLine.subLine(line, 0, len, false);
024                                more = getOutput().processLine(cutLine);
025                        }
026                        return after < count && more;
027                } else {
028                        return false;
029                }
030        }
031}