| 1 |
/* |
| 2 |
* Copyright (c) 2013 Jean Niklas L'orange. All rights reserved. |
| 3 |
* |
| 4 |
* The use and distribution terms for this software are covered by the |
| 5 |
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) |
| 6 |
* which can be found in the file LICENSE at the root of this distribution. |
| 7 |
* |
| 8 |
* By using this software in any fashion, you are agreeing to be bound by |
| 9 |
* the terms of this license. |
| 10 |
* |
| 11 |
* You must not remove this notice, or any other, from this software. |
| 12 |
*/ |
| 13 |
|
| 14 |
package com.hypirion.io; |
| 15 |
|
| 16 |
import java.io.InputStream; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InterruptedIOException; |
| 19 |
|
| 20 |
/** |
| 21 |
* A revivable input stream is an unbuffered input stream wrapping another input |
| 22 |
* stream. Its primary feature is that it allows to "kill" blocking |
| 23 |
* <code>.read</code> calls by calling <code>.kill</code>. Reading from the |
| 24 |
* stream can be resumed by calling <code>.resurrect</code>. |
| 25 |
* <p> |
| 26 |
* The common use for this is to avoid closing an input stream, while still be |
| 27 |
* able to cancel a blocking <code>.read</code> where you must use an input |
| 28 |
* stream. This is useful if you need to send a message to the thread which |
| 29 |
* attempts to read from the input stream. |
| 30 |
* |
| 31 |
* @author Jean Niklas L'orange |
| 32 |
* @since <code>com.hypirion.io 0.1.0</code> |
| 33 |
*/ |
| 34 |
|
| 35 |
public class RevivableInputStream extends InputStream { |
| 36 |
private InputStream in; |
| 37 |
|
| 38 |
private volatile boolean killed; |
| 39 |
private volatile boolean streamClosed; |
| 40 |
private volatile int requestedBytes; |
| 41 |
private volatile byte[] data; |
| 42 |
private volatile boolean requestData; |
| 43 |
private final Object dataLock; |
| 44 |
private volatile boolean threadCrashed; |
| 45 |
private volatile IOException threadException; |
| 46 |
|
| 47 |
private final ThreadReader reader; |
| 48 |
private final Thread readerThread; |
| 49 |
|
| 50 |
/** |
| 51 |
* Creates a new <code>RevivableInputStream</code> which wraps |
| 52 |
* <code>in</code>, giving it power to be killed and resurrected. |
| 53 |
*/ |
| 54 |
public RevivableInputStream(InputStream in) { |
| 55 |
this.in = in; |
| 56 |
killed = false; |
| 57 |
streamClosed = false; |
| 58 |
requestData = true; |
| 59 |
dataLock = new Object(); |
| 60 |
threadCrashed = false; |
| 61 |
threadException = null; |
| 62 |
requestedBytes = -1; |
| 63 |
data = null; |
| 64 |
reader = new ThreadReader(); |
| 65 |
readerThread = new Thread(reader); |
| 66 |
readerThread.setDaemon(true); |
| 67 |
readerThread.setName("RevivableReader " + in.hashCode()); |
| 68 |
readerThread.start(); |
| 69 |
while (requestData) { |
| 70 |
Thread.yield(); |
| 71 |
// ensure no deadlock by waiting for the reader to get into its |
| 72 |
// correct state |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the number of bytes than can be read from this input stream |
| 78 |
* without blocking. |
| 79 |
* |
| 80 |
* Will as of now return 0. |
| 81 |
* |
| 82 |
* @return 0 |
| 83 |
*/ |
| 84 |
public synchronized int available() { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Closes this revivable input stream and the underlying input stream, and |
| 90 |
* releases any with system resources (threads, memory) associated with this |
| 91 |
* stream. |
| 92 |
* |
| 93 |
* @exception IOException if the underlying <code>InputStream</code> throws |
| 94 |
* an <code>IOException</code>. |
| 95 |
*/ |
| 96 |
public synchronized void close() throws IOException { |
| 97 |
synchronized (dataLock) { |
| 98 |
in.close(); |
| 99 |
dataLock.notifyAll(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Reads the next byte of data from this revivable input stream. The value |
| 105 |
* byte is returned as an <code>int</code> in the range <code>0</code> to |
| 106 |
* <code>255</code>. This method blocks until no data is available, the end |
| 107 |
* of the stream is detected, an exception is thrown or if the reviable |
| 108 |
* input stream is (potentially temporarily) killed. |
| 109 |
* |
| 110 |
* @return the next byte of data, or <code>-1</code> if the end of the |
| 111 |
* stream is reached or the stream is killed. |
| 112 |
* @exception IOException if the underlying <code>InputStream</code> throws |
| 113 |
* an <code>IOException</code> when attempted to read. This exception will |
| 114 |
* be thrown every time read is called until the stream is closed. |
| 115 |
*/ |
| 116 |
public synchronized int read() throws IOException { |
| 117 |
byte[] b = new byte[1]; |
| 118 |
int count = 0; |
| 119 |
do { |
| 120 |
count = read(b); |
| 121 |
} while (count == 0); |
| 122 |
if (count == -1){ |
| 123 |
return -1; |
| 124 |
} |
| 125 |
else { |
| 126 |
return b[0]; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
public synchronized int read(byte[] b, int off, int len) |
| 131 |
throws IOException { |
| 132 |
synchronized (dataLock) { |
| 133 |
if (data == null) { |
| 134 |
requestedBytes = len; |
| 135 |
requestData = true; |
| 136 |
dataLock.notifyAll(); |
| 137 |
} |
| 138 |
try { |
| 139 |
while (data == null && !killed && !streamClosed && !threadCrashed) { |
| 140 |
dataLock.wait(); |
| 141 |
} |
| 142 |
} |
| 143 |
catch (InterruptedException ie) { |
| 144 |
throw new InterruptedIOException(); |
| 145 |
} |
| 146 |
if (streamClosed) |
| 147 |
return -1; |
| 148 |
if (threadCrashed) |
| 149 |
throw threadException; |
| 150 |
if (killed) |
| 151 |
return -1; |
| 152 |
// data must be non-null here due to dataLock's critical section. |
| 153 |
int n = data.length; |
| 154 |
if (n < len) { |
| 155 |
int totRead = n; |
| 156 |
System.arraycopy(data, 0, b, off, n); |
| 157 |
data = null; |
| 158 |
|
| 159 |
// In case we can read additional data without blocking |
| 160 |
int additional = Math.min(in.available(), len - n); |
| 161 |
if (additional > 0) { |
| 162 |
additional = in.read(b, off + n, additional); |
| 163 |
// ^ sanity check |
| 164 |
} |
| 165 |
totRead += additional; |
| 166 |
return totRead; |
| 167 |
} |
| 168 |
else if (n > len) { |
| 169 |
System.arraycopy(data, 0, b, off, len); |
| 170 |
int diff = n - len; |
| 171 |
byte[] newData = new byte[diff]; |
| 172 |
System.arraycopy(data, len, newData, 0, diff); |
| 173 |
data = newData; |
| 174 |
return len; |
| 175 |
} |
| 176 |
else { // here n == len |
| 177 |
System.arraycopy(data, 0, b, off, len); |
| 178 |
data = null; |
| 179 |
return len; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Kills this revivable input stream. Makes current and future read calls |
| 186 |
* immediately return -1. The input stream may be revived through |
| 187 |
* {@link #resurrect()}. If this revivable input stream is already killed, |
| 188 |
* this method does nothing. |
| 189 |
* |
| 190 |
* @see #resurrect() |
| 191 |
*/ |
| 192 |
public void kill() { |
| 193 |
synchronized (dataLock) { |
| 194 |
killed = true; |
| 195 |
dataLock.notifyAll(); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Resurrects a killed revivable input stream. This makes it possible to |
| 201 |
* read from this input stream once again. If this revivable input stream is |
| 202 |
* not killed, this method does nothing. |
| 203 |
* |
| 204 |
* @see #kill() |
| 205 |
*/ |
| 206 |
public synchronized void resurrect() { |
| 207 |
killed = false; |
| 208 |
} |
| 209 |
|
| 210 |
private class ThreadReader implements Runnable { |
| 211 |
@Override |
| 212 |
public void run() { |
| 213 |
while (true) { |
| 214 |
synchronized (dataLock) { |
| 215 |
requestData = false; |
| 216 |
dataLock.notifyAll(); |
| 217 |
try { |
| 218 |
while (!requestData) { |
| 219 |
dataLock.wait(); |
| 220 |
} |
| 221 |
} |
| 222 |
catch (InterruptedException ie) { |
| 223 |
threadCrashed = true; |
| 224 |
threadException = new InterruptedIOException(); |
| 225 |
// TODO: Use "real" exception |
| 226 |
return; |
| 227 |
} |
| 228 |
} |
| 229 |
// Data has been requested, create new array with data. |
| 230 |
try { |
| 231 |
byte[] buffer = new byte[requestedBytes]; |
| 232 |
int actualBytes = in.read(buffer); // <-- actual reading |
| 233 |
|
| 234 |
if (actualBytes == -1){ |
| 235 |
synchronized (dataLock){ |
| 236 |
streamClosed = true; |
| 237 |
dataLock.notifyAll(); |
| 238 |
return; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
byte[] actual = new byte[actualBytes]; |
| 243 |
System.arraycopy(buffer, 0, actual, 0, actualBytes); |
| 244 |
data = actual; |
| 245 |
} |
| 246 |
catch (IOException ioe) { |
| 247 |
synchronized (dataLock) { |
| 248 |
threadCrashed = true; |
| 249 |
threadException = ioe; // TODO: Proper wrapping here. |
| 250 |
dataLock.notifyAll(); |
| 251 |
return; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |