1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.model.streaming; |
8 | |
|
9 | |
import java.io.IOException; |
10 | |
import java.io.OutputStream; |
11 | |
|
12 | |
import org.apache.commons.logging.Log; |
13 | |
import org.apache.commons.logging.LogFactory; |
14 | |
|
15 | |
public class CallbackOutputStream extends OutputStream |
16 | |
{ |
17 | 0 | private static final Log logger = LogFactory.getLog(CallbackOutputStream.class); |
18 | |
|
19 | |
public static interface Callback |
20 | |
{ |
21 | |
public void onClose() throws Exception; |
22 | |
} |
23 | |
|
24 | |
private OutputStream delegate; |
25 | |
private Callback callback; |
26 | |
|
27 | |
public CallbackOutputStream(OutputStream delegate, Callback callback) |
28 | 0 | { |
29 | 0 | this.delegate = delegate; |
30 | 0 | this.callback = callback; |
31 | 0 | } |
32 | |
|
33 | |
@Override |
34 | |
public void write(int b) throws IOException |
35 | |
{ |
36 | 0 | delegate.write(b); |
37 | 0 | } |
38 | |
|
39 | |
@Override |
40 | |
public void write(byte b[]) throws IOException |
41 | |
{ |
42 | 0 | delegate.write(b); |
43 | 0 | } |
44 | |
|
45 | |
@Override |
46 | |
public void write(byte b[], int off, int len) throws IOException |
47 | |
{ |
48 | 0 | delegate.write(b, off, len); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public void close() throws IOException |
53 | |
{ |
54 | |
try |
55 | |
{ |
56 | 0 | delegate.close(); |
57 | |
} |
58 | |
finally |
59 | |
{ |
60 | 0 | closeCallback(); |
61 | 0 | } |
62 | 0 | } |
63 | |
|
64 | |
private void closeCallback() |
65 | |
{ |
66 | 0 | if (null != callback) |
67 | |
{ |
68 | |
try |
69 | |
{ |
70 | 0 | callback.onClose(); |
71 | |
} |
72 | 0 | catch(Exception e) |
73 | |
{ |
74 | 0 | logger.debug("Suppressing exception while releasing resources: " + e.getMessage()); |
75 | 0 | } |
76 | |
} |
77 | |
|
78 | 0 | } |
79 | |
} |