View Javadoc

1   /*
2    * $Id: CloseCountDownOutputStream.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.impl.model.streaming;
12  
13  import java.io.IOException;
14  import java.io.OutputStream;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17  
18  /**
19   * Decrements the latch on close.
20   */
21  public class CloseCountDownOutputStream extends OutputStream
22  {
23  
24      private OutputStream delegate;
25      private CountDownLatch latch;
26  
27      public CloseCountDownOutputStream(OutputStream delegate, CountDownLatch latch)
28      {
29          this.delegate = delegate;
30          this.latch = latch;
31      }
32  
33      public void write(int b) throws IOException
34      {
35          delegate.write(b);
36      }
37  
38      public void write(byte b[]) throws IOException
39      {
40          delegate.write(b);
41      }
42  
43      public void write(byte b[], int off, int len) throws IOException
44      {
45          delegate.write(b, off, len);
46      }
47  
48      public void close() throws IOException
49      {
50          try
51          {
52              delegate.close();
53          }
54          finally
55          {
56              if (null != latch)
57              {
58                  latch.countDown();
59              }
60          }
61      }
62  
63  }