View Javadoc

1   /*
2    * $Id: CloseCountDownInputStream.java 7976 2007-08-21 14:26:13Z 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.InputStream;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
17  
18  /**
19   * Decrements the latch on close.
20   */
21  public class CloseCountDownInputStream extends InputStream
22  {
23  
24      private InputStream delegate;
25      private CountDownLatch latch;
26  
27      public CloseCountDownInputStream(InputStream delegate, CountDownLatch latch)
28      {
29          this.delegate = delegate;
30          this.latch = latch;
31      }
32  
33      public int read() throws IOException
34      {
35          return delegate.read();
36      }
37  
38      public int read(byte b[]) throws IOException
39      {
40          return delegate.read(b);
41      }
42  
43      public int read(byte b[], int off, int len) throws IOException
44      {
45          return delegate.read(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  }