/**
   * Set the output buffer size
   *
   * @param size The new buffer size (>= {@link TdsCore#MIN_PKT_SIZE} <= {@link
   *     TdsCore#MAX_PKT_SIZE}).
   */
  void setBufferSize(int size) {
    if (size < bufferPtr || size == bufferSize) {
      return; // Can't shrink buffer size;
    }

    if (size < TdsCore.MIN_PKT_SIZE || size > TdsCore.MAX_PKT_SIZE) {
      throw new IllegalArgumentException("Invalid buffer size parameter " + size);
    }

    byte[] tmp = new byte[size];
    System.arraycopy(buffer, 0, tmp, 0, bufferPtr);
    buffer = tmp;
  }
  /**
   * Write an array of bytes to the output stream.
   *
   * @param b The byte array to write.
   * @throws IOException
   */
  void write(byte[] b) throws IOException {
    int bytesToWrite = b.length;
    int off = 0;

    while (bytesToWrite > 0) {
      int available = buffer.length - bufferPtr;

      if (available == 0) {
        putPacket(0);
        continue;
      }

      int bc = (available > bytesToWrite) ? bytesToWrite : available;
      System.arraycopy(b, off, buffer, bufferPtr, bc);
      off += bc;
      bufferPtr += bc;
      bytesToWrite -= bc;
    }
  }
  /**
   * Write a partial byte buffer to the output stream.
   *
   * @param b The byte array buffer.
   * @param off The offset into the byte array.
   * @param len The number of bytes to write.
   * @throws IOException
   */
  void write(byte[] b, int off, int len) throws IOException {
    int limit = (off + len) > b.length ? b.length : off + len;
    int bytesToWrite = limit - off;
    int i = len - bytesToWrite;

    while (bytesToWrite > 0) {
      int available = buffer.length - bufferPtr;

      if (available == 0) {
        putPacket(0);
        continue;
      }

      int bc = (available > bytesToWrite) ? bytesToWrite : available;
      System.arraycopy(b, off, buffer, bufferPtr, bc);
      off += bc;
      bufferPtr += bc;
      bytesToWrite -= bc;
    }

    for (; i > 0; i--) {
      write((byte) 0);
    }
  }