public void serializeTokens(List<ReplicaTokenInfo> tokenInfoList, OutputStream outputStream) throws IOException {
      CrcOutputStream crcOutputStream = new CrcOutputStream(outputStream);
      DataOutputStream writer = new DataOutputStream(crcOutputStream);
      try {
        // write the current version
        writer.writeShort(CURRENT_VERSION);
        for (ReplicaTokenInfo replicaTokenInfo : tokenInfoList) {
          writer.write(replicaTokenInfo.getPartitionId().getBytes());
          // Write hostname
          writer.writeInt(replicaTokenInfo.getHostname().getBytes().length);
          writer.write(replicaTokenInfo.getHostname().getBytes());
          // Write replica path
          writer.writeInt(replicaTokenInfo.getReplicaPath().getBytes().length);
          writer.write(replicaTokenInfo.getReplicaPath().getBytes());
          // Write port
          writer.writeInt(replicaTokenInfo.getPort());
          // Write total bytes read from local store
          writer.writeLong(replicaTokenInfo.getTotalBytesReadFromLocalStore());
          // Write replica type
          writer.writeShort((short) replicaTokenInfo.getReplicaInfo().getReplicaId().getReplicaType().ordinal());
          // Write replica token
          writer.write(replicaTokenInfo.getReplicaToken().toBytes());
        }
        long crcValue = crcOutputStream.getValue();
        writer.writeLong(crcValue);
      } catch (IOException e) {
        logger.error("IO error while serializing replica tokens");
        throw e;
      } finally {
        if (outputStream instanceof FileOutputStream) {
          // flush and overwrite file
          ((FileOutputStream) outputStream).getChannel().force(true);
        }
        writer.close();
      }
    }
Class to serialize and deserialize replica tokens
ambry-replication/src/main/java/com/github/ambry/replication/ReplicaTokenPersistor.java