Findbugs all over #2

Mostly char encoding
Use StringWriter rather than OSW->BAOS->String
This commit is contained in:
zzz
2015-07-12 16:06:49 +00:00
parent 914cc120ad
commit 1f9bb046f5
25 changed files with 76 additions and 68 deletions

View File

@ -159,8 +159,13 @@ class AddressBook {
* @since 0.8.7
*/
public Iterator<Map.Entry<String, String>> iterator() {
if (this.subFile != null)
if (this.subFile != null) {
try {
return new ConfigIterator(this.subFile);
} catch (IOException ioe) {
return new ConfigIterator();
}
}
return this.addresses.entrySet().iterator();
}

View File

@ -54,11 +54,9 @@ class ConfigIterator implements Iterator<Map.Entry<String, String>> {
/**
* An iterator over the key/value pairs in the file.
*/
public ConfigIterator(File file) {
try {
public ConfigIterator(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
input = new BufferedReader(new InputStreamReader(fileStream));
} catch (IOException ioe) {}
input = new BufferedReader(new InputStreamReader(fileStream, "UTF-8"));
}
public boolean hasNext() {

View File

@ -116,7 +116,7 @@ class ConfigParser {
public static Map<String, String> parse(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream));
fileStream, "UTF-8"));
Map<String, String> rv = parse(input);
try {
fileStream.close();
@ -205,7 +205,7 @@ class ConfigParser {
public static List<String> parseSubscriptions(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(
fileStream));
fileStream, "UTF-8"));
List<String> rv = parseSubscriptions(input);
try {
fileStream.close();

View File

@ -23,8 +23,9 @@ package net.i2p.addressbook;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
/**
@ -56,8 +57,8 @@ class Log {
public void append(String entry) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(this.file,
true));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.file,
true), "UTF-8"));
String timestamp = new Date().toString();
bw.write(timestamp + " -- " + entry);
bw.newLine();

View File

@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map;
import net.i2p.data.Base64;
import net.i2p.data.DataHelper;
/**
* Holds different types that a bencoded byte array can represent.
@ -208,7 +209,7 @@ public class BEValue
} else if (bin) {
buf.append(bs.length).append(" bytes: ").append(Base64.encode(bs));
} else {
buf.append('"').append(new String(bs)).append('"');
buf.append('"').append(DataHelper.getUTF8(bs)).append('"');
}
valueString = buf.toString();
} else

View File

@ -15,6 +15,7 @@ import java.util.Locale;
import net.i2p.I2PAppContext;
import net.i2p.data.ByteArray;
import net.i2p.data.DataHelper;
import net.i2p.util.ByteCache;
import net.i2p.util.Log;
@ -145,7 +146,7 @@ class HTTPResponseOutputStream extends FilterOutputStream {
for (int i = 0; i < _headerBuffer.getValid(); i++) {
if (isNL(_headerBuffer.getData()[i])) {
if (lastEnd == -1) {
responseLine = new String(_headerBuffer.getData(), 0, i+1); // includes NL
responseLine = DataHelper.getUTF8(_headerBuffer.getData(), 0, i+1); // includes NL
responseLine = filterResponseLine(responseLine);
responseLine = (responseLine.trim() + "\r\n");
if (_log.shouldLog(Log.INFO))
@ -158,12 +159,12 @@ class HTTPResponseOutputStream extends FilterOutputStream {
int valLen = i-(j+1);
if ( (keyLen <= 0) || (valLen < 0) )
throw new IOException("Invalid header @ " + j);
String key = new String(_headerBuffer.getData(), lastEnd+1, keyLen);
String key = DataHelper.getUTF8(_headerBuffer.getData(), lastEnd+1, keyLen);
String val = null;
if (valLen == 0)
val = "";
else
val = new String(_headerBuffer.getData(), j+2, valLen).trim();
val = DataHelper.getUTF8(_headerBuffer.getData(), j+2, valLen).trim();
if (_log.shouldLog(Log.INFO))
_log.info("Response header [" + key + "] = [" + val + "]");

View File

@ -24,6 +24,7 @@ import net.i2p.I2PException;
import net.i2p.client.streaming.I2PSocket;
import net.i2p.client.streaming.I2PSocketOptions;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase;
import net.i2p.i2ptunnel.I2PTunnel;
@ -226,7 +227,7 @@ public class SOCKS5Server extends SOCKSServer {
}
byte addr[] = new byte[addrLen];
in.readFully(addr);
connHostName = new String(addr);
connHostName = DataHelper.getUTF8(addr);
}
_log.debug("DOMAINNAME address type in request: " + connHostName);
break;

View File

@ -1,6 +1,7 @@
package net.i2p.i2ptunnel.socks;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
/**
@ -65,7 +66,7 @@ public class SOCKSHeader {
int namelen = (this.header[4] & 0xff);
byte[] nameBytes = new byte[namelen];
System.arraycopy(nameBytes, 0, this.header, 5, namelen);
return new String(nameBytes);
return DataHelper.getUTF8(nameBytes);
}
public Destination getDestination() {

View File

@ -1,20 +1,19 @@
package net.i2p.router.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
public class ConfigKeyringHelper extends HelperBase {
public ConfigKeyringHelper() {}
public String getSummary() {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4*1024);
StringWriter sw = new StringWriter(4*1024);
try {
_context.keyRing().renderStatusHTML(new OutputStreamWriter(baos));
_context.keyRing().renderStatusHTML(sw);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return new String(baos.toByteArray());
return sw.toString();
}
}

View File

@ -1,20 +1,19 @@
package net.i2p.router.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
public class ConfigPeerHelper extends HelperBase {
public ConfigPeerHelper() {}
public String getBlocklistSummary() {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4*1024);
StringWriter sw = new StringWriter(4*1024);
try {
_context.blocklist().renderStatusHTML(new OutputStreamWriter(baos));
_context.blocklist().renderStatusHTML(sw);
} catch (IOException ioe) {
ioe.printStackTrace();
}
return new String(baos.toByteArray());
return sw.toString();
}
}

View File

@ -1,8 +1,7 @@
package net.i2p.router.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Serializable;
import java.io.Writer;
import java.util.ArrayList;
@ -25,9 +24,9 @@ public class JobQueueHelper extends HelperBase {
renderStatusHTML(_out);
return "";
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream(32*1024);
renderStatusHTML(new OutputStreamWriter(baos));
return new String(baos.toByteArray());
StringWriter sw = new StringWriter(32*1024);
renderStatusHTML(sw);
return sw.toString();
}
} catch (IOException ioe) {
ioe.printStackTrace();

View File

@ -1,8 +1,7 @@
package net.i2p.router.web;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
public class TunnelHelper extends HelperBase {
@ -15,9 +14,9 @@ public class TunnelHelper extends HelperBase {
renderer.renderStatusHTML(_out);
return "";
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream(32*1024);
renderer.renderStatusHTML(new OutputStreamWriter(baos));
return new String(baos.toByteArray());
StringWriter sw = new StringWriter(32*1024);
renderer.renderStatusHTML(sw);
return sw.toString();
}
} catch (IOException ioe) {
ioe.printStackTrace();

View File

@ -94,7 +94,7 @@ public class SAMReader {
_log.error("Error reading from SAM", ioe);
}
String line = new String(baos.toByteArray());
String line = DataHelper.getUTF8(baos.toByteArray());
baos.reset();
if (line == null) {

View File

@ -180,7 +180,7 @@ public class SAMStreamSend {
byte dest[] = new byte[1024];
int read = DataHelper.read(fin, dest);
_remoteDestination = new String(dest, 0, read);
_remoteDestination = DataHelper.getUTF8(dest, 0, read);
synchronized (_remotePeers) {
_connectionId = _remotePeers.size() + 1;
_remotePeers.put(Integer.valueOf(_connectionId), Sender.this);

View File

@ -23,6 +23,8 @@
*/
package i2p.susi.util;
import net.i2p.data.DataHelper;
/**
* @author susi
*/
@ -39,6 +41,6 @@ public class ReadBuffer {
public String toString()
{
return content != null ? new String( content, offset, length ) : "";
return content != null ? DataHelper.getUTF8(content, offset, length) : "";
}
}

View File

@ -1735,6 +1735,7 @@
<arg value="apps/BOB/src/:apps/addressbook/java/src/:apps/i2psnark/java/src/:apps/i2ptunnel/java/src/:apps/ministreaming/java/src/:apps/routerconsole/java/src/:apps/sam/java/src/:apps/streaming/java/src/:apps/susidns/src/java/src/:apps/susimail/src/src/:apps/systray/java/src/:core/java/src/:router/java/src/:installer/java/src"/>
<!-- start of the files to be analyzed -->
<arg value="build/BOB.jar"/>
<arg value="build/addressbook.jar"/>
<arg value="build/addressbook.war"/>
<arg value="build/i2p.jar"/>
<arg value="build/i2psnark.jar"/>

View File

@ -15,12 +15,12 @@ import net.i2p.crypto.eddsa.spec.EdDSAPrivateKeySpec;
*/
public class EdDSAPrivateKey implements EdDSAKey, PrivateKey {
private static final long serialVersionUID = 23495873459878957L;
private transient final byte[] seed;
private transient final byte[] h;
private transient final byte[] a;
private transient final GroupElement A;
private transient final byte[] Abyte;
private transient final EdDSAParameterSpec edDsaSpec;
private final byte[] seed;
private final byte[] h;
private final byte[] a;
private final GroupElement A;
private final byte[] Abyte;
private final EdDSAParameterSpec edDsaSpec;
public EdDSAPrivateKey(EdDSAPrivateKeySpec spec) {
this.seed = spec.getSeed();

View File

@ -101,8 +101,8 @@ public class Base32 {
}
private static byte[] read(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
byte buf[] = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
byte buf[] = new byte[64];
while (true) {
int read = in.read(buf);
if (read < 0) break;
@ -118,7 +118,7 @@ public class Base32 {
}
private static void decode(InputStream in, OutputStream out) throws IOException {
byte decoded[] = decode(new String(read(in)));
byte decoded[] = decode(DataHelper.getUTF8(read(in)));
if (decoded == null) {
System.out.println("FAIL");
return;
@ -199,7 +199,7 @@ public class Base32 {
byte[] b = decode(s);
if (b == null)
return null;
return new String(b);
return DataHelper.getUTF8(b);
}
/**

View File

@ -246,8 +246,8 @@ public class Base64 {
}
private static byte[] read(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
byte buf[] = new byte[4096];
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
byte buf[] = new byte[1024];
while (true) {
int read = in.read(buf);
if (read < 0) break;
@ -263,7 +263,7 @@ public class Base64 {
}
private static void decode(InputStream in, OutputStream out) throws IOException {
byte decoded[] = decode(new String(read(in)));
byte decoded[] = decode(DataHelper.getUTF8(read(in)));
if (decoded == null)
throw new IOException("Invalid base 64 string");
out.write(decoded);

View File

@ -262,7 +262,7 @@ public class Certificate extends DataStructureImpl {
} else {
buf.append(" payload size: ").append(_payload.length);
if (getCertificateType() == CERTIFICATE_TYPE_HASHCASH) {
buf.append(" Stamp: ").append(new String(_payload));
buf.append(" Stamp: ").append(DataHelper.getUTF8(_payload));
} else if (getCertificateType() == CERTIFICATE_TYPE_SIGNED && _payload.length == CERTIFICATE_LENGTH_SIGNED_WITH_HASH) {
buf.append(" Signed by hash: ").append(Base64.encode(_payload, Signature.SIGNATURE_BYTES, Hash.HASH_LENGTH));
} else {

View File

@ -1864,7 +1864,6 @@ public class DataHelper {
*
* @return null if orig is null
* @throws RuntimeException
* @deprecated unused
*/
public static String getUTF8(byte orig[], int offset, int len) {
if (orig == null) return null;

View File

@ -87,7 +87,7 @@ public class VerifiedDestination extends Destination {
* zeros and see if it meets our minimum effort.
*/
protected boolean verifyHashCashCert() {
String hcs = new String(_certificate.getPayload());
String hcs = DataHelper.getUTF8(_certificate.getPayload());
int end1 = 0;
for (int i = 0; i < 3; i++) {
end1 = 1 + hcs.indexOf(':', end1);

View File

@ -9,12 +9,13 @@ package net.i2p.util;
*
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
/**
* Render a log record according to the log manager's settings
@ -71,16 +72,11 @@ class LogRecordFormatter {
}
buf.append(NL);
if (rec.getThrowable() != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
PrintWriter pw = new PrintWriter(baos, true);
StringWriter sw = new StringWriter(512);
PrintWriter pw = new PrintWriter(sw, true);
rec.getThrowable().printStackTrace(pw);
try {
pw.flush();
baos.flush();
} catch (IOException ioe) { // nop
}
byte tb[] = baos.toByteArray();
buf.append(new String(tb));
sw.flush();
buf.append(sw.toString());
}
return buf.toString();
}

View File

@ -572,7 +572,7 @@ public class Reseeder {
System.err.println("Reseed got no router infos from " + seedURL);
return 0;
}
String content = new String(contentRaw);
String content = DataHelper.getUTF8(contentRaw);
// This isn't really URLs, but Base64 hashes
// but they may include % encoding
Set<String> urls = new HashSet<String>(1024);

View File

@ -442,7 +442,13 @@ class NtpMessage {
// or stratum-1 (primary) servers, this is a four-character ASCII
// string, left justified and zero padded to 32 bits.
if(stratum==0 || stratum==1) {
return new String(ref);
StringBuilder buf = new StringBuilder(4);
for (int i = 0; i < 4; i++) {
if (ref[i] == 0)
break;
buf.append((char) (ref[i] & 0xff));
}
return buf.toString();
}
// In NTP Version 3 secondary servers, this is the 32-bit IPv4