diff --git a/src/com/google/bitcoin/core/BitcoinSerializer.java b/src/com/google/bitcoin/core/BitcoinSerializer.java index 96f9f25b..8ab4505c 100644 --- a/src/com/google/bitcoin/core/BitcoinSerializer.java +++ b/src/com/google/bitcoin/core/BitcoinSerializer.java @@ -51,8 +51,6 @@ public class BitcoinSerializer private boolean usesChecksumming; private static Map, String> names = new HashMap,String>(); - private static Map> - messageConstructors = new HashMap>(); static { names.put(VersionMessage.class, "version"); @@ -75,14 +73,6 @@ public class BitcoinSerializer public BitcoinSerializer(NetworkParameters params, boolean usesChecksumming) { this.params = params; this.usesChecksumming = usesChecksumming; - - // some Message subclasses can only be sent for now, ignore missing constructors - for (Class c : names.keySet()) { - Constructor ct = makeConstructor(c); - if (ct != null) { - messageConstructors.put(names.get(c),ct); - } - } } public void useChecksumming(boolean usesChecksumming) { @@ -217,17 +207,36 @@ public class BitcoinSerializer } try { - Constructor c = messageConstructors.get(command); - if (c == null) { - throw new ProtocolException("No support for deserializing message with name " + command); - } - return c.newInstance(params, payloadBytes); + return makeMessage(command, payloadBytes); } catch (Exception e) { throw new ProtocolException("Error deserializing message " + Utils.bytesToHexString(payloadBytes) + "\n", e); } } + private Message makeMessage(String command, byte[] payloadBytes) throws ProtocolException { + // We use an if ladder rather than reflection because reflection is very slow on Android. + if (command.equals("version")) { + return new VersionMessage(params, payloadBytes); + } else if (command.equals("inv")) { + return new InventoryMessage(params, payloadBytes); + } else if (command.equals("block")) { + return new Block(params, payloadBytes); + } else if (command.equals("getdata")) { + return new GetDataMessage(params, payloadBytes); + } else if (command.equals("tx")) { + return new Transaction(params, payloadBytes); + } else if (command.equals("addr")) { + return new AddressMessage(params, payloadBytes); + } else if (command.equals("ping")) { + return new Ping(); + } else if (command.equals("verack")) { + return new VersionAck(params, payloadBytes); + } else { + throw new ProtocolException("No support for deserializing message with name " + command); + } + } + private Constructor makeConstructor(Class c) { Class parTypes[] = new Class[2]; parTypes[0] = NetworkParameters.class;