1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.monitor;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.FilterInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19
20 public class CaptureInputStream extends FilterInputStream
21 {
22 private final ByteArrayOutputStream capture = new ByteArrayOutputStream();
23 private long maxData = 0;
24 private boolean inCapture;
25
26 public CaptureInputStream( InputStream in, long maxData )
27 {
28 super( in );
29 this.maxData = maxData;
30 }
31
32 public CaptureInputStream( InputStream in )
33 {
34 super( in );
35 }
36
37 @Override
38 public int read() throws IOException
39 {
40 if( inCapture )
41 {
42 return super.read();
43 }
44 else
45 {
46 inCapture = true;
47 int i = super.read();
48 if( i != -1 && ( maxData == 0 || capture.size() < maxData ) )
49 capture.write( i );
50 inCapture = false;
51 return i;
52 }
53 }
54
55 @Override
56 public int read( byte[] b ) throws IOException
57 {
58 if( inCapture )
59 {
60 return super.read( b );
61 }
62 else
63 {
64 inCapture = true;
65 int i = super.read( b );
66 if( i > 0 )
67 {
68 if( maxData == 0 )
69 {
70 capture.write( b, 0, i );
71 }
72 else if( i > 0 && maxData > 0 && capture.size() < maxData )
73 {
74 if( i + capture.size() < maxData )
75 capture.write( b, 0, i );
76 else
77 capture.write( b, 0, ( int )( maxData - capture.size() ) );
78 }
79 }
80 inCapture = false;
81 return i;
82 }
83 }
84
85 @Override
86 public int read( byte[] b, int off, int len ) throws IOException
87 {
88 if( inCapture )
89 {
90 return super.read( b, off, len );
91 }
92 else
93 {
94 inCapture = true;
95 int i = super.read( b, off, len );
96 if( i > 0 )
97 {
98 if( maxData == 0 )
99 {
100 capture.write( b, off, i );
101 }
102 else if( i > 0 && maxData > 0 && capture.size() < maxData )
103 {
104 if( i + capture.size() < maxData )
105 capture.write( b, off, i );
106 else
107 capture.write( b, off, ( int )( maxData - capture.size() ) );
108 }
109 inCapture = false;
110 }
111
112 return i;
113 }
114 }
115
116 public byte[] getCapturedData()
117 {
118 return capture.toByteArray();
119 }
120 }