001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import java.io.IOException;
021import java.nio.ByteBuffer;
022import java.nio.channels.GatheringByteChannel;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Chain of ByteBuffers. Used writing out an array of byte buffers.
027 */
028@InterfaceAudience.Private
029class BufferChain {
030  private final ByteBuffer[] buffers;
031  private int remaining = 0;
032  private int size;
033
034  BufferChain(ByteBuffer... buffers) {
035    for (ByteBuffer b : buffers) {
036      this.remaining += b.remaining();
037    }
038    this.size = remaining;
039    this.buffers = buffers;
040  }
041
042  /**
043   * Expensive. Makes a new buffer to hold a copy of what is in contained ByteBuffers. This call
044   * drains this instance; it cannot be used subsequent to the call.
045   * @return A new byte buffer with the content of all contained ByteBuffers.
046   */
047  byte[] getBytes() {
048    if (!hasRemaining()) throw new IllegalAccessError();
049    byte[] bytes = new byte[this.remaining];
050    int offset = 0;
051    for (ByteBuffer bb : this.buffers) {
052      int length = bb.remaining();
053      bb.get(bytes, offset, length);
054      offset += length;
055    }
056    return bytes;
057  }
058
059  boolean hasRemaining() {
060    return remaining > 0;
061  }
062
063  long write(GatheringByteChannel channel) throws IOException {
064    if (!hasRemaining()) {
065      return 0;
066    }
067    long written = 0;
068    for (ByteBuffer bb : this.buffers) {
069      if (bb.hasRemaining()) {
070        final int pos = bb.position();
071        final int result = channel.write(bb);
072        if (result <= 0) {
073          // Write error. Return how much we were able to write until now.
074          return written;
075        }
076        // Adjust the position of buffers already written so we don't write out
077        // duplicate data upon retry of incomplete write with the same buffer chain.
078        bb.position(pos + result);
079        remaining -= result;
080        written += result;
081      }
082    }
083    return written;
084  }
085
086  int size() {
087    return size;
088  }
089
090  ByteBuffer[] getBuffers() {
091    return this.buffers;
092  }
093}