public
Description: JSR308 Typestate Checker
Homepage: http://www.warski.org/typestate.html
Clone URL: git://github.com/adamw/jsr308-typestate-checker.git
100644 48 lines (41 sloc) 1.528 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package checkers.typestate.ioexample;
 
import java.io.InputStream;
import java.io.IOException;
 
import static checkers.typestate.ioexample.InputStreamStates.*;
 
/**
* Reading from stream with a recovery function.
* @author Adam Warski (adam at warski dot org)
*/
public class Example2 {
/**
* Reads the first byte of the input stream.
* @param stream Stream from which to read the byte.
* @param numberOfRetries Number of times, which the read should be retries, in case of exceptions.
* @return The first byte of the stream.
* @throws IOException When the first byte couldn't be read in the specified number of retries.
*/
public int readRetry(@Open InputStream stream, int numberOfRetries) throws IOException {
try {
// Looping and trying to read from the stream the specified number of times - 1.
while (numberOfRetries > 1) {
try {
return stream.read();
} catch (IOException e) {
numberOfRetries--;
 
// Recovering the stream and trying again.
recover(stream);
}
}
 
// Here the state of "stream" is "@Open" for sure:
// - if no exception is thrown, the state doesn't change
// - if one is thrown, then the stream is recovered
 
// Trying to read one last time. If this throws an exception - the method will just throw it.
return stream.read();
} finally {
stream.close();
}
}
 
private void recover(@InputStreamError(after = Open.class, onException = InputStreamError.class) InputStream stream) throws IOException {
// Do some magic.
}
}