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