/**
   * {@inheritDoc}
   *
   * @see net.sf.hajdbc.distributed.Stateful#readState(java.io.ObjectInput)
   */
  @Override
  public void readState(ObjectInput input) throws IOException, ClassNotFoundException {
    if (input.available() > 0) {
      Set<String> databases = new TreeSet<String>();

      int size = input.readInt();

      for (int i = 0; i < size; ++i) {
        databases.add(input.readUTF());
      }

      this.logger.info(
          Messages.INITIAL_CLUSTER_STATE_REMOTE.getMessage(
              databases, this.dispatcher.getCoordinator()));

      this.stateManager.setActiveDatabases(databases);
    }
  }
  @Override
  public Document readObject(ObjectInput input) throws IOException {
    if (input.available() < 5) {
      // There's not enough data in the input, so return null
      return null;
    }
    // Read the type byte ...
    int type = input.readByte();
    assert type == 1;
    // Read the number of bytes ...
    int length = input.readInt();
    if (length == 0) {
      return new BasicDocument();
    }

    // Otherwise there's data ...
    byte[] bytes = new byte[length];
    // Read the BSON bytes...
    input.readFully(bytes);
    // Convert to a document ...
    return Bson.read(new ByteArrayInputStream(bytes));
  }