1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.util.io;
12
13 import java.io.IOException;
14 import java.io.InterruptedIOException;
15 import java.io.OutputStream;
16 import java.text.MessageFormat;
17
18 import org.eclipse.jgit.internal.JGitText;
19
20
21
22
23 public class TimeoutOutputStream extends OutputStream {
24 private final OutputStream dst;
25
26 private final InterruptTimer myTimer;
27
28 private int timeout;
29
30
31
32
33
34
35
36
37
38
39 public TimeoutOutputStream(final OutputStream destination,
40 final InterruptTimer timer) {
41 dst = destination;
42 myTimer = timer;
43 }
44
45
46
47
48
49
50 public int getTimeout() {
51 return timeout;
52 }
53
54
55
56
57
58
59
60
61 public void setTimeout(int millis) {
62 if (millis < 0)
63 throw new IllegalArgumentException(MessageFormat.format(
64 JGitText.get().invalidTimeout, Integer.valueOf(millis)));
65 timeout = millis;
66 }
67
68
69 @Override
70 public void write(int b) throws IOException {
71 try {
72 beginWrite();
73 dst.write(b);
74 } catch (InterruptedIOException e) {
75 throw writeTimedOut(e);
76 } finally {
77 endWrite();
78 }
79 }
80
81
82 @Override
83 public void write(byte[] buf) throws IOException {
84 write(buf, 0, buf.length);
85 }
86
87
88 @Override
89 public void write(byte[] buf, int off, int len) throws IOException {
90 try {
91 beginWrite();
92 dst.write(buf, off, len);
93 } catch (InterruptedIOException e) {
94 throw writeTimedOut(e);
95 } finally {
96 endWrite();
97 }
98 }
99
100
101 @Override
102 public void flush() throws IOException {
103 try {
104 beginWrite();
105 dst.flush();
106 } catch (InterruptedIOException e) {
107 throw writeTimedOut(e);
108 } finally {
109 endWrite();
110 }
111 }
112
113
114 @Override
115 public void close() throws IOException {
116 try {
117 beginWrite();
118 dst.close();
119 } catch (InterruptedIOException e) {
120 throw writeTimedOut(e);
121 } finally {
122 endWrite();
123 }
124 }
125
126 private void beginWrite() {
127 myTimer.begin(timeout);
128 }
129
130 private void endWrite() {
131 myTimer.end();
132 }
133
134 private InterruptedIOException writeTimedOut(InterruptedIOException cause) {
135 InterruptedIOException e = new InterruptedIOException(
136 MessageFormat.format(JGitText.get().writeTimedOut,
137 Integer.valueOf(timeout)));
138 e.initCause(cause);
139 return e;
140 }
141 }