View Javadoc

1   /*
2    * $Id: TransformContinuouslyPolicy.java 22865 2011-09-05 17:23:45Z dfeist $
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.module.pgp;
12  
13  import java.io.PipedOutputStream;
14  
15  /**
16   * A {@link TransformPolicy} that copies the transformed bytes continuously into the {@link PipedOutputStream}
17   * without taking into account about how many bytes the object has requested.
18   */
19  public class TransformContinuouslyPolicy extends AbstractTransformPolicy
20  {
21  
22      public static final long DEFAULT_CHUNK_SIZE = 1 << 24;
23      
24      private long chunkSize;
25  
26      public TransformContinuouslyPolicy()
27      {
28          this(DEFAULT_CHUNK_SIZE);
29      }
30  
31      public TransformContinuouslyPolicy(long chunkSize)
32      {
33          this.chunkSize = chunkSize;
34      }
35  
36      /**
37       * {@inheritDoc}
38       */
39      @Override
40      public void readRequest(long length)
41      {
42          /**
43           * Avoid calling super so that we don't add more bytes. 
44           * The ContinuousWork will add the requested bytes as necessary
45           * only start the copying thread
46           */
47          startCopyingThread();
48      }
49      
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      protected Thread getCopyingThread()
55      {
56          return new ContinuousWork();
57      }
58  
59      private class ContinuousWork extends TransformerWork
60      {
61          @Override
62          protected void execute() throws Exception
63          {
64              getTransformer().initialize(getInputStream().getOut());
65              
66              boolean finishWriting = false;
67              while (!finishWriting)
68              {
69                  getBytesRequested().addAndGet(chunkSize);
70                  finishWriting = getTransformer().write(getInputStream().getOut(), getBytesRequested());
71              }            
72          }
73      }
74  }
75  
76