1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared.util.io; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.PushbackInputStream; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class DynamicPushbackInputStream extends PushbackInputStream |
31 | |
{ |
32 | |
|
33 | |
private final int origsize; |
34 | |
|
35 | |
public DynamicPushbackInputStream(InputStream in) |
36 | |
{ |
37 | 0 | super(in); |
38 | 0 | this.origsize = 1; |
39 | 0 | } |
40 | |
|
41 | |
public DynamicPushbackInputStream(InputStream in, int initialSize) |
42 | |
{ |
43 | 0 | super(in, initialSize); |
44 | 0 | this.origsize = initialSize; |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public int clear() |
51 | |
{ |
52 | 0 | int m = buf.length; |
53 | 0 | buf = new byte[origsize]; |
54 | 0 | pos = origsize; |
55 | 0 | return m; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public int shrink() |
63 | |
{ |
64 | 0 | byte[] old = buf; |
65 | 0 | if (pos == 0) |
66 | |
{ |
67 | 0 | return 0; |
68 | |
} |
69 | 0 | int n = old.length - pos; |
70 | |
int m; |
71 | |
int p; |
72 | |
int s; |
73 | |
int l; |
74 | 0 | if (n < origsize) |
75 | |
{ |
76 | 0 | buf = new byte[origsize]; |
77 | 0 | p = pos; |
78 | 0 | s = origsize - n; |
79 | 0 | l = old.length - p; |
80 | 0 | m = old.length - origsize; |
81 | 0 | pos = s; |
82 | |
} |
83 | |
else |
84 | |
{ |
85 | 0 | buf = new byte[n]; |
86 | 0 | p = pos; |
87 | 0 | s = 0; |
88 | 0 | l = n; |
89 | 0 | m = old.length - l; |
90 | 0 | pos = 0; |
91 | |
} |
92 | 0 | System.arraycopy(old, p, buf, s, l); |
93 | 0 | return m; |
94 | |
} |
95 | |
|
96 | |
private void resize(int len) |
97 | |
{ |
98 | 0 | byte[] old = buf; |
99 | 0 | buf = new byte[old.length + len]; |
100 | 0 | System.arraycopy(old, 0, buf, len, old.length); |
101 | 0 | } |
102 | |
|
103 | |
public void unread(byte[] b, int off, int len) throws IOException |
104 | |
{ |
105 | 0 | if (len > pos && pos + len > buf.length) |
106 | |
{ |
107 | 0 | resize(len - pos); |
108 | 0 | pos += len - pos; |
109 | |
} |
110 | 0 | super.unread(b, off, len); |
111 | 0 | } |
112 | |
|
113 | |
public void unread(int b) throws IOException |
114 | |
{ |
115 | 0 | if (pos == 0) |
116 | |
{ |
117 | 0 | resize(1); |
118 | 0 | pos++; |
119 | |
} |
120 | 0 | super.unread(b); |
121 | 0 | } |
122 | |
|
123 | |
public int read() throws IOException |
124 | |
{ |
125 | 0 | int m = super.read(); |
126 | 0 | if (pos >= buf.length && buf.length > origsize) |
127 | |
{ |
128 | 0 | shrink(); |
129 | |
} |
130 | 0 | return m; |
131 | |
} |
132 | |
|
133 | |
public int read(byte[] b, int off, int len) throws IOException |
134 | |
{ |
135 | 0 | this.available(); |
136 | |
|
137 | 0 | int r = super.read(b, off, len); |
138 | 0 | if (pos >= buf.length && buf.length > origsize) |
139 | |
{ |
140 | 0 | shrink(); |
141 | |
} |
142 | 0 | return r; |
143 | |
} |
144 | |
|
145 | |
public long skip(long n) throws IOException |
146 | |
{ |
147 | 0 | long r = super.skip(n); |
148 | 0 | if (pos >= buf.length && buf.length > origsize) |
149 | |
{ |
150 | 0 | shrink(); |
151 | |
} |
152 | 0 | return r; |
153 | |
} |
154 | |
} |