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

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