Author: j16sdiz
Date: 2008-05-27 07:01:28 +0000 (Tue, 27 May 2008)
New Revision: 20099
Modified:
trunk/freenet/src/freenet/io/comm/Peer.java
trunk/freenet/src/freenet/support/Serializer.java
Log:
Optimization:
- Peer implements WritableToDataOutputStream
--> remove one if() in writeToDataOutputStream
- writeToDataOutputStream sort from most common to least
Modified: trunk/freenet/src/freenet/io/comm/Peer.java
===================================================================
--- trunk/freenet/src/freenet/io/comm/Peer.java 2008-05-26 09:35:02 UTC (rev
20098)
+++ trunk/freenet/src/freenet/io/comm/Peer.java 2008-05-27 07:01:28 UTC (rev
20099)
@@ -20,12 +20,12 @@
package freenet.io.comm;
import java.io.DataInput;
-import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import freenet.io.WritableToDataOutputStream;
import freenet.support.transport.ip.HostnameSyntaxException;
import freenet.support.transport.ip.IPUtil;
@@ -35,7 +35,7 @@
* To change the template for this generated type comment go to Window -
Preferences - Java - Code Generation - Code and
* Comments
*/
-public class Peer {
+public class Peer implements WritableToDataOutputStream {
public static class LocalAddressException extends Exception {
private static final long serialVersionUID = -1;
Modified: trunk/freenet/src/freenet/support/Serializer.java
===================================================================
--- trunk/freenet/src/freenet/support/Serializer.java 2008-05-26 09:35:02 UTC
(rev 20098)
+++ trunk/freenet/src/freenet/support/Serializer.java 2008-05-27 07:01:28 UTC
(rev 20099)
@@ -63,7 +63,7 @@
throw new IOException("Boolean is non boolean value:
"+bool);
} else if (type.equals(Byte.class)) {
int b = dis.readByte();
- return new Byte((byte)b);
+ return new Byte((byte) b);
} else if (type.equals(Short.class)) {
return new Short(dis.readShort());
} else if (type.equals(Integer.class)) {
@@ -102,20 +102,21 @@
}
}
- public static void writeToDataOutputStream(Object object,
DataOutputStream dos, PeerContext ctx) throws IOException {
+ public static void writeToDataOutputStream(Object object,
DataOutputStream dos, PeerContext ctx) throws IOException {
Class type = object.getClass();
- if (type.equals(Boolean.class)) {
+ if (type.equals(Long.class)) {
+ dos.writeLong(((Long) object).longValue());
+ } else if (type.equals(Boolean.class)) {
dos.write(((Boolean) object).booleanValue() ? 1 : 0);
- } else if (type.equals(Byte.class)) {
- dos.write(((Byte) object).byteValue());
- } else if (type.equals(Short.class)) {
- dos.writeShort(((Short) object).shortValue());
} else if (type.equals(Integer.class)) {
dos.writeInt(((Integer) object).intValue());
- } else if (type.equals(Long.class)) {
- dos.writeLong(((Long) object).longValue());
+ } else if (type.equals(Short.class)) {
+ dos.writeShort(((Short) object).shortValue());
} else if (type.equals(Double.class)) {
dos.writeDouble(((Double) object).doubleValue());
+ } else if
(WritableToDataOutputStream.class.isAssignableFrom(type)) {
+ WritableToDataOutputStream b =
(WritableToDataOutputStream) object;
+ b.writeToDataOutputStream(dos);
} else if (type.equals(String.class)) {
String s = (String) object;
dos.writeInt(s.length());
@@ -130,11 +131,8 @@
writeToDataOutputStream(i.next(), dos,
ctx);
}
}
- } else if (type.equals(Peer.class)) {
- ((Peer)object).writeToDataOutputStream(dos);
- } else if
(WritableToDataOutputStream.class.isAssignableFrom(type)) {
- WritableToDataOutputStream b =
(WritableToDataOutputStream) object;
- b.writeToDataOutputStream(dos);
+ } else if (type.equals(Byte.class)) {
+ dos.write(((Byte) object).byteValue());
} else {
throw new RuntimeException("Unrecognised field type: "
+ type);
}