public static Packet deencapsulatePacket(ByteBuf buffer) throws IOException {
   String clazzName = (String) Packet.decodePrimitive(buffer);
   try {
     if (clazzName == null) throw new IOException("No class specified!");
     Class<? extends Packet> clazz = (Class<? extends Packet>) Class.forName(clazzName);
     Packet packet = clazz.newInstance();
     packet.readPacket(buffer.slice());
     return packet;
   } catch (Exception ex) {
     if (ex instanceof IOException) throw (IOException) ex;
     throw new IOException("Decoding exception", ex);
   }
 }
 /**
  * Instruct the client that the given session has received a message with size # of bytes.
  *
  * @param session session to notify
  * @param msgId message number available
  * @param size size of the message
  */
 public void messageAvailable(
     I2PSession session, int msgId, long size, int proto, int fromPort, int toPort) {
   byte data[] = null;
   try {
     data = session.receiveMessage(msgId);
   } catch (I2PSessionException ise) {
     _context.statManager().addRateData("stream.packetReceiveFailure", 1);
     if (_log.shouldLog(Log.WARN)) _log.warn("Error receiving the message", ise);
     return;
   }
   if (data == null) return;
   Packet packet = new Packet();
   try {
     packet.readPacket(data, 0, data.length);
     packet.setRemotePort(fromPort);
     packet.setLocalPort(toPort);
     _manager.getPacketHandler().receivePacket(packet);
   } catch (IllegalArgumentException iae) {
     _context.statManager().addRateData("stream.packetReceiveFailure", 1);
     if (_log.shouldLog(Log.WARN)) _log.warn("Received an invalid packet", iae);
   }
 }