View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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.InputStream;
48  import java.io.InterruptedIOException;
49  import java.io.OutputStream;
50  
51  /** Thread to copy from an input stream to an output stream. */
52  public class StreamCopyThread extends Thread {
53  	private static final int BUFFER_SIZE = 1024;
54  
55  	private final InputStream src;
56  
57  	private final OutputStream dst;
58  
59  	private volatile boolean done;
60  
61  	/** Lock held by flush to avoid interrupting a write. */
62  	private final Object writeLock;
63  
64  	/**
65  	 * Create a thread to copy data from an input stream to an output stream.
66  	 *
67  	 * @param i
68  	 *            stream to copy from. The thread terminates when this stream
69  	 *            reaches EOF. The thread closes this stream before it exits.
70  	 * @param o
71  	 *            stream to copy into. The destination stream is automatically
72  	 *            closed when the thread terminates.
73  	 */
74  	public StreamCopyThread(final InputStream i, final OutputStream o) {
75  		setName(Thread.currentThread().getName() + "-StreamCopy"); //$NON-NLS-1$
76  		src = i;
77  		dst = o;
78  		writeLock = new Object();
79  	}
80  
81  	/**
82  	 * Request the thread to flush the output stream as soon as possible.
83  	 * <p>
84  	 * This is an asynchronous request to the thread. The actual flush will
85  	 * happen at some future point in time, when the thread wakes up to process
86  	 * the request.
87  	 */
88  	@Deprecated
89  	public void flush() {
90  		synchronized (writeLock) {
91  			interrupt();
92  		}
93  	}
94  
95  	/**
96  	 * Request that the thread terminate, and wait for it.
97  	 * <p>
98  	 * This method signals to the copy thread that it should stop as soon as
99  	 * there is no more IO occurring.
100 	 *
101 	 * @throws InterruptedException
102 	 *             the calling thread was interrupted.
103 	 */
104 	public void halt() throws InterruptedException {
105 		for (;;) {
106 			join(250 /* milliseconds */);
107 			if (isAlive()) {
108 				done = true;
109 				interrupt();
110 			} else
111 				break;
112 		}
113 	}
114 
115 	@Override
116 	public void run() {
117 		try {
118 			final byte[] buf = new byte[BUFFER_SIZE];
119 			boolean readInterrupted = false;
120 			for (;;) {
121 				try {
122 					if (readInterrupted) {
123 						synchronized (writeLock) {
124 							boolean interruptedAgain = Thread.interrupted();
125 							dst.flush();
126 							if (interruptedAgain) {
127 								interrupt();
128 							}
129 						}
130 						readInterrupted = false;
131 					}
132 
133 					if (done)
134 						break;
135 
136 					final int n;
137 					try {
138 						n = src.read(buf);
139 					} catch (InterruptedIOException wakey) {
140 						readInterrupted = true;
141 						continue;
142 					}
143 					if (n < 0)
144 						break;
145 
146 					synchronized (writeLock) {
147 						boolean writeInterrupted = Thread.interrupted();
148 						dst.write(buf, 0, n);
149 						if (writeInterrupted) {
150 							interrupt();
151 						}
152 					}
153 				} catch (IOException e) {
154 					break;
155 				}
156 			}
157 		} finally {
158 			try {
159 				src.close();
160 			} catch (IOException e) {
161 				// Ignore IO errors on close
162 			}
163 			try {
164 				dst.close();
165 			} catch (IOException e) {
166 				// Ignore IO errors on close
167 			}
168 		}
169 	}
170 }