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

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