1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.pgp; |
8 | |
|
9 | |
import java.io.IOException; |
10 | |
import java.io.InputStream; |
11 | |
import java.io.PipedInputStream; |
12 | |
import java.io.PipedOutputStream; |
13 | |
|
14 | |
import org.apache.commons.lang.Validate; |
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class LazyTransformedInputStream extends InputStream |
28 | |
{ |
29 | |
private PipedInputStream in; |
30 | |
private PipedOutputStream out; |
31 | |
private TransformPolicy transformPolicy; |
32 | |
private StreamTransformer transformer; |
33 | |
|
34 | |
public LazyTransformedInputStream(TransformPolicy transformPolicy, StreamTransformer transformer) throws IOException |
35 | 0 | { |
36 | 0 | Validate.notNull(transformPolicy, "The transformPolicy should not be null"); |
37 | 0 | Validate.notNull(transformer, "The transformer should not be null"); |
38 | |
|
39 | 0 | this.in = new PipedInputStream(); |
40 | 0 | this.out = new PipedOutputStream(this.in); |
41 | 0 | this.transformPolicy = transformPolicy; |
42 | 0 | this.transformer = transformer; |
43 | 0 | this.transformPolicy.initialize(this); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
public int available() throws IOException |
48 | |
{ |
49 | 0 | this.transformPolicy.readRequest(100); |
50 | 0 | return this.in.available(); |
51 | |
} |
52 | |
|
53 | |
@Override |
54 | |
public void close() throws IOException |
55 | |
{ |
56 | 0 | this.in.close(); |
57 | 0 | this.transformPolicy.release(); |
58 | 0 | } |
59 | |
|
60 | |
@Override |
61 | |
protected void finalize() throws Throwable |
62 | |
{ |
63 | 0 | this.transformPolicy.release(); |
64 | 0 | } |
65 | |
|
66 | |
@Override |
67 | |
public synchronized void mark(int readlimit) |
68 | |
{ |
69 | 0 | this.in.mark(readlimit); |
70 | 0 | } |
71 | |
|
72 | |
@Override |
73 | |
public boolean markSupported() |
74 | |
{ |
75 | 0 | return this.in.markSupported(); |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public int read() throws IOException |
80 | |
{ |
81 | 0 | this.transformPolicy.readRequest(1); |
82 | 0 | return this.in.read(); |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
public int read(byte[] b, int off, int len) throws IOException |
87 | |
{ |
88 | 0 | this.transformPolicy.readRequest(len); |
89 | 0 | return this.in.read(b, off, len); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public int read(byte[] b) throws IOException |
94 | |
{ |
95 | 0 | this.transformPolicy.readRequest(b.length); |
96 | 0 | return this.in.read(b); |
97 | |
} |
98 | |
|
99 | |
@Override |
100 | |
public synchronized void reset() throws IOException |
101 | |
{ |
102 | 0 | this.in.reset(); |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public long skip(long n) throws IOException |
107 | |
{ |
108 | 0 | this.transformPolicy.readRequest(n); |
109 | 0 | return this.in.skip(n); |
110 | |
} |
111 | |
|
112 | |
PipedOutputStream getOut() |
113 | |
{ |
114 | 0 | return out; |
115 | |
} |
116 | |
|
117 | |
StreamTransformer getTransformer() |
118 | |
{ |
119 | 0 | return transformer; |
120 | |
} |
121 | |
} |