View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.pgp;
8   
9   import java.io.PipedOutputStream;
10  
11  /**
12   * A {@link TransformPolicy} that copies the transformed bytes continuously into the {@link PipedOutputStream}
13   * without taking into account about how many bytes the object has requested.
14   */
15  public class TransformContinuouslyPolicy extends AbstractTransformPolicy
16  {
17  
18      public static final long DEFAULT_CHUNK_SIZE = 1 << 24;
19      
20      private long chunkSize;
21  
22      public TransformContinuouslyPolicy()
23      {
24          this(DEFAULT_CHUNK_SIZE);
25      }
26  
27      public TransformContinuouslyPolicy(long chunkSize)
28      {
29          this.chunkSize = chunkSize;
30      }
31  
32      /**
33       * {@inheritDoc}
34       */
35      @Override
36      public void readRequest(long length)
37      {
38          /**
39           * Avoid calling super so that we don't add more bytes. 
40           * The ContinuousWork will add the requested bytes as necessary
41           * only start the copying thread
42           */
43          startCopyingThread();
44      }
45      
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      protected Thread getCopyingThread()
51      {
52          return new ContinuousWork();
53      }
54  
55      private class ContinuousWork extends TransformerWork
56      {
57          @Override
58          protected void execute() throws Exception
59          {
60              getTransformer().initialize(getInputStream().getOut());
61              
62              boolean finishWriting = false;
63              while (!finishWriting)
64              {
65                  getBytesRequested().addAndGet(chunkSize);
66                  finishWriting = getTransformer().write(getInputStream().getOut(), getBytesRequested());
67              }            
68          }
69      }
70  }
71  
72