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.nio.ByteBuffer;
  import java.util.concurrent.Semaphore;
  
  /**
   * 虚拟ByteBuffer缓冲区
   *
   * @author Kimi Liu
   * @since Java 17+
   */
  public final class VirtualBuffer {
  
      /**
       * 当前虚拟buffer的归属内存页
       */
      private final BufferPage bufferPage;
      /**
       * 是否已回收
       */
      private final Semaphore clean = new Semaphore(1);
      /**
       * 通过ByteBuffer.slice()隐射出来的虚拟ByteBuffer
       *
       * @see ByteBuffer#slice()
       */
      private ByteBuffer buffer;
      /**
       * 当前虚拟buffer映射的实际buffer.position
       */
      private int parentPosition;
  
      /**
       * 当前虚拟buffer映射的实际buffer.limit
       */
      private int parentLimit;
  
      /**
       * 缓冲区容量
       */
      private int capacity;
  
      VirtualBuffer(BufferPage bufferPage, ByteBuffer buffer, int parentPosition, int parentLimit) {
          this.bufferPage = bufferPage;
          this.buffer = buffer;
          this.parentPosition = parentPosition;
          this.parentLimit = parentLimit;
          updateCapacity();
      }
  
      public static VirtualBuffer wrap(ByteBuffer buffer) {
          return new VirtualBuffer(null, buffer, 0, 0);
      }
  
      int getParentPosition() {
          return parentPosition;
      }
  
      void setParentPosition(int parentPosition) {
          this.parentPosition = parentPosition;
          updateCapacity();
      }
  
      int getParentLimit() {
          return parentLimit;
      }
  
      void setParentLimit(int parentLimit) {
          this.parentLimit = parentLimit;
          updateCapacity();
      }
  
      private void updateCapacity() {
          capacity = this.parentLimit - this.parentPosition;
      }
  
      public int getCapacity() {
          return capacity;
      }
  
      /**
       * 获取真实缓冲区
       *
       * @return 真实缓冲区
       */
      public ByteBuffer buffer() {
          return buffer;
      }
  
      /**
       * 设置真实缓冲区
       *
       * @param buffer 真实缓冲区
       */
      void buffer(ByteBuffer buffer) {
          this.buffer = buffer;
          clean.release();
      }
  
      /**
       * 释放虚拟缓冲区
       */
      public void clean() {
          if (clean.tryAcquire()) {
              if (bufferPage != null) {
                  bufferPage.clean(this);
              }
          } else {
              throw new UnsupportedOperationException("buffer has cleaned");
          }
      }
  
      @Override
      public String toString() {
          return "VirtualBuffer{parentPosition=" + parentPosition + ", parentLimit=" + parentLimit + '}';
      }
  
  }
  