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 | |
|
22 | 0 | protected final Log logger = LogFactory.getLog(CallbackOutputStream.class); |
23 | |
|
24 | |
public static interface Callback |
25 | |
{ |
26 | |
|
27 | |
public void onClose() throws Exception; |
28 | |
|
29 | |
} |
30 | |
|
31 | |
private OutputStream delegate; |
32 | |
private Callback callback; |
33 | |
|
34 | |
public CallbackOutputStream(OutputStream delegate, Callback callback) |
35 | 0 | { |
36 | 0 | this.delegate = delegate; |
37 | 0 | this.callback = callback; |
38 | 0 | } |
39 | |
|
40 | |
public void write(int b) throws IOException |
41 | |
{ |
42 | 0 | delegate.write(b); |
43 | 0 | } |
44 | |
|
45 | |
public void write(byte b[]) throws IOException |
46 | |
{ |
47 | 0 | delegate.write(b); |
48 | 0 | } |
49 | |
|
50 | |
public void write(byte b[], int off, int len) throws IOException |
51 | |
{ |
52 | 0 | delegate.write(b, off, len); |
53 | 0 | } |
54 | |
|
55 | |
public void close() throws IOException |
56 | |
{ |
57 | |
try |
58 | |
{ |
59 | 0 | delegate.close(); |
60 | |
} |
61 | |
finally |
62 | |
{ |
63 | 0 | closeCallback(); |
64 | 0 | } |
65 | 0 | } |
66 | |
|
67 | |
private void closeCallback() |
68 | |
{ |
69 | 0 | if (null != callback) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 0 | callback.onClose(); |
74 | |
} |
75 | 0 | catch(Exception e) |
76 | |
{ |
77 | 0 | logger.debug("Suppressing exception while releasing resources: " + e.getMessage()); |
78 | 0 | } |
79 | |
} |
80 | |
|
81 | 0 | } |
82 | |
|
83 | |
} |