下面列出了怎么用org.apache.commons.io.output.TeeOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
private static void setupConsoleLogging() {
boolean doConsoleRedirect = !Boolean.getBoolean( "HopUi.Console.Redirect.Disabled" );
if ( doConsoleRedirect ) {
try {
Path parent = Paths.get( Const.HOP_AUDIT_DIRECTORY );
Files.createDirectories( parent );
Files.deleteIfExists( Paths.get( parent.toString(), "hopui.log" ) );
Path path = Files.createFile( Paths.get( parent.toString(), "hopui.log" ) );
System.setProperty( "LOG_PATH", path.toString() );
final FileOutputStream fos = new FileOutputStream( path.toFile() );
System.setOut( new PrintStream( new TeeOutputStream( originalSystemOut, fos ) ) );
System.setErr( new PrintStream( new TeeOutputStream( originalSystemErr, fos ) ) );
HopLogStore.OriginalSystemOut = System.out;
HopLogStore.OriginalSystemErr = System.err;
} catch ( Throwable ignored ) {
// ignored
}
}
}
private List<String> listExtensions()
throws MavenInvocationException, IOException {
InvocationRequest request = new DefaultInvocationRequest();
request.setBatchMode(true);
request.setGoals(Collections.singletonList(
getPluginGroupId() + ":" + getPluginArtifactId() + ":" + getPluginVersion() + ":list-extensions"));
getEnv().forEach(request::addShellEnvironment);
File outputLog = new File(testDir, "output.log");
InvocationOutputHandler outputHandler = new PrintStreamHandler(
new PrintStream(new TeeOutputStream(System.out, Files.newOutputStream(outputLog.toPath())), true, "UTF-8"),
true);
invoker.setOutputHandler(outputHandler);
File invokerLog = new File(testDir, "invoker.log");
PrintStreamLogger logger = new PrintStreamLogger(new PrintStream(new FileOutputStream(invokerLog), false, "UTF-8"),
InvokerLogger.DEBUG);
invoker.setLogger(logger);
invoker.execute(request);
return Files.readAllLines(outputLog.toPath());
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
private TeeOutputStream tee = new TeeOutputStream(ResponseWrapper.super.getOutputStream(), bos);
@Override
public void write(int b) throws IOException {
tee.write(b);
}
};
}
/**
* Returns an OuputStream that writes to local webapp and to file inside <code>MCR.WCMS2.DataDir</code>.
*/
public static OutputStream getOutputStream(String path) throws IOException {
String cleanPath = path.startsWith("/") ? path.substring(1) : path;
File wcmsDataDir = getWCMSDataDir();
File webappBaseDir = getWebAppBaseDir();
File webappTarget = new File(webappBaseDir, cleanPath);
if (!webappTarget.toPath().startsWith(webappBaseDir.toPath())) {
throw new IOException(String.format(Locale.ROOT, "Cannot write %s outside the web application: %s",
webappTarget, webappBaseDir));
}
File wcmsDataDirTarget = new File(wcmsDataDir, cleanPath);
createDirectoryIfNeeded(webappTarget);
createDirectoryIfNeeded(wcmsDataDirTarget);
LOGGER.info(String.format(Locale.ROOT, "Writing content to %s and to %s.", webappTarget, wcmsDataDirTarget));
return new TeeOutputStream(new FileOutputStream(wcmsDataDirTarget), new FileOutputStream(webappTarget));
}
public AbstractCppcheckCommand(IConsole console, String[] defaultArguments,
long timeout, String binaryPath) {
this.binaryPath = binaryPath;
this.console = console;
this.defaultArguments = defaultArguments;
executor = new DefaultExecutor();
// all modes of operation returns 0 when no error occured,
executor.setExitValue(0);
watchdog = new ExecuteWatchdog(timeout);
executor.setWatchdog(watchdog);
out = new ByteArrayOutputStream();
err = new ByteArrayOutputStream();
processStdOut = new LineFilterOutputStream(new TeeOutputStream(out,
console.getConsoleOutputStream(false)), DEFAULT_CHARSET);
processStdErr = new LineFilterOutputStream(new TeeOutputStream(err,
console.getConsoleOutputStream(true)), DEFAULT_CHARSET);
}
private ProcReturn execCommandInContainer(String containerName, Node node, boolean quiet, String... cmd) throws Exception {
if (containerName != null && ! containerName.isEmpty()) {
decorator.setContainerName(containerName);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
Launcher launcher = decorator
.decorate(new DummyLauncher(new StreamTaskListener(new TeeOutputStream(out, System.out))), node);
Map<String, String> envs = new HashMap<>(100);
for (int i = 0; i < 50; i++) {
envs.put("aaaaaaaa" + i, "bbbbbbbb");
}
envs.put("workingDir1", "/home/jenkins/agent");
ContainerExecProc proc = (ContainerExecProc) launcher
.launch(launcher.new ProcStarter().pwd("/tmp").cmds(cmd).envs(envs).quiet(quiet));
// wait for proc to finish (shouldn't take long)
for (int i = 0; proc.isAlive() && i < 200; i++) {
Thread.sleep(100);
}
assertFalse("proc is alive", proc.isAlive());
int exitCode = proc.join();
return new ProcReturn(proc, exitCode, out.toString());
}
private FilePath downloadDocker(DumbSlave slave, FilePath toolDir, String version) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TeeOutputStream tee = new TeeOutputStream(baos, new PlainTextConsoleOutputStream(System.err));
TaskListener l = new StreamTaskListener(tee);
FilePath exe = toolDir.child(version+"/bin/docker");
// Download for first time:
assertEquals(exe.getRemote(), DockerTool.getExecutable(version, slave, l, null));
assertTrue(exe.exists());
assertThat(baos.toString(), containsString(Messages.DockerToolInstaller_downloading_docker_client_(version)));
// Next time we do not need to download:
baos.reset();
assertEquals(exe.getRemote(), DockerTool.getExecutable(version, slave, l, null));
assertTrue(exe.exists());
assertThat(baos.toString(), not(containsString(Messages.DockerToolInstaller_downloading_docker_client_(version))));
// Version check:
baos.reset();
assertEquals(0, slave.createLauncher(l).launch().cmds(exe.getRemote(), "version", "--format", "{{.Client.Version}}").quiet(true).stdout(tee).stderr(System.err).join());
if (!version.equals("latest")) {
assertEquals(version, baos.toString().trim());
}
return exe;
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
private TeeOutputStream tee = new TeeOutputStream(ResponseWrapper.super.getOutputStream(), bos);
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
@Override
public void write(int b) throws IOException {
tee.write(b);
}
};
}
public static void startPlatformLogging(Path fileName) {
defaultSysOut = System.out;
deafultSysErr = System.err;
try {
File file = fileName.toFile();
file.getParentFile().mkdirs();
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
TeeOutputStream bothStream = new TeeOutputStream(System.out, fos);
PrintStream ps = new PrintStream(bothStream);
System.setOut(ps);
System.setErr(ps);
} catch (Exception e) {
e.printStackTrace();
LOG.error("Failed to redirect to log file %s", fileName);
throw new GraphalyticsExecutionException("Failed to log the benchmark run. Benchmark run aborted.");
}
}
private void captureStdOut() {
try {
stdOutStream = new FileOutputStream(stdOutFile);
final TeeOutputStream myOut = new TeeOutputStream(System.out, stdOutStream);
final PrintStream ps = new PrintStream(myOut, true); // true - auto-flush after println
System.setOut(ps);
logger.info("Writing sysout to file: " + stdOutFile.getCanonicalPath());
} catch (final Exception e) {
logger.info("Failed to capture sysout.", e);
}
}
public GradleHandle start() {
if (execHandle != null) {
throw new IllegalStateException("you have already called start() on this handle");
}
AbstractExecHandleBuilder execBuilder = execHandleFactory.create();
execBuilder.setStandardOutput(new CloseShieldOutputStream(new TeeOutputStream(System.out, standardOutput)));
execBuilder.setErrorOutput(new CloseShieldOutputStream(new TeeOutputStream(System.err, errorOutput)));
execHandle = execBuilder.build();
execHandle.start();
return this;
}
public GradleHandle start() {
if (execHandle != null) {
throw new IllegalStateException("you have already called start() on this handle");
}
AbstractExecHandleBuilder execBuilder = execHandleFactory.create();
execBuilder.setStandardOutput(new CloseShieldOutputStream(new TeeOutputStream(System.out, standardOutput)));
execBuilder.setErrorOutput(new CloseShieldOutputStream(new TeeOutputStream(System.err, errorOutput)));
execHandle = execBuilder.build();
execHandle.start();
return this;
}
private void captureStdOut() {
try {
stdOutFile = new File(logDirectory, stdOutFilePath);
stdOutStream = new FileOutputStream(stdOutFile);
final TeeOutputStream myOut = new TeeOutputStream(System.out, stdOutStream);
final PrintStream ps = new PrintStream(myOut, true); // true - auto-flush after println
System.setOut(ps);
logger.info("Writing sysout to file: " + stdOutFile.getCanonicalPath());
} catch (final Exception e) {
logger.info("Failed to capture sysout.", e);
}
}
public GradleHandle start() {
if (execHandle != null) {
throw new IllegalStateException("you have already called start() on this handle");
}
AbstractExecHandleBuilder execBuilder = execHandleFactory.create();
execBuilder.setStandardOutput(new CloseShieldOutputStream(new TeeOutputStream(System.out, standardOutput)));
execBuilder.setErrorOutput(new CloseShieldOutputStream(new TeeOutputStream(System.err, errorOutput)));
execHandle = execBuilder.build();
execHandle.start();
return this;
}
public GradleHandle start() {
if (execHandle != null) {
throw new IllegalStateException("you have already called start() on this handle");
}
AbstractExecHandleBuilder execBuilder = execHandleFactory.create();
execBuilder.setStandardOutput(new CloseShieldOutputStream(new TeeOutputStream(System.out, standardOutput)));
execBuilder.setErrorOutput(new CloseShieldOutputStream(new TeeOutputStream(System.err, errorOutput)));
execHandle = execBuilder.build();
execHandle.start();
return this;
}
public static void main(final String[] args) throws IOException {
final int topN = 20;
final String baseDir = "/afs/inf.ed.ac.uk/user/j/jfowkes/Code/Sequences/";
// final String dataset = "jmlr";
// final String seqLabels = baseDir + "Datasets/JMLR/jmlr.lab";
final String dataset = "alice_punc";
final String seqLabels = baseDir + "Datasets/Alice/WithPunctuation/alice_punc.lab";
// Set up logging
final FileOutputStream outFile = new FileOutputStream(baseDir + dataset + "_exclusive.txt");
final TeeOutputStream out = new TeeOutputStream(System.out, outFile);
final PrintStream ps = new PrintStream(out);
System.setOut(ps);
final Map<Sequence, Double> ismSeqs = SequenceMiningCore
.readISMSequences(new File(baseDir + "Logs/" + dataset + ".log"));
final Map<Sequence, Double> sqsSeqs = StatisticalSequenceMining
.readSQSSequences(new File(baseDir + "SQS/" + dataset + ".txt"));
final Map<Sequence, Double> gokrimpSeqs = StatisticalSequenceMining
.readGoKrimpSequences(new File(baseDir + "GoKrimp/" + dataset + ".txt"));
final Set<Sequence> ISMnotSQSorGoKrimp = getExclusiveSequences(ismSeqs.keySet(), sqsSeqs.keySet(),
gokrimpSeqs.keySet());
final Set<Sequence> SQSnotISMorGoKrimp = getExclusiveSequences(sqsSeqs.keySet(), ismSeqs.keySet(),
gokrimpSeqs.keySet());
final Set<Sequence> GoKrimpnotISMorSQS = getExclusiveSequences(gokrimpSeqs.keySet(), ismSeqs.keySet(),
sqsSeqs.keySet());
final List<String> dict = FileUtils.readLines(new File(seqLabels));
// Print top ten
System.out.print("\n============= ISM not SQS/GoKrimp =============\n");
printTopExclusiveSequences(topN, ismSeqs, ISMnotSQSorGoKrimp, dict);
System.out.print("\n============= SQS not ISM/GoKrimp =============\n");
printTopExclusiveSequences(topN, sqsSeqs, SQSnotISMorGoKrimp, dict);
System.out.print("\n============= GoKrimp not ISM/SQS =============\n");
printTopExclusiveSequences(topN, gokrimpSeqs, GoKrimpnotISMorSQS, dict);
}
public static void main(final String[] args) throws IOException, ClassNotFoundException {
final String baseFolder = "/afs/inf.ed.ac.uk/user/j/jfowkes/Code/Sequences/";
// final File dbFile = new File(baseFolder + "Datasets/parallel",
// ".dat");
// generateParallelDataset(dbFile);
// Set up logging
final FileOutputStream outFile = new FileOutputStream(baseFolder + "PrecisionRecall/parallel_pr.txt");
final TeeOutputStream out = new TeeOutputStream(System.out, outFile);
final PrintStream ps = new PrintStream(out);
System.setOut(ps);
// Read SQS sequences
final File outSQS = new File(baseFolder + "SQS/parallel_partial.txt");
final Map<Sequence, Double> seqsSQS = StatisticalSequenceMining.readSQSSequences(outSQS);
// Read GoKrimp sequences
final File outGOKRIMP = new File(baseFolder + "GoKrimp/parallel.txt");
final Map<Sequence, Double> seqsGORKIMP = StatisticalSequenceMining.readGoKrimpSequences(outGOKRIMP);
// Read ISM sequences
final File outISM = new File(baseFolder + "Logs/parallel.log");
final Map<Sequence, Double> seqsISM = SequenceMining.readISMSequences(outISM);
// Precision-recall
precisionRecall(seqsSQS, "SQS");
precisionRecall(seqsGORKIMP, "GoKrimp");
precisionRecall(seqsISM, "ISM");
}
/**
* Return the dataset sample.
*
* @param parameters the export parameters
* @param dataSet the sample
* @param preparationId the id of the corresponding preparation
*
*/
private void executePipeline(ExportParameters parameters, OutputStream outputStream, TransformationCacheKey key,
String preparationId, String version, DataSet dataSet) {
final ExportFormat format = getFormat(parameters.getExportType());
// get the actions to apply (no preparation ==> dataset export ==> no actions)
final String actions = getActions(preparationId, version);
try (final TeeOutputStream tee =
new TeeOutputStream(outputStream, contentCache.put(key, ContentCache.TimeToLive.DEFAULT))) {
final Configuration.Builder configurationBuilder = Configuration
.builder() //
.args(parameters.getArguments()) //
.outFilter(rm -> filterService.build(parameters.getFilter(), rm)) //
.sourceType(parameters.getFrom())
.format(format.getName()) //
.actions(actions) //
.preparation(getPreparation(preparationId)) //
.stepId(version) //
.volume(SMALL) //
.output(tee) //
.limit(this.limit);
// no need for statistics if it's not JSON output
if (!Objects.equals(format.getName(), JSON)) {
configurationBuilder.globalStatistics(false);
}
final Configuration configuration = configurationBuilder.build();
factory.get(configuration).buildExecutable(dataSet, configuration).execute();
tee.flush();
} catch (IOException e1) { // NOSONAR
LOGGER.debug("evicting cache {}", key.getKey());
contentCache.evict(key);
throw new TDPException(TransformationErrorCodes.UNABLE_TO_TRANSFORM_DATASET, e1);
}
}
@SuppressWarnings("resource")
@Test
public void whenUsingTeeInputOutputStream_thenWriteto2OutputStreams() throws IOException {
final String str = "Hello World.";
ByteArrayInputStream inputStream = new ByteArrayInputStream(str.getBytes());
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
FilterOutputStream teeOutputStream = new TeeOutputStream(outputStream1, outputStream2);
new TeeInputStream(inputStream, teeOutputStream, true).read(new byte[str.length()]);
Assert.assertEquals(str, String.valueOf(outputStream1));
Assert.assertEquals(str, String.valueOf(outputStream2));
}
@Before
public final void captureStdout() {
originalStdout = System.out;
originalStderr = System.err;
capturedOutputBytes = new ByteArrayOutputStream();
System.setOut(new PrintStream(new TeeOutputStream(originalStdout, capturedOutputBytes)));
System.setErr(new PrintStream(new TeeOutputStream(originalStderr, capturedOutputBytes)));
}
public TeeServletOutputStream(OutputStream one, OutputStream two) {
targetStream = new TeeOutputStream(one, two);
}
public TeeServletOutputStream(OutputStream one, OutputStream two) {
targetStream = new TeeOutputStream(one, two);
}
public TeeServletOutputStream(OutputStream one, OutputStream two) {
targetStream = new TeeOutputStream(one, two);
}
DelegatedServletOutputStream(ServletOutputStream servletStream) {
this.servletStream = servletStream;
teeStream = new TeeOutputStream(servletStream, contentStream);
}
@Nonnull
public static StdStreams tee(@Nonnull StdStreams a, @Nonnull StdStreams b) {
final OutputStream out = new TeeOutputStream(a.out(), b.out());
final OutputStream err = new TeeOutputStream(a.err(), b.err());
return new Impl(out, err);
}
public TeeOutputContainer(OutputStream stdOut, OutputStream errOut) {
teeStdOut = new TeeOutputStream(stdOut, mergedStream);
teeErrOut = new TeeOutputStream(errOut, mergedStream);
}
@SuppressWarnings("rawtypes")
@Override
public OutputStream decorateLogger(Run build, final OutputStream logger) throws IOException, InterruptedException {
return new TeeOutputStream(logger, stream = append(f, stream));
}
public TeeServletOutputStream(OutputStream one, OutputStream two) {
targetStream = new TeeOutputStream(one, two);
}
public TeeServletOutputStream( OutputStream one, OutputStream two ) {
targetStream = new TeeOutputStream( one, two);
}
/** creates a capturing context which sees the output to this stream, without interrupting the original target */
public OutputCapturingContext captureTee() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintStream toRestore = setThreadLocalPrintStream(new TeeOutputStream(getDelegate(), out));
return new OutputCapturingContext(this, out, toRestore);
}