code: public static byte[] serializeToken(final Token<AMRMTokenIdentifier> token) {
      try (final DataOutputBuffer dob = new DataOutputBuffer()) {
        final Credentials credentials = new Credentials();
        credentials.addToken(token.getService(), token);
        credentials.writeTokenStorageToStream(dob);
        return dob.getData();
      } catch (final IOException ex) {
        LOG.log(Level.SEVERE, "Could not write credentials to the buffer.", ex);
        throw new RuntimeException(ex);
      }
    }
docs: Helper method to serialize a security token.
  @param token AM security token.
  @return ByteBuffer that contains the token. It is compatible with addTokens() method.