All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.unix4j.unix.tail.TailCommand Maven / Gradle / Ivy
package org.unix4j.unix.tail;
import java.io.File;
import java.util.List;
import org.unix4j.command.AbstractCommand;
import org.unix4j.context.ExecutionContext;
import org.unix4j.io.FileInput;
import org.unix4j.io.Input;
import org.unix4j.line.Line;
import org.unix4j.line.SimpleLine;
import org.unix4j.processor.DefaultInputProcessor;
import org.unix4j.processor.InputProcessor;
import org.unix4j.processor.LineProcessor;
import org.unix4j.processor.MultipleInputLineProcessor;
import org.unix4j.processor.RedirectInputLineProcessor;
import org.unix4j.unix.Tail;
import org.unix4j.util.FileUtil;
/**
* Implementation of the {@link Tail tail} command.
*/
class TailCommand extends AbstractCommand {
public TailCommand(TailArguments arguments) {
super(Tail.NAME, arguments);
if (arguments.isCountSet() && arguments.getCount() < 0) {
throw new IllegalArgumentException("count cannot be negative: " + arguments);
}
}
@Override
public LineProcessor execute(ExecutionContext context, final LineProcessor output) {
final TailArguments args = getArguments(context);
//input from file(s)?
if (args.isFilesSet()) {
final List inputs = FileInput.multiple(args.getFiles());
return getFileInputProcessor(inputs, context, output, args);
} else if (args.isPathsSet()) {
final List files = FileUtil.expandFiles(context.getCurrentDirectory(), args.getPaths());
final List inputs = FileInput.multiple(files);
return getFileInputProcessor(inputs, context, output, args);
}
//read from standard input
return getStandardInputProcessor(context, output, args);
}
private AbstractTailProcessor getStandardInputProcessor(ExecutionContext context, LineProcessor output, TailArguments args) {
if (args.isChars()) {
if (args.isCountFromStart()) {
return new TailCharsFromStartProcessor(this, context, output);
} else {
return new TailCharsFromEndProcessor(this, context, output);
}
} else {
if (args.isCountFromStart()) {
return new TailLinesFromStartProcessor(this, context, output);
} else {
return new TailLinesFromEndProcessor(this, context, output);
}
}
}
private LineProcessor getFileInputProcessor(List inputs, final ExecutionContext context, final LineProcessor output, TailArguments args) {
final AbstractTailProcessor tailProcessor = getStandardInputProcessor(context, output, args);
if (inputs.size() <= 1 || args.isSuppressHeaders()) {
return new RedirectInputLineProcessor(inputs, tailProcessor);
} else {
//write header line per file
final InputProcessor inputProcessor = new DefaultInputProcessor() {
private boolean firstFile = true;
@Override
public void begin(Input input, LineProcessor standardInputProcessor) {
if (firstFile) {
firstFile = false;
} else {
output.processLine(Line.EMPTY_LINE);
}
final String fileInfo = input instanceof FileInput ? ((FileInput)input).getFileInfo(context.getCurrentDirectory()) : input.toString();
output.processLine(new SimpleLine("==> " + fileInfo + " <=="));
}
@Override
public void finish(Input input, LineProcessor output) {
super.finish(input, output);
tailProcessor.resetCountersAndFlush();
}
};
return new MultipleInputLineProcessor(inputs, inputProcessor, tailProcessor);
}
}
}