def write()

in util-core/src/main/scala/com/twitter/io/Buf.scala [51:175]


  def write(output: Array[Byte], off: Int): Unit

  /**
   * Write the entire contents of this `Buf` into the given nio buffer.
   * Partial writes aren't supported directly through this API; they can be
   * accomplished by first [[slice slicing]] the buffer.
   * This method should be preferred over `Buf.ByteBuffer.extract`, `Buf.ByteArray.Owned`,
   * etc family of functions when you want to control the destination of the data, as
   * opposed to letting the `Buf` implementation manage the destination. For example, if
   * the data is destined for an IO operation, it may be preferable to provide a direct
   * nio `ByteBuffer` to ensure the avoidance of intermediate heap-based representations.
   *
   * @see [[com.twitter.io.Buf.write(output:Array[Byte],off:Int):Unit* write(Array[Byte],Int)]]
   * for writing to byte arrays.
   *
   * @note Throws `java.lang.IllegalArgumentException` when `output` doesn't have enough
   * space as defined by `ByteBuffer.remaining()` to hold the contents of this `Buf`.
   *
   * @note Throws `ReadOnlyBufferException` if the provided buffer is read-only.
   *
   * @note [[Buf]] implementors should use the helper [[checkWriteArgs]].
   */
  @throws(classOf[IllegalArgumentException])
  @throws(classOf[ReadOnlyBufferException])
  def write(output: java.nio.ByteBuffer): Unit

  /**
   * The number of bytes in the buffer
   */
  def length: Int

  /**
   * Returns a new buffer representing a slice of this buffer, delimited
   * by the indices `from` inclusive and `until` exclusive: `[from, until)`.
   * Out of bounds indices are truncated. Negative indices are not accepted.
   *
   * @note [[Buf]] implementors should use the helpers [[Buf.checkSliceArgs]],
   *       [[isSliceEmpty]], and [[isSliceIdentity]].
   */
  def slice(from: Int, until: Int): Buf

  /**
   * Concatenate this buffer with the given buffer.
   */
  final def concat(right: Buf): Buf = {
    if (this.isEmpty) right
    else if (right.isEmpty) this
    else
      this match {
        // This could be much cleaner as a Tuple match, but we want to avoid the allocation.
        case Buf.Composite(left: Vector[Buf], leftLength) =>
          right match {
            case Buf.Composite(right: Vector[Buf], rightLength) =>
              Buf.Composite(Buf.fastConcat(left, right), leftLength + rightLength)
            case Buf.Composite(right: IndexedTwo, rightLength) =>
              Buf.Composite(left :+ right.a :+ right.b, leftLength + rightLength)
            case Buf.Composite(right: IndexedThree, rightLength) =>
              Buf.Composite(left :+ right.a :+ right.b :+ right.c, leftLength + rightLength)
            case _ =>
              Buf.Composite(left :+ right, leftLength + right.length)
          }

        case Buf.Composite(left: IndexedTwo, leftLength) =>
          right match {
            case Buf.Composite(right: Vector[Buf], rightLength) =>
              Buf.Composite(left.a +: left.b +: right, leftLength + rightLength)
            case Buf.Composite(right: IndexedTwo, rightLength) =>
              Buf.Composite(
                (new VectorBuilder[Buf] += left.a += left.b += right.a += right.b).result(),
                leftLength + rightLength
              )
            case Buf.Composite(right: IndexedThree, rightLength) =>
              Buf.Composite(
                (new VectorBuilder[Buf] += left.a += left.b += right.a += right.b += right.c)
                  .result(),
                leftLength + rightLength
              )
            case _ =>
              Buf.Composite(
                new IndexedThree(left.a, left.b, right),
                leftLength + right.length
              )
          }

        case Buf.Composite(left: IndexedThree, leftLength) =>
          right match {
            case Buf.Composite(right: Vector[Buf], rightLength) =>
              Buf.Composite(left.a +: left.b +: left.c +: right, leftLength + rightLength)
            case Buf.Composite(right: IndexedTwo, rightLength) =>
              Buf.Composite(
                (new VectorBuilder[Buf] += left.a += left.b += left.c += right.a += right.b)
                  .result(),
                leftLength + rightLength
              )
            case Buf.Composite(right: IndexedThree, rightLength) =>
              Buf.Composite(
                (new VectorBuilder[
                  Buf
                ] += left.a += left.b += left.c += right.a += right.b += right.c)
                  .result(),
                leftLength + rightLength
              )
            case _ =>
              Buf.Composite(
                (new VectorBuilder[Buf] += left.a += left.b += left.c += right).result(),
                leftLength + right.length
              )
          }

        case left =>
          right match {
            case Buf.Composite(right: Vector[Buf], rightLength) =>
              Buf.Composite(left +: right, left.length + rightLength)
            case Buf.Composite(right: IndexedTwo, rightLength) =>
              Buf.Composite(new IndexedThree(left, right.a, right.b), left.length + rightLength)
            case Buf.Composite(right: IndexedThree, rightLength) =>
              Buf.Composite(
                (new VectorBuilder[Buf] += left += right.a += right.b += right.c).result(),
                left.length + rightLength
              )
            case _ =>
              Buf.Composite(new IndexedTwo(left, right), left.length + right.length)
          }
      }
  }