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