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