private void saveNextChunkIfNeeded() throws IOException {
    if (myBufferPosition == myNextChunkBuffer.length) {
      int dataWrittenCount = 0;
      try (DataOutputStream stream = getChunkAppendStream()) {
        compress(stream, myNextChunkBuffer);
        dataWrittenCount = stream.getWrittenBytesCount();
      }

      try (DataOutputStream chunkLengthStream = getChunkLengthAppendStream()) {
        DataInputOutputUtil.writeINT(chunkLengthStream, dataWrittenCount);
      }

      assert dataWrittenCount <= MAX_PAGE_LENGTH; // we need to be in short range for chunk length table

      myBufferPosition = 0;
      initChunkLengthTable();

      myFileLength += dataWrittenCount;
      if (DO_DEBUG_SELF_CHECKS) myCompressedChunksFileOffsets.add(myFileLength);

      if (myChunkLengthTable.length == myChunkTableLength) {
        myChunkLengthTable = reallocShortTable(myChunkLengthTable);
      }

      myChunkLengthTable[myChunkTableLength++] = (short)dataWrittenCount;
      if (myChunkTableLength / CHUNKS_PER_SINGLE_OFFSET > myChunkOffsetTable.length) {
        long[] newChunkOffsetTable = new long[myChunkOffsetTable.length + 1];
        System.arraycopy(myChunkOffsetTable, 0, newChunkOffsetTable, 0, myChunkOffsetTable.length);
        newChunkOffsetTable[myChunkOffsetTable.length] = myFileLength;
        myChunkOffsetTable = newChunkOffsetTable;
      }

      byte[] bytes = new byte[myAppendBufferLength];
      System.arraycopy(myNextChunkBuffer, 0, bytes, 0, myAppendBufferLength);
      FileChunkReadCache.ourDecompressedCache.put(this, myChunkTableLength - 1, bytes);
    }
  }
private void saveNextChunkIfNeeded()