diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java index 023bde39f..d57a9b443 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnelHTTPServer.java @@ -658,8 +658,6 @@ public class I2PTunnelHTTPServer extends I2PTunnelServer { private final String _name; // shadows _log in super() private final Log _log; - private static final int BUF_SIZE = 8*1024; - private static final ByteCache _cache = ByteCache.getInstance(16, BUF_SIZE); public Sender(OutputStream out, InputStream in, String name, Log log) { _out = out; @@ -671,25 +669,15 @@ public class I2PTunnelHTTPServer extends I2PTunnelServer { public void run() { if (_log.shouldLog(Log.INFO)) _log.info(_name + ": Begin sending"); - ByteArray ba = _cache.acquire(); try { - byte buf[] = ba.getData(); - int read = 0; - long total = 0; - while ( (read = _in.read(buf)) != -1) { - if (_log.shouldLog(Log.INFO)) - _log.info(_name + ": read " + read + " and sending through the stream"); - _out.write(buf, 0, read); - total += read; - } + DataHelper.copy(_in, _out); if (_log.shouldLog(Log.INFO)) - _log.info(_name + ": Done sending: " + total); + _log.info(_name + ": Done sending"); //_out.flush(); } catch (IOException ioe) { if (_log.shouldLog(Log.DEBUG)) _log.debug("Error sending", ioe); } finally { - _cache.release(ba); if (_out != null) try { _out.close(); } catch (IOException ioe) {} if (_in != null) try { _in.close(); } catch (IOException ioe) {} } diff --git a/apps/routerconsole/java/src/net/i2p/router/update/NewsFetcher.java b/apps/routerconsole/java/src/net/i2p/router/update/NewsFetcher.java index 2b0d5b081..2f39f1053 100644 --- a/apps/routerconsole/java/src/net/i2p/router/update/NewsFetcher.java +++ b/apps/routerconsole/java/src/net/i2p/router/update/NewsFetcher.java @@ -560,11 +560,7 @@ class NewsFetcher extends UpdateRunner { try { in.initialize(new FileInputStream(from)); out = new SecureFileOutputStream(to); - byte buf[] = new byte[4096]; - int read; - while ((read = in.read(buf)) != -1) { - out.write(buf, 0, read); - } + DataHelper.copy(in, out); } finally { if (out != null) try { out.close(); diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java index 844dd3d5d..4cdf13e35 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/ConfigClientsHandler.java @@ -434,11 +434,7 @@ public class ConfigClientsHandler extends FormHandler { tmp = new File(_context.getTempDir(), "plugin-" + _context.random().nextInt() + (isSU3 ? ".su3" : ".xpi2p")); out = new BufferedOutputStream(new SecureFileOutputStream(tmp)); out.write(magic); - byte buf[] = new byte[16*1024]; - int read = 0; - while ( (read = in.read(buf)) != -1) { - out.write(buf, 0, read); - } + DataHelper.copy(in, out); out.close(); String url = tmp.toURI().toString(); // threaded... TODO inline to get better result to UI? diff --git a/apps/routerconsole/jsp/createreseed.jsp b/apps/routerconsole/jsp/createreseed.jsp index 650a237f0..67653b155 100644 --- a/apps/routerconsole/jsp/createreseed.jsp +++ b/apps/routerconsole/jsp/createreseed.jsp @@ -22,11 +22,8 @@ try { response.addHeader("Content-Disposition", "attachment; filename=\"i2preseed.zip\""); byte buf[] = new byte[16*1024]; in = new java.io.FileInputStream(zip); - int read = 0; java.io.OutputStream cout = response.getOutputStream(); - while ( (read = in.read(buf)) != -1) { - cout.write(buf, 0, read); - } + net.i2p.data.DataHelper.copy(in, cout); } finally { if (in != null) try { in.close(); } catch (java.io.IOException ioe) {} diff --git a/apps/routerconsole/jsp/viewrouterlog.jsp b/apps/routerconsole/jsp/viewrouterlog.jsp index 766f43852..c829f7715 100644 --- a/apps/routerconsole/jsp/viewrouterlog.jsp +++ b/apps/routerconsole/jsp/viewrouterlog.jsp @@ -24,11 +24,7 @@ if (length <= 0 || !f.isFile()) { try { in = new java.io.FileInputStream(f); java.io.OutputStream bout = response.getOutputStream(); - int read = 0; - byte buf[] = new byte[4*1024]; - while ((read = in.read(buf)) != -1) { - bout.write(buf, 0, read); - } + net.i2p.data.DataHelper.copy(in, bout); } catch (java.io.IOException ioe) { // prevent 'Committed' IllegalStateException from Jetty if (!response.isCommitted()) { diff --git a/apps/routerconsole/jsp/viewwrapperlog.jsp b/apps/routerconsole/jsp/viewwrapperlog.jsp index 9a60a0931..8ac9a70ca 100644 --- a/apps/routerconsole/jsp/viewwrapperlog.jsp +++ b/apps/routerconsole/jsp/viewwrapperlog.jsp @@ -22,11 +22,7 @@ if (length <= 0 || !f.isFile()) { try { in = new java.io.FileInputStream(f); java.io.OutputStream bout = response.getOutputStream(); - int read = 0; - byte buf[] = new byte[4*1024]; - while ((read = in.read(buf)) != -1) { - bout.write(buf, 0, read); - } + net.i2p.data.DataHelper.copy(in, bout); } catch (java.io.IOException ioe) { // prevent 'Committed' IllegalStateException from Jetty if (!response.isCommitted()) { diff --git a/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java b/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java index d5aa9bebc..5a17a79b3 100644 --- a/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java +++ b/apps/susimail/src/src/i2p/susi/webmail/PersistentMailCache.java @@ -272,11 +272,7 @@ class PersistentMailCache { } in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(f))); ByteArrayOutputStream out = new ByteArrayOutputStream((int) len); - int read = 0; - byte buf[] = new byte[4*1024]; - while ( (read = in.read(buf)) != -1) { - out.write(buf, 0, read); - } + DataHelper.copy(in, out); ReadBuffer rb = new ReadBuffer(out.toByteArray(), 0, out.size()); return rb; } catch (IOException ioe) { diff --git a/core/java/src/freenet/support/CPUInformation/CPUID.java b/core/java/src/freenet/support/CPUInformation/CPUID.java index 5bd79d0ae..3cbee478b 100644 --- a/core/java/src/freenet/support/CPUInformation/CPUID.java +++ b/core/java/src/freenet/support/CPUInformation/CPUID.java @@ -18,6 +18,7 @@ import java.net.URL; import java.util.Locale; import net.i2p.I2PAppContext; +import net.i2p.data.DataHelper; import net.i2p.util.FileUtil; import net.i2p.util.SystemVersion; @@ -535,12 +536,7 @@ public class CPUID { InputStream libStream = resource.openStream(); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename); fos = new FileOutputStream(outFile); - byte buf[] = new byte[4096]; - while (true) { - int read = libStream.read(buf); - if (read < 0) break; - fos.write(buf, 0, read); - } + DataHelper.copy(libStream, fos); fos.close(); fos = null; System.load(outFile.getAbsolutePath());//System.load requires an absolute path to the lib diff --git a/core/java/src/net/i2p/data/Base32.java b/core/java/src/net/i2p/data/Base32.java index 704c79312..e2ba29d0b 100644 --- a/core/java/src/net/i2p/data/Base32.java +++ b/core/java/src/net/i2p/data/Base32.java @@ -102,12 +102,7 @@ public class Base32 { private static byte[] read(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(64); - byte buf[] = new byte[64]; - while (true) { - int read = in.read(buf); - if (read < 0) break; - baos.write(buf, 0, read); - } + DataHelper.copy(in, baos); return baos.toByteArray(); } diff --git a/core/java/src/net/i2p/data/Base64.java b/core/java/src/net/i2p/data/Base64.java index 210bf9efc..aabc9c473 100644 --- a/core/java/src/net/i2p/data/Base64.java +++ b/core/java/src/net/i2p/data/Base64.java @@ -258,12 +258,7 @@ public class Base64 { private static byte[] read(InputStream in) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); - byte buf[] = new byte[1024]; - while (true) { - int read = in.read(buf); - if (read < 0) break; - baos.write(buf, 0, read); - } + DataHelper.copy(in, baos); return baos.toByteArray(); } diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 84b77ae4a..1ab34c1b9 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -1830,4 +1830,25 @@ public class DataHelper { } return p.split(s, limit); } + + /** + * Copy in to out. Caller MUST close the streams. + * + * @param in non-null + * @param out non-null + * @since 0.9.29 + */ + public static void copy(InputStream in, OutputStream out) throws IOException { + final ByteCache cache = ByteCache.getInstance(8, 8*1024); + final ByteArray ba = cache.acquire(); + try { + final byte buf[] = ba.getData(); + int read; + while ((read = in.read(buf)) != -1) { + out.write(buf, 0, read); + } + } finally { + cache.release(ba); + } + } } diff --git a/core/java/src/net/i2p/util/EepGet.java b/core/java/src/net/i2p/util/EepGet.java index 85c34b49b..85644a982 100644 --- a/core/java/src/net/i2p/util/EepGet.java +++ b/core/java/src/net/i2p/util/EepGet.java @@ -1735,8 +1735,6 @@ public class EepGet { protected class Gunzipper implements Runnable { private final InputStream _inRaw; private final OutputStream _out; - private static final int CACHE_SIZE = 8*1024; - private final ByteCache _cache = ByteCache.getInstance(8, CACHE_SIZE); public Gunzipper(InputStream in, OutputStream out) { _inRaw = in; @@ -1750,12 +1748,7 @@ public class EepGet { try { // blocking in.initialize(_inRaw); - ba = _cache.acquire(); - byte buf[] = ba.getData(); - int read = -1; - while ( (read = in.read(buf)) != -1) { - _out.write(buf, 0, read); - } + DataHelper.copy(in, _out); } catch (IOException ioe) { _decompressException = ioe; if (_log.shouldLog(Log.WARN)) @@ -1768,8 +1761,6 @@ public class EepGet { _out.close(); } catch (IOException ioe) {} ReusableGZIPInputStream.release(in); - if (ba != null) - _cache.release(ba); } } } diff --git a/core/java/src/net/i2p/util/FileUtil.java b/core/java/src/net/i2p/util/FileUtil.java index 296030818..c72e93de5 100644 --- a/core/java/src/net/i2p/util/FileUtil.java +++ b/core/java/src/net/i2p/util/FileUtil.java @@ -18,6 +18,8 @@ import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import net.i2p.data.DataHelper; + // Pack200 now loaded dynamically in unpack() below // // For Sun, OpenJDK, IcedTea, etc, use this @@ -104,7 +106,6 @@ public class FileUtil { int files = 0; ZipFile zip = null; try { - byte buf[] = new byte[16*1024]; zip = new ZipFile(zipfile); Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { @@ -152,10 +153,7 @@ public class FileUtil { System.err.println("INFO: File [" + entry.getName() + "] extracted and unpacked"); } else { fos = new FileOutputStream(target); - int read = 0; - while ( (read = in.read(buf)) != -1) { - fos.write(buf, 0, read); - } + DataHelper.copy(in, fos); if (logLevel <= Log.INFO) System.err.println("INFO: File [" + entry.getName() + "] extracted"); } @@ -405,13 +403,10 @@ public class FileUtil { String rootDirStr = rootDir.getCanonicalPath(); if (!targetStr.startsWith(rootDirStr)) throw new FileNotFoundException("Requested file is outside the root dir: " + path); - byte buf[] = new byte[4*1024]; FileInputStream in = null; try { in = new FileInputStream(target); - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); + DataHelper.copy(in, out); try { out.close(); } catch (IOException ioe) {} } finally { if (in != null) @@ -448,17 +443,12 @@ public class FileUtil { if (!src.exists()) return false; if (dst.exists() && !overwriteExisting) return false; - byte buf[] = new byte[4096]; InputStream in = null; OutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dst); - - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); - + DataHelper.copy(in, out); return true; } catch (IOException ioe) { if (!quiet) diff --git a/core/java/src/net/i2p/util/NativeBigInteger.java b/core/java/src/net/i2p/util/NativeBigInteger.java index b867cf76f..2b435b286 100644 --- a/core/java/src/net/i2p/util/NativeBigInteger.java +++ b/core/java/src/net/i2p/util/NativeBigInteger.java @@ -1103,12 +1103,7 @@ public class NativeBigInteger extends BigInteger { InputStream libStream = resource.openStream(); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename); fos = new FileOutputStream(outFile); - byte buf[] = new byte[4096]; - while (true) { - int read = libStream.read(buf); - if (read < 0) break; - fos.write(buf, 0, read); - } + DataHelper.copy(libStream, fos); fos.close(); fos = null; System.load(outFile.getAbsolutePath()); //System.load requires an absolute path to the lib diff --git a/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java b/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java index c402d0c38..61dde7af7 100644 --- a/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java +++ b/router/java/src/net/i2p/router/networkdb/reseed/Reseeder.java @@ -210,10 +210,7 @@ public class Reseeder { tmp = new File(_context.getTempDir(), "manualreseeds-" + _context.random().nextInt() + (isSU3 ? ".su3" : ".zip")); out = new BufferedOutputStream(new SecureFileOutputStream(tmp)); out.write(magic); - byte buf[] = new byte[16*1024]; - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); + DataHelper.copy(in, out); out.close(); int[] stats; ReseedRunner reseedRunner = new ReseedRunner(); diff --git a/router/java/src/net/i2p/router/startup/WorkingDir.java b/router/java/src/net/i2p/router/startup/WorkingDir.java index 4a9a0e4a3..9d4fb5128 100644 --- a/router/java/src/net/i2p/router/startup/WorkingDir.java +++ b/router/java/src/net/i2p/router/startup/WorkingDir.java @@ -430,11 +430,7 @@ public class WorkingDir { try { in = new FileInputStream(src); out = new SecureFileOutputStream(dst); - - int read = 0; - while ( (read = in.read(buf)) != -1) - out.write(buf, 0, read); - + DataHelper.copy(in, out); System.err.println("Copied " + src.getPath()); } catch (IOException ioe) { System.err.println("FAILED copy " + src.getPath() + ": " + ioe);