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  	@Override
95  	public void write(int ch) throws IOException {
96  		write(new byte[] { (byte) ch }, 0, 1);
97  	}
98  
99  	@Override
100 	public void write(final byte[] buf, final int pos, final int cnt)
101 			throws IOException {
102 		checkClosed();
103 		execute(new Callable<Void>() {
104 			@Override
105 			public Void call() throws IOException {
106 				dst.write(buf, pos, cnt);
107 				return null;
108 			}
109 		});
110 	}
111 
112 	@Override
113 	public void flush() throws IOException {
114 		checkClosed();
115 		execute(new Callable<Void>() {
116 			@Override
117 			public Void call() throws IOException {
118 				dst.flush();
119 				return null;
120 			}
121 		});
122 	}
123 
124 	@Override
125 	public void close() throws IOException {
126 		if (!copier.isShutdown()) {
127 			try {
128 				if (pending == null || tryCleanClose()) {
129 					cleanClose();
130 				} else {
131 					dirtyClose();
132 				}
133 			} finally {
134 				copier.shutdown();
135 			}
136 		}
137 	}
138 
139 	private boolean tryCleanClose() {
140 		/*
141 		 * If the caller stopped waiting for a prior write or flush, they could
142 		 * be trying to close a stream that is still in-use. Check if the prior
143 		 * operation ended in a predictable way.
144 		 */
145 		try {
146 			pending.get(0, TimeUnit.MILLISECONDS);
147 			pending = null;
148 			return true;
149 		} catch (TimeoutException | InterruptedException e) {
150 			return false;
151 		} catch (ExecutionException e) {
152 			pending = null;
153 			return true;
154 		}
155 	}
156 
157 	private void cleanClose() throws IOException {
158 		execute(new Callable<Void>() {
159 			@Override
160 			public Void call() throws IOException {
161 				dst.close();
162 				return null;
163 			}
164 		});
165 	}
166 
167 	private void dirtyClose() throws IOException {
168 		/*
169 		 * Interrupt any still pending write or flush operation. This may cause
170 		 * massive failures inside of the stream, but its going to be closed as
171 		 * the next step.
172 		 */
173 		pending.cancel(true);
174 
175 		Future<Void> close;
176 		try {
177 			close = copier.submit(new Callable<Void>() {
178 				@Override
179 				public Void call() throws IOException {
180 					dst.close();
181 					return null;
182 				}
183 			});
184 		} catch (RejectedExecutionException e) {
185 			throw new IOException(e);
186 		}
187 		try {
188 			close.get(200, TimeUnit.MILLISECONDS);
189 		} catch (InterruptedException | TimeoutException e) {
190 			close.cancel(true);
191 			throw new IOException(e);
192 		} catch (ExecutionException e) {
193 			throw new IOException(e.getCause());
194 		}
195 	}
196 
197 	private void checkClosed() throws IOException {
198 		if (copier.isShutdown()) {
199 			throw new IOException(JGitText.get().closed);
200 		}
201 	}
202 
203 	private void execute(Callable<Void> task) throws IOException {
204 		if (pending != null) {
205 			// Check (and rethrow) any prior failed operation.
206 			checkedGet(pending);
207 		}
208 		try {
209 			pending = copier.submit(task);
210 		} catch (RejectedExecutionException e) {
211 			throw new IOException(e);
212 		}
213 		checkedGet(pending);
214 		pending = null;
215 	}
216 
217 	private static void checkedGet(Future<Void> future) throws IOException {
218 		try {
219 			future.get();
220 		} catch (InterruptedException e) {
221 			throw interrupted(e);
222 		} catch (ExecutionException e) {
223 			throw new IOException(e.getCause());
224 		}
225 	}
226 
227 	private static InterruptedIOException interrupted(InterruptedException c) {
228 		InterruptedIOException e = new InterruptedIOException();
229 		e.initCause(c);
230 		return e;
231 	}
232 
233 	private static class NamedThreadFactory implements ThreadFactory {
234 		private static final AtomicInteger cnt = new AtomicInteger();
235 
236 		@Override
237 		public Thread newThread(Runnable r) {
238 			int n = cnt.incrementAndGet();
239 			String name = IsolatedOutputStream.class.getSimpleName() + '-' + n;
240 			return new Thread(r, name);
241 		}
242 	}
243 }