content: /*
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
   ~                                                                               ~
   ~ The MIT License (MIT)                                                         ~
   ~                                                                               ~
   ~ Copyright (c) 2015-2025 miaixz.org sandao and other contributors.             ~
   ~                                                                               ~
   ~ Permission is hereby granted, free of charge, to any person obtaining a copy  ~
   ~ of this software and associated documentation files (the "Software"), to deal ~
   ~ in the Software without restriction, including without limitation the rights  ~
   ~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell     ~
   ~ copies of the Software, and to permit persons to whom the Software is         ~
   ~ furnished to do so, subject to the following conditions:                      ~
   ~                                                                               ~
   ~ The above copyright notice and this permission notice shall be included in    ~
   ~ all copies or substantial portions of the Software.                           ~
   ~                                                                               ~
   ~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR    ~
   ~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,      ~
   ~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE   ~
   ~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER        ~
   ~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ~
   ~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN     ~
   ~ THE SOFTWARE.                                                                 ~
   ~                                                                               ~
   ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  */
  package org.miaixz.bus.socket.buffer;
  
  import java.util.concurrent.ScheduledFuture;
  import java.util.concurrent.ScheduledThreadPoolExecutor;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.atomic.AtomicInteger;
  
  /**
   * ByteBuffer内存池
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public final class BufferPagePool {
  
      public static final BufferPagePool DEFAULT_BUFFER_PAGE_POOL = new BufferPagePool(0, 1, false);
      /**
       * 守护线程在空闲时期回收内存资源
       */
      private static final ScheduledThreadPoolExecutor BUFFER_POOL_CLEAN = new ScheduledThreadPoolExecutor(1, r -> {
          Thread thread = new Thread(r, "BufferPoolClean");
          thread.setDaemon(true);
          return thread;
      });
      /**
       * 内存页游标
       */
      private final AtomicInteger cursor = new AtomicInteger(0);
      /**
       * 内存页组
       */
      private BufferPage[] bufferPages;
      private boolean enabled = true;
  
      /**
       * @param pageSize 内存页大小
       * @param pageNum  内存页个数
       * @param isDirect 是否使用直接缓冲区
       */
      public BufferPagePool(final int pageSize, final int pageNum, final boolean isDirect) {
          bufferPages = new BufferPage[pageNum];
          for (int i = 0; i < pageNum; i++) {
              bufferPages[i] = new BufferPage(pageSize, isDirect);
          }
          if (pageNum == 0 || pageSize == 0) {
              future.cancel(false);
          }
      }
  
      /**
       * 申请内存页
       *
       * @return 缓存页对象
       */
      public BufferPage allocateBufferPage() {
          if (enabled) {
              // 轮训游标，均衡分配内存页
              return bufferPages[(cursor.getAndIncrement() & Integer.MAX_VALUE) % bufferPages.length];
          }
          throw new IllegalStateException("buffer pool is disable");
      }
  
      /**
       * 释放回收内存
       */
      public void release() {
          enabled = false;
      }
  
      /**
       * 内存回收任务
       */
      private final ScheduledFuture<?> future = BUFFER_POOL_CLEAN.scheduleWithFixedDelay(new Runnable() {
  
          @Override
          public void run() {
              if (enabled) {
                  for (BufferPage bufferPage : bufferPages) {
                      bufferPage.tryClean();
                  }
              } else {
                  if (bufferPages != null) {
                      for (BufferPage page : bufferPages) {
                          page.release();
                      }
                      bufferPages = null;
                  }
                  future.cancel(false);
              }
          }
      }, 500, 1000, TimeUnit.MILLISECONDS);
  
  }
  