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.transport.tcp.integration;
8   
9   import java.io.InputStream;
10  import java.io.IOException;
11  import java.text.MessageFormat;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  
16  public class BigInputStream extends InputStream
17  {
18  
19      private static final int SUMMARY_SIZE = 4;
20      private static final MessageFormat FORMAT  =
21              new MessageFormat("Sent {0,number,#} bytes, {1,number,###.##}% (free {2,number,#}/{3,number,#})");
22      private final Log logger = LogFactory.getLog(getClass());
23      private long size;
24      private int messages;
25  
26      private long sent = 0;
27      private byte[] data;
28      private int dataIndex = 0;
29      private long printedMessages = 0;
30      private long nextMessage = 0;
31  
32  
33      /**
34       * @param size Number of bytes to transfer
35       * @param messages Number of mesagges logged as INFO
36       */
37      public BigInputStream(long size, int messages)
38      {
39          this.size = size;
40          this.messages = messages;
41          data = ("This message is repeated for " + size + " bytes. ").getBytes();
42      }
43  
44      /**
45       * @return String matching {@link org.mule.tck.functional.FunctionalStreamingTestComponent}
46       */
47      public String summary()
48      {
49  
50          byte[] tail = new byte[SUMMARY_SIZE];
51          for (int i = 0; i < SUMMARY_SIZE; ++i)
52          {
53              tail[i] = data[(int) ((sent - SUMMARY_SIZE + i) % data.length)];
54          }
55          return "Received stream; length: " + sent + "; '" +
56                  new String(data, 0, 4) + "..." + new String(tail) +
57                  "'";
58      }
59  
60      public int read() throws IOException
61      {
62          if (sent == size)
63          {
64              return -1;
65          }
66          else
67          {
68              if (++sent > nextMessage)
69              {
70                  double percent = 100l * sent / ((double) size);
71                  Runtime runtime = Runtime.getRuntime();
72                  logger.info(FORMAT.format(new Object[]{
73                          new Long(sent), new Double(percent),
74                          new Long(runtime.freeMemory()), new Long(runtime.maxMemory())}));
75                  nextMessage = ++printedMessages *
76                          ((int) Math.floor(((double) size) / (messages - 1)) - 1);
77              }
78              if (dataIndex == data.length)
79              {
80                  dataIndex = 0;
81              }
82              return data[dataIndex++];
83          }
84      }
85  
86      public int available() throws IOException
87      {
88          return (int) Math.min(size - sent, Integer.MAX_VALUE);
89      }
90  
91  }
92  
93