java/tsfile/src/main/java/org/apache/tsfile/write/page/PageWriter.java [238:308]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ByteBuffer pageData = getUncompressedBytes();
    int uncompressedSize = pageData.remaining();
    int compressedSize;
    byte[] compressedBytes = null;

    if (compressor.getType().equals(CompressionType.UNCOMPRESSED)) {
      compressedSize = uncompressedSize;
    } else if (compressor.getType().equals(CompressionType.GZIP)) {
      compressedBytes =
          compressor.compress(pageData.array(), pageData.position(), uncompressedSize);
      compressedSize = compressedBytes.length;
    } else {
      compressedBytes = new byte[compressor.getMaxBytesForCompression(uncompressedSize)];
      // data is never a directByteBuffer now, so we can use data.array()
      compressedSize =
          compressor.compress(
              pageData.array(), pageData.position(), uncompressedSize, compressedBytes);
    }

    // write the page header to IOWriter
    int sizeWithoutStatistic = 0;
    if (first) {
      sizeWithoutStatistic +=
          ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer);
      sizeWithoutStatistic +=
          ReadWriteForEncodingUtils.writeUnsignedVarInt(compressedSize, pageBuffer);
    } else {
      ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer);
      ReadWriteForEncodingUtils.writeUnsignedVarInt(compressedSize, pageBuffer);
      statistics.serialize(pageBuffer);
    }

    IEncryptor encryptor = IEncryptor.getEncryptor(encryptParam);

    // write page content to temp PBAOS
    logger.trace("start to flush a page data into buffer, buffer position {} ", pageBuffer.size());
    if (compressor.getType().equals(CompressionType.UNCOMPRESSED)) {
      if (encryptor.getEncryptionType().equals(EncryptionType.UNENCRYPTED)) {
        try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) {
          channel.write(pageData);
        }
      } else {
        byte[] encryptedBytes = null;
        encryptedBytes = encryptor.encrypt(pageData.array(), pageData.position(), uncompressedSize);
        // data is never a directByteBuffer now, so we can use data.array()
        int encryptedSize = encryptedBytes.length;
        pageBuffer.write(encryptedBytes, 0, encryptedSize);
      }

    } else {
      if (encryptor.getEncryptionType().equals(EncryptionType.UNENCRYPTED)) {
        pageBuffer.write(compressedBytes, 0, compressedSize);
      } else {
        byte[] encryptedBytes = null;
        encryptedBytes = encryptor.encrypt(compressedBytes, 0, compressedSize);
        // data is never a directByteBuffer now, so we can use data.array()
        int encryptedSize = encryptedBytes.length;
        pageBuffer.write(encryptedBytes, 0, encryptedSize);
      }
    }
    logger.trace("start to flush a page data into buffer, buffer position {} ", pageBuffer.size());
    return sizeWithoutStatistic;
  }

  /**
   * calculate max possible memory size it occupies, including time outputStream and value
   * outputStream, because size outputStream is never used until flushing.
   *
   * @return allocated size in time, value and outputStream
   */
  public long estimateMaxMemSize() {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



java/tsfile/src/main/java/org/apache/tsfile/write/page/ValuePageWriter.java [282:350]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    ByteBuffer pageData = getUncompressedBytes();
    int uncompressedSize = pageData.remaining();
    int compressedSize;
    byte[] compressedBytes = null;

    if (compressor.getType().equals(CompressionType.UNCOMPRESSED)) {
      compressedSize = uncompressedSize;
    } else if (compressor.getType().equals(CompressionType.GZIP)) {
      compressedBytes =
          compressor.compress(pageData.array(), pageData.position(), uncompressedSize);
      compressedSize = compressedBytes.length;
    } else {
      compressedBytes = new byte[compressor.getMaxBytesForCompression(uncompressedSize)];
      // data is never a directByteBuffer now, so we can use data.array()
      compressedSize =
          compressor.compress(
              pageData.array(), pageData.position(), uncompressedSize, compressedBytes);
    }

    // write the page header to IOWriter
    int sizeWithoutStatistic = 0;
    if (first) {
      sizeWithoutStatistic +=
          ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer);
      sizeWithoutStatistic +=
          ReadWriteForEncodingUtils.writeUnsignedVarInt(compressedSize, pageBuffer);
    } else {
      ReadWriteForEncodingUtils.writeUnsignedVarInt(uncompressedSize, pageBuffer);
      ReadWriteForEncodingUtils.writeUnsignedVarInt(compressedSize, pageBuffer);
      statistics.serialize(pageBuffer);
    }
    IEncryptor encryptor = IEncryptor.getEncryptor(encryptParam);
    // write page content to temp PBAOS
    logger.trace("start to flush a page data into buffer, buffer position {} ", pageBuffer.size());
    if (compressor.getType().equals(CompressionType.UNCOMPRESSED)) {
      if (encryptor.getEncryptionType().equals(EncryptionType.UNENCRYPTED)) {
        try (WritableByteChannel channel = Channels.newChannel(pageBuffer)) {
          channel.write(pageData);
        }
      } else {
        byte[] encryptedBytes = null;
        encryptedBytes = encryptor.encrypt(pageData.array(), pageData.position(), uncompressedSize);
        // data is never a directByteBuffer now, so we can use data.array()
        int encryptedSize = encryptedBytes.length;
        pageBuffer.write(encryptedBytes, 0, encryptedSize);
      }

    } else {
      if (encryptor.getEncryptionType().equals(EncryptionType.UNENCRYPTED)) {
        pageBuffer.write(compressedBytes, 0, compressedSize);
      } else {
        byte[] encryptedBytes = null;
        encryptedBytes = encryptor.encrypt(compressedBytes, 0, compressedSize);
        // data is never a directByteBuffer now, so we can use data.array()
        int encryptedSize = encryptedBytes.length;
        pageBuffer.write(encryptedBytes, 0, encryptedSize);
      }
    }
    logger.trace("start to flush a page data into buffer, buffer position {} ", pageBuffer.size());
    return sizeWithoutStatistic;
  }

  /**
   * calculate max possible memory size it occupies, including time outputStream and value
   * outputStream, because size outputStream is never used until flushing.
   *
   * @return allocated size in time, value and outputStream
   */
  public long estimateMaxMemSize() {
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



