Findbugs all over #4

char encoding
This commit is contained in:
zzz
2015-07-12 19:19:32 +00:00
parent d087fd674b
commit 1ed1e4414b
10 changed files with 47 additions and 31 deletions

View File

@ -45,11 +45,11 @@
</p><p>
<%
if (ERROR_THROWABLE != null) {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(2048);
java.io.PrintStream ps = new java.io.PrintStream(baos);
ERROR_THROWABLE.printStackTrace(ps);
ps.close();
String trace = baos.toString();
java.io.StringWriter sw = new java.io.StringWriter(2048);
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
ERROR_THROWABLE.printStackTrace(pw);
pw.flush();
String trace = sw.toString();
trace = trace.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
trace = trace.replace("\n", "<br>&nbsp;&nbsp;&nbsp;&nbsp;\n");
out.print(trace);

View File

@ -28,6 +28,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.i2p.I2PAppContext;
import net.i2p.data.DataFormatException;
import net.i2p.data.DataHelper;
import net.i2p.data.Destination;
import net.i2p.util.FileUtil;
import net.i2p.util.Log;
@ -235,7 +236,7 @@ public class SingleFileNamingService extends NamingService {
// FIXME fails if previous last line didn't have a trailing \n
out.write(hostname.getBytes("UTF-8"));
out.write('=');
out.write(d.toBase64().getBytes());
out.write(DataHelper.getASCII(d.toBase64()));
out.write('\n');
out.close();
for (NamingServiceListener nsl : _listeners) {

View File

@ -184,7 +184,7 @@ public class RateStat {
buf.append("# Rate: ").append(_groupName).append(": ").append(_statName).append(NL);
buf.append("# ").append(_description).append(NL);
buf.append("# ").append(NL).append(NL);
out.write(buf.toString().getBytes());
out.write(buf.toString().getBytes("UTF-8"));
buf.setLength(0);
for (Rate r: _rates){
buf.append("#######").append(NL);
@ -193,7 +193,7 @@ public class RateStat {
buf.append(NL);
String curPrefix = prefix + "." + DataHelper.formatDuration(r.getPeriod());
r.store(curPrefix, buf);
out.write(buf.toString().getBytes());
out.write(buf.toString().getBytes("UTF-8"));
buf.setLength(0);
}
}

View File

@ -13,6 +13,8 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import net.i2p.data.DataHelper;
/**
* Hexdump class (well, it's actually a namespace with some functions,
* but let's stick with java terminology :-). These methods generate
@ -25,7 +27,7 @@ public class HexDump {
private static final int FORMAT_OFFSET_PADDING = 8;
private static final int FORMAT_BYTES_PER_ROW = 16;
private static final byte[] HEXCHARS = "0123456789abcdef".getBytes();
private static final byte[] HEXCHARS = DataHelper.getASCII("0123456789abcdef");
/**
* Dump a byte array in a String.
@ -37,11 +39,10 @@ public class HexDump {
try {
dump(data, 0, data.length, out);
return out.toString("ISO-8859-1");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("no 8859?", e);
}
return out.toString();
}
/**
@ -56,11 +57,10 @@ public class HexDump {
try {
dump(data, off, len, out);
return out.toString("ISO-8859-1");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("no 8859?", e);
}
return out.toString();
}
/**
@ -91,9 +91,10 @@ public class HexDump {
hexoff = Integer.toString(dumpoff, 16);
hexofflen = hexoff.length();
for (i = 0; i < FORMAT_OFFSET_PADDING - hexofflen; ++i) {
hexoff = "0" + hexoff;
out.write('0');
}
out.write((hexoff + " ").getBytes());
out.write(DataHelper.getASCII(hexoff));
out.write(' ');
// Bytes to be printed in the current line
nextbytes = (FORMAT_BYTES_PER_ROW < (end - dumpoff) ? FORMAT_BYTES_PER_ROW : (end - dumpoff));
@ -101,15 +102,15 @@ public class HexDump {
for (i = 0; i < FORMAT_BYTES_PER_ROW; ++i) {
// Put two spaces to separate 8-bytes blocks
if ((i % 8) == 0) {
out.write(" ".getBytes());
out.write(' ');
}
if (i >= nextbytes) {
out.write(" ".getBytes());
out.write(DataHelper.getASCII(" "));
} else {
val = data[dumpoff + i] & 0xff;
out.write(HEXCHARS[val >>> 4]);
out.write(HEXCHARS[val & 0xf]);
out.write(" ".getBytes());
out.write(' ');
}
}
@ -117,19 +118,32 @@ public class HexDump {
for (i = 0; i < FORMAT_BYTES_PER_ROW; ++i) {
if (i >= nextbytes) {
out.write(" ".getBytes());
out.write(' ');
} else {
val = data[i + dumpoff];
// Is it a printable character?
if ((val > 31) && (val < 127)) {
out.write(val);
} else {
out.write(".".getBytes());
out.write('.');
}
}
}
out.write("|\n".getBytes());
out.write('|');
out.write('\n');
}
}
/**
* @since 0.9.21
*/
/****
public static void main(String[] args) {
byte[] b = new byte[9993];
RandomSource.getInstance().nextBytes(b);
System.out.println(dump(b));
System.out.println(dump("test test test abcde xyz !!!".getBytes()));
}
****/
}

View File

@ -73,9 +73,9 @@ class LogRecordFormatter {
buf.append(NL);
if (rec.getThrowable() != null) {
StringWriter sw = new StringWriter(512);
PrintWriter pw = new PrintWriter(sw, true);
PrintWriter pw = new PrintWriter(sw);
rec.getThrowable().printStackTrace(pw);
sw.flush();
pw.flush();
buf.append(sw.toString());
}
return buf.toString();

View File

@ -228,7 +228,7 @@ public class DBHistory {
add(buf, "unpromptedDbStoreOld", _unpromptedDbStoreOld, "How times have they sent us something we didn't ask for but have seen before?");
add(buf, "lastLookupReceived", _lastLookupReceived, "When was the last time they send us a lookup? (milliseconds since the epoch)");
add(buf, "avgDelayBetweenLookupsReceived", _avgDelayBetweenLookupsReceived, "How long is it typically between each db lookup they send us? (in milliseconds)");
out.write(buf.toString().getBytes());
out.write(buf.toString().getBytes("UTF-8"));
_failedLookupRate.store(out, "dbHistory.failedLookupRate");
_invalidReplyRate.store(out, "dbHistory.invalidReplyRate");
}

View File

@ -139,7 +139,7 @@ class ProfilePersistenceHelper {
add(buf, "tunnelPeakTunnel1mThroughput", profile.getPeakTunnel1mThroughputKBps(), "KBytes/sec");
buf.append(NL);
out.write(buf.toString().getBytes());
out.write(buf.toString().getBytes("UTF-8"));
if (profile.getIsExpanded()) {
// only write out expanded data if, uh, we've got it

View File

@ -151,7 +151,7 @@ public class TunnelHistory {
add(buf, "lifetimeAgreedTo", _lifetimeAgreedTo.get(), "How many tunnels has the peer ever agreed to participate in?");
add(buf, "lifetimeFailed", _lifetimeFailed.get(), "How many tunnels has the peer ever agreed to participate in that failed prematurely?");
add(buf, "lifetimeRejected", _lifetimeRejected.get(), "How many tunnels has the peer ever refused to participate in?");
out.write(buf.toString().getBytes());
out.write(buf.toString().getBytes("UTF-8"));
_rejectRate.store(out, "tunnelHistory.rejectRate");
_failRate.store(out, "tunnelHistory.failRate");
}

View File

@ -252,7 +252,7 @@ public class WorkingDir {
}
System.setProperty(PROP_WRAPPER_LOG, logfile.getAbsolutePath());
try {
PrintStream ps = new PrintStream(new SecureFileOutputStream(logfile, true));
PrintStream ps = new PrintStream(new SecureFileOutputStream(logfile, true), true, "UTF-8");
System.setOut(ps);
System.setErr(ps);
} catch (IOException ioe) {

View File

@ -12,6 +12,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import net.i2p.data.DataHelper;
import net.i2p.router.Router;
import net.i2p.util.Log;
import net.i2p.util.SecureFileOutputStream;
@ -45,7 +46,7 @@ public class MarkLiveliness implements SimpleTimer.TimedEvent {
FileOutputStream fos = null;
try {
fos = new SecureFileOutputStream(_pingFile);
fos.write(Long.toString(System.currentTimeMillis()).getBytes());
fos.write(DataHelper.getASCII(Long.toString(System.currentTimeMillis())));
} catch (IOException ioe) {
if (!_errorLogged) {
Log log = _router.getContext().logManager().getLog(MarkLiveliness.class);