Clean up single char indexOf()

This commit is contained in:
zzz
2016-12-03 16:00:09 +00:00
parent f461d4881d
commit 42efed578a
16 changed files with 42 additions and 101 deletions

View File

@@ -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) {}
}

View File

@@ -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();

View File

@@ -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?

View File

@@ -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) {}

View File

@@ -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()) {

View File

@@ -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()) {

View File

@@ -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) {

View File

@@ -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

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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<? extends ZipEntry> 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)

View File

@@ -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

View File

@@ -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();

View File

@@ -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);