1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.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 long printedMessages = 0;
34 private long nextMessage = 0;
35
36
37
38
39
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
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 public int available() throws IOException
91 {
92 return (int) Math.min(size - sent, Integer.MAX_VALUE);
93 }
94
95 }
96
97