1 /* |
1 /* |
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
31 import java.io.File; |
31 import java.io.File; |
32 import java.io.FileInputStream; |
32 import java.io.FileInputStream; |
33 import java.io.FileOutputStream; |
33 import java.io.FileOutputStream; |
34 import java.io.IOException; |
34 import java.io.IOException; |
35 import java.io.InputStream; |
35 import java.io.InputStream; |
|
36 import java.util.logging.Level; |
|
37 import java.util.logging.Logger; |
36 |
38 |
37 /** |
39 /** |
38 * Reads a input stream completely and creates a new stream |
40 * Reads a input stream completely and creates a new stream |
39 * by keeping some data in memory and the rest on the file system. |
41 * by keeping some data in memory and the rest on the file system. |
40 * |
42 * |
45 private final @NotNull MemoryStream memStream; |
47 private final @NotNull MemoryStream memStream; |
46 private final @NotNull FileStream fileStream; |
48 private final @NotNull FileStream fileStream; |
47 |
49 |
48 private boolean readAll; |
50 private boolean readAll; |
49 private boolean closed; |
51 private boolean closed; |
|
52 |
|
53 private static final Logger LOGGER = Logger.getLogger(ReadAllStream.class.getName()); |
50 |
54 |
51 public ReadAllStream() { |
55 public ReadAllStream() { |
52 memStream = new MemoryStream(); |
56 memStream = new MemoryStream(); |
53 fileStream = new FileStream(); |
57 fileStream = new FileStream(); |
54 } |
58 } |
166 long total = 0; |
176 long total = 0; |
167 while(true) { |
177 while(true) { |
168 byte[] buf = new byte[8192]; |
178 byte[] buf = new byte[8192]; |
169 int read = fill(in, buf); |
179 int read = fill(in, buf); |
170 total += read; |
180 total += read; |
171 if (read != 0) |
181 if (read != 0) { |
172 add(buf, read); |
182 add(buf, read); |
173 if (read != buf.length) |
183 } |
174 return true; // EOF |
184 if (read != buf.length) { |
175 if (total > inMemory) |
185 return true; |
176 return false; // Reached in-memory size |
186 } // EOF |
|
187 if (total > inMemory) { |
|
188 return false; // Reached in-memory size |
|
189 } |
177 } |
190 } |
178 } |
191 } |
179 |
192 |
180 private int fill(InputStream in, byte[] buf) throws IOException { |
193 private int fill(InputStream in, byte[] buf) throws IOException { |
181 int read; |
194 int read; |