|
1 /* |
|
2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.util; |
|
27 |
|
28 import com.sun.istack.internal.NotNull; |
|
29 import com.sun.istack.internal.Nullable; |
|
30 |
|
31 import java.io.File; |
|
32 import java.io.FileInputStream; |
|
33 import java.io.FileOutputStream; |
|
34 import java.io.IOException; |
|
35 import java.io.InputStream; |
|
36 import java.util.logging.Level; |
|
37 import java.util.logging.Logger; |
|
38 |
|
39 /** |
|
40 * Reads a input stream completely and creates a new stream |
|
41 * by keeping some data in memory and the rest on the file system. |
|
42 * |
|
43 * @author Jitendra Kotamraju |
|
44 */ |
|
45 public class ReadAllStream extends InputStream { |
|
46 |
|
47 private final @NotNull MemoryStream memStream; |
|
48 private final @NotNull FileStream fileStream; |
|
49 |
|
50 private boolean readAll; |
|
51 private boolean closed; |
|
52 |
|
53 private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName()); |
|
54 |
|
55 public ReadAllStream() { |
|
56 memStream = new MemoryStream(); |
|
57 fileStream = new FileStream(); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Reads the data from input stream completely. It keeps |
|
62 * inMemory size in the memory, and the rest on the file |
|
63 * system. |
|
64 * |
|
65 * Caller's responsibility to close the InputStream. This |
|
66 * method can be called only once. |
|
67 * |
|
68 * @param in from which to be read |
|
69 * @param inMemory this much data is kept in the memory |
|
70 * @throws IOException in case of exception |
|
71 */ |
|
72 public void readAll(InputStream in, long inMemory) throws IOException { |
|
73 assert !readAll; |
|
74 readAll = true; |
|
75 |
|
76 boolean eof = memStream.readAll(in, inMemory); |
|
77 if (!eof) { |
|
78 fileStream.readAll(in); |
|
79 } |
|
80 } |
|
81 |
|
82 @Override |
|
83 public int read() throws IOException { |
|
84 int ch = memStream.read(); |
|
85 if (ch == -1) { |
|
86 ch = fileStream.read(); |
|
87 } |
|
88 return ch; |
|
89 } |
|
90 |
|
91 @Override |
|
92 public int read(byte b[], int off, int sz) throws IOException { |
|
93 int len = memStream.read(b, off, sz); |
|
94 if (len == -1) { |
|
95 len = fileStream.read(b, off, sz); |
|
96 } |
|
97 return len; |
|
98 } |
|
99 |
|
100 @Override |
|
101 public void close() throws IOException { |
|
102 if (!closed) { |
|
103 memStream.close(); |
|
104 fileStream.close(); |
|
105 closed = true; |
|
106 } |
|
107 } |
|
108 |
|
109 // Keeps the rest of the data on the file system |
|
110 private static class FileStream extends InputStream { |
|
111 private @Nullable File tempFile; |
|
112 private @Nullable FileInputStream fin; |
|
113 |
|
114 void readAll(InputStream in) throws IOException { |
|
115 tempFile = File.createTempFile("jaxws",".bin"); |
|
116 FileOutputStream fileOut = new FileOutputStream(tempFile); |
|
117 try { |
|
118 byte[] buf = new byte[8192]; |
|
119 int len; |
|
120 while((len=in.read(buf)) != -1) { |
|
121 fileOut.write(buf, 0, len); |
|
122 } |
|
123 } finally { |
|
124 fileOut.close(); |
|
125 } |
|
126 fin = new FileInputStream(tempFile); |
|
127 } |
|
128 |
|
129 @Override |
|
130 public int read() throws IOException { |
|
131 return (fin != null) ? fin.read() : -1; |
|
132 } |
|
133 |
|
134 @Override |
|
135 public int read(byte b[], int off, int sz) throws IOException { |
|
136 return (fin != null) ? fin.read(b, off, sz) : -1; |
|
137 } |
|
138 |
|
139 @Override |
|
140 public void close() throws IOException { |
|
141 if (fin != null) { |
|
142 fin.close(); |
|
143 } |
|
144 if (tempFile != null) { |
|
145 boolean success = tempFile.delete(); |
|
146 if (!success) { |
|
147 LOGGER.log(Level.INFO, "File {0} could not be deleted", tempFile); |
|
148 } |
|
149 } |
|
150 } |
|
151 } |
|
152 |
|
153 // Keeps data in memory until certain size |
|
154 private static class MemoryStream extends InputStream { |
|
155 private Chunk head, tail; |
|
156 private int curOff; |
|
157 |
|
158 private void add(byte[] buf, int len) { |
|
159 if (tail != null) { |
|
160 tail = tail.createNext(buf, 0, len); |
|
161 } else { |
|
162 head = tail = new Chunk(buf, 0, len); |
|
163 } |
|
164 } |
|
165 |
|
166 /** |
|
167 * Reads until the size specified |
|
168 * |
|
169 * @param in stream from which to be read |
|
170 * @param inMemory reads until this size |
|
171 * @return true if eof |
|
172 * false otherwise |
|
173 * @throws IOException in case of exception |
|
174 */ |
|
175 boolean readAll(InputStream in, long inMemory) throws IOException { |
|
176 long total = 0; |
|
177 while(true) { |
|
178 byte[] buf = new byte[8192]; |
|
179 int read = fill(in, buf); |
|
180 total += read; |
|
181 if (read != 0) { |
|
182 add(buf, read); |
|
183 } |
|
184 if (read != buf.length) { |
|
185 return true; |
|
186 } // EOF |
|
187 if (total > inMemory) { |
|
188 return false; // Reached in-memory size |
|
189 } |
|
190 } |
|
191 } |
|
192 |
|
193 private int fill(InputStream in, byte[] buf) throws IOException { |
|
194 int read; |
|
195 int total = 0; |
|
196 while(total < buf.length && (read=in.read(buf, total, buf.length-total)) != -1) { |
|
197 total += read; |
|
198 } |
|
199 return total; |
|
200 } |
|
201 |
|
202 @Override |
|
203 public int read() throws IOException { |
|
204 if (!fetch()) { |
|
205 return -1; |
|
206 } |
|
207 return (head.buf[curOff++] & 0xff); |
|
208 } |
|
209 |
|
210 @Override |
|
211 public int read(byte b[], int off, int sz) throws IOException { |
|
212 if (!fetch()) { |
|
213 return -1; |
|
214 } |
|
215 sz = Math.min(sz, head.len-(curOff-head.off)); |
|
216 System.arraycopy(head.buf,curOff,b,off,sz); |
|
217 curOff += sz; |
|
218 return sz; |
|
219 } |
|
220 |
|
221 // if eof, return false else true |
|
222 private boolean fetch() { |
|
223 if (head == null) { |
|
224 return false; |
|
225 } |
|
226 if (curOff == head.off+head.len) { |
|
227 head = head.next; |
|
228 if (head == null) { |
|
229 return false; |
|
230 } |
|
231 curOff = head.off; |
|
232 } |
|
233 return true; |
|
234 } |
|
235 |
|
236 private static final class Chunk { |
|
237 Chunk next; |
|
238 final byte[] buf; |
|
239 final int off; |
|
240 final int len; |
|
241 |
|
242 public Chunk(byte[] buf, int off, int len) { |
|
243 this.buf = buf; |
|
244 this.off = off; |
|
245 this.len = len; |
|
246 } |
|
247 |
|
248 public Chunk createNext(byte[] buf, int off, int len) { |
|
249 return next = new Chunk(buf, off, len); |
|
250 } |
|
251 } |
|
252 } |
|
253 } |