public Object readObject(Connection connection) {
   readBuffer.flip();
   try {
     try {
       Object object = serialization.read(connection, readBuffer);
       if (readBuffer.hasRemaining())
         throw new DiDiNetException(
             "Incorrect number of bytes ("
                 + readBuffer.remaining()
                 + " remaining) used to deserialize object: "
                 + object);
       return object;
     } catch (Exception ex) {
       throw new DiDiNetException("Error during deserialization.", ex);
     }
   } finally {
     readBuffer.clear();
   }
 }
  /** This method is thread safe. */
  public int send(Connection connection, Object json, SocketAddress address) throws IOException {
    DatagramChannel datagramChannel = this.datagramChannel;
    if (datagramChannel == null) throw new SocketException("Connection is closed.");
    synchronized (writeLock) {
      try {
        try {
          serialization.write(connection, writeBuffer, json);
        } catch (Exception ex) {
          throw new DiDiNetException("Error serializing object of type: " + json, ex);
        }
        writeBuffer.flip();
        int length = writeBuffer.limit();
        datagramChannel.send(writeBuffer, address);

        lastCommunicationTime = System.currentTimeMillis();

        boolean wasFullWrite = !writeBuffer.hasRemaining();
        return wasFullWrite ? length : -1;
      } finally {
        writeBuffer.clear();
      }
    }
  }