View Javadoc
1   /*
2    * Copyright (C) 2016, Christian Halstrick <christian.halstrick@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.attributes;
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.OutputStream;
15  
16  /**
17   * An abstraction for JGit's builtin implementations for hooks and filters.
18   * Instead of spawning an external processes to start a filter/hook and to pump
19   * data from/to stdin/stdout these builtin commmands may be used. They are
20   * constructed by {@link org.eclipse.jgit.attributes.FilterCommandFactory}.
21   *
22   * @since 4.6
23   */
24  public abstract class FilterCommand {
25  	/**
26  	 * The {@link InputStream} this command should read from
27  	 */
28  	protected InputStream in;
29  
30  	/**
31  	 * The {@link OutputStream} this command should write to
32  	 */
33  	protected OutputStream out;
34  
35  	/**
36  	 * Constructor for FilterCommand
37  	 * <p>
38  	 * FilterCommand implementors are required to manage the in and out streams
39  	 * (close on success and/or exception).
40  	 *
41  	 * @param in
42  	 *            The {@link java.io.InputStream} this command should read from
43  	 * @param out
44  	 *            The {@link java.io.OutputStream} this command should write to
45  	 */
46  	public FilterCommand(InputStream in, OutputStream out) {
47  		this.in = in;
48  		this.out = out;
49  	}
50  
51  	/**
52  	 * Execute the command. The command is supposed to read data from
53  	 * {@link #in} and to write the result to {@link #out}. It returns the
54  	 * number of bytes it read from {@link #in}. It should be called in a loop
55  	 * until it returns -1 signaling that the {@link java.io.InputStream} is
56  	 * completely processed.
57  	 * <p>
58  	 * On successful completion (return -1) or on Exception, the streams
59  	 * {@link #in} and {@link #out} are closed by the implementation.
60  	 *
61  	 * @return the number of bytes read from the {@link java.io.InputStream} or
62  	 *         -1. -1 means that the {@link java.io.InputStream} is completely
63  	 *         processed.
64  	 * @throws java.io.IOException
65  	 *             when {@link java.io.IOException} occurred while reading from
66  	 *             {@link #in} or writing to {@link #out}
67  	 */
68  	public abstract int run() throws IOException;
69  }