View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util.io;
45  
46  import java.io.IOException;
47  import java.io.InterruptedIOException;
48  import java.io.OutputStream;
49  import java.util.concurrent.ArrayBlockingQueue;
50  import java.util.concurrent.Callable;
51  import java.util.concurrent.ExecutionException;
52  import java.util.concurrent.ExecutorService;
53  import java.util.concurrent.Future;
54  import java.util.concurrent.RejectedExecutionException;
55  import java.util.concurrent.ThreadFactory;
56  import java.util.concurrent.ThreadPoolExecutor;
57  import java.util.concurrent.TimeUnit;
58  import java.util.concurrent.TimeoutException;
59  import java.util.concurrent.atomic.AtomicInteger;
60  
61  import org.eclipse.jgit.internal.JGitText;
62  
63  /**
64   * OutputStream isolated from interrupts.
65   * <p>
66   * Wraps an OutputStream to prevent interrupts during writes from being made
67   * visible to that stream instance. This works around buggy or difficult
68   * OutputStream implementations like JSch that cannot gracefully handle an
69   * interrupt during write.
70   * <p>
71   * Every write (or flush) requires a context switch to another thread. Callers
72   * should wrap this stream with {@code BufferedOutputStream} using a suitable
73   * buffer size to amortize the cost of context switches.
74   *
75   * @since 4.6
76   */
77  public class IsolatedOutputStream extends OutputStream {
78  	private final OutputStream dst;
79  	private final ExecutorService copier;
80  	private Future<Void> pending;
81  
82  	/**
83  	 * Wraps an OutputStream.
84  	 *
85  	 * @param out
86  	 *            stream to send all writes to.
87  	 */
88  	public IsolatedOutputStream(OutputStream out) {
89  		dst = out;
90  		copier = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS,
91  				new ArrayBlockingQueue<Runnable>(1), new NamedThreadFactory());
92  	}
93  
94  	/** {@inheritDoc} */
95  	@Override
96  	public void write(int ch) throws IOException {
97  		write(new byte[] { (byte) ch }, 0, 1);
98  	}
99  
100 	/** {@inheritDoc} */
101 	@Override
102 	public void write(byte[] buf, int pos, int cnt)
103 			throws IOException {
104 		checkClosed();
105 		execute(new Callable<Void>() {
106 			@Override
107 			public Void call() throws IOException {
108 				dst.write(buf, pos, cnt);
109 				return null;
110 			}
111 		});
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public void flush() throws IOException {
117 		checkClosed();
118 		execute(new Callable<Void>() {
119 			@Override
120 			public Void call() throws IOException {
121 				dst.flush();
122 				return null;
123 			}
124 		});
125 	}
126 
127 	/** {@inheritDoc} */
128 	@Override
129 	public void close() throws IOException {
130 		if (!copier.isShutdown()) {
131 			try {
132 				if (pending == null || tryCleanClose()) {
133 					cleanClose();
134 				} else {
135 					dirtyClose();
136 				}
137 			} finally {
138 				copier.shutdown();
139 			}
140 		}
141 	}
142 
143 	private boolean tryCleanClose() {
144 		/*
145 		 * If the caller stopped waiting for a prior write or flush, they could
146 		 * be trying to close a stream that is still in-use. Check if the prior
147 		 * operation ended in a predictable way.
148 		 */
149 		try {
150 			pending.get(0, TimeUnit.MILLISECONDS);
151 			pending = null;
152 			return true;
153 		} catch (TimeoutException | InterruptedException e) {
154 			return false;
155 		} catch (ExecutionException e) {
156 			pending = null;
157 			return true;
158 		}
159 	}
160 
161 	private void cleanClose() throws IOException {
162 		execute(new Callable<Void>() {
163 			@Override
164 			public Void call() throws IOException {
165 				dst.close();
166 				return null;
167 			}
168 		});
169 	}
170 
171 	private void dirtyClose() throws IOException {
172 		/*
173 		 * Interrupt any still pending write or flush operation. This may cause
174 		 * massive failures inside of the stream, but its going to be closed as
175 		 * the next step.
176 		 */
177 		pending.cancel(true);
178 
179 		Future<Void> close;
180 		try {
181 			close = copier.submit(new Callable<Void>() {
182 				@Override
183 				public Void call() throws IOException {
184 					dst.close();
185 					return null;
186 				}
187 			});
188 		} catch (RejectedExecutionException e) {
189 			throw new IOException(e);
190 		}
191 		try {
192 			close.get(200, TimeUnit.MILLISECONDS);
193 		} catch (InterruptedException | TimeoutException e) {
194 			close.cancel(true);
195 			throw new IOException(e);
196 		} catch (ExecutionException e) {
197 			throw new IOException(e.getCause());
198 		}
199 	}
200 
201 	private void checkClosed() throws IOException {
202 		if (copier.isShutdown()) {
203 			throw new IOException(JGitText.get().closed);
204 		}
205 	}
206 
207 	private void execute(Callable<Void> task) throws IOException {
208 		if (pending != null) {
209 			// Check (and rethrow) any prior failed operation.
210 			checkedGet(pending);
211 		}
212 		try {
213 			pending = copier.submit(task);
214 		} catch (RejectedExecutionException e) {
215 			throw new IOException(e);
216 		}
217 		checkedGet(pending);
218 		pending = null;
219 	}
220 
221 	private static void checkedGet(Future<Void> future) throws IOException {
222 		try {
223 			future.get();
224 		} catch (InterruptedException e) {
225 			throw interrupted(e);
226 		} catch (ExecutionException e) {
227 			throw new IOException(e.getCause());
228 		}
229 	}
230 
231 	private static InterruptedIOException interrupted(InterruptedException c) {
232 		InterruptedIOException e = new InterruptedIOException();
233 		e.initCause(c);
234 		return e;
235 	}
236 
237 	private static class NamedThreadFactory implements ThreadFactory {
238 		private static final AtomicInteger cnt = new AtomicInteger();
239 
240 		@Override
241 		public Thread newThread(Runnable r) {
242 			int n = cnt.incrementAndGet();
243 			String name = IsolatedOutputStream.class.getSimpleName() + '-' + n;
244 			return new Thread(r, name);
245 		}
246 	}
247 }