hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject [07/51] [partial] hbase-site git commit: Published site at 58283fa1b1b10beec62cefa40babff6a1424b06c.
Date Tue, 23 Feb 2016 17:08:15 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/d02dd5db/devapidocs/src-html/org/apache/hadoop/hbase/util/ByteBufferArray.Visitor.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/util/ByteBufferArray.Visitor.html b/devapidocs/src-html/org/apache/hadoop/hbase/util/ByteBufferArray.Visitor.html
index 6cff868..2a832cb 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/util/ByteBufferArray.Visitor.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/util/ByteBufferArray.Visitor.html
@@ -26,258 +26,262 @@
 <span class="sourceLineNo">018</span> */<a name="line.18"></a>
 <span class="sourceLineNo">019</span>package org.apache.hadoop.hbase.util;<a name="line.19"></a>
 <span class="sourceLineNo">020</span><a name="line.20"></a>
-<span class="sourceLineNo">021</span>import java.nio.ByteBuffer;<a name="line.21"></a>
-<span class="sourceLineNo">022</span>import java.util.concurrent.locks.Lock;<a name="line.22"></a>
-<span class="sourceLineNo">023</span>import java.util.concurrent.locks.ReentrantLock;<a name="line.23"></a>
-<span class="sourceLineNo">024</span><a name="line.24"></a>
-<span class="sourceLineNo">025</span>import org.apache.commons.logging.Log;<a name="line.25"></a>
-<span class="sourceLineNo">026</span>import org.apache.commons.logging.LogFactory;<a name="line.26"></a>
-<span class="sourceLineNo">027</span>import org.apache.hadoop.hbase.classification.InterfaceAudience;<a name="line.27"></a>
-<span class="sourceLineNo">028</span>import org.apache.hadoop.hbase.nio.ByteBuff;<a name="line.28"></a>
-<span class="sourceLineNo">029</span>import org.apache.hadoop.hbase.nio.MultiByteBuff;<a name="line.29"></a>
-<span class="sourceLineNo">030</span>import org.apache.hadoop.hbase.nio.SingleByteBuff;<a name="line.30"></a>
-<span class="sourceLineNo">031</span>import org.apache.hadoop.util.StringUtils;<a name="line.31"></a>
-<span class="sourceLineNo">032</span><a name="line.32"></a>
-<span class="sourceLineNo">033</span>/**<a name="line.33"></a>
-<span class="sourceLineNo">034</span> * This class manages an array of ByteBuffers with a default size 4MB. These<a name="line.34"></a>
-<span class="sourceLineNo">035</span> * buffers are sequential and could be considered as a large buffer.It supports<a name="line.35"></a>
-<span class="sourceLineNo">036</span> * reading/writing data from this large buffer with a position and offset<a name="line.36"></a>
-<span class="sourceLineNo">037</span> */<a name="line.37"></a>
-<span class="sourceLineNo">038</span>@InterfaceAudience.Private<a name="line.38"></a>
-<span class="sourceLineNo">039</span>public final class ByteBufferArray {<a name="line.39"></a>
-<span class="sourceLineNo">040</span>  private static final Log LOG = LogFactory.getLog(ByteBufferArray.class);<a name="line.40"></a>
-<span class="sourceLineNo">041</span><a name="line.41"></a>
-<span class="sourceLineNo">042</span>  static final int DEFAULT_BUFFER_SIZE = 4 * 1024 * 1024;<a name="line.42"></a>
-<span class="sourceLineNo">043</span>  private ByteBuffer buffers[];<a name="line.43"></a>
-<span class="sourceLineNo">044</span>  private Lock locks[];<a name="line.44"></a>
-<span class="sourceLineNo">045</span>  private int bufferSize;<a name="line.45"></a>
-<span class="sourceLineNo">046</span>  private int bufferCount;<a name="line.46"></a>
-<span class="sourceLineNo">047</span><a name="line.47"></a>
-<span class="sourceLineNo">048</span>  /**<a name="line.48"></a>
-<span class="sourceLineNo">049</span>   * We allocate a number of byte buffers as the capacity. In order not to out<a name="line.49"></a>
-<span class="sourceLineNo">050</span>   * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}),<a name="line.50"></a>
-<span class="sourceLineNo">051</span>   * we will allocate one additional buffer with capacity 0;<a name="line.51"></a>
-<span class="sourceLineNo">052</span>   * @param capacity total size of the byte buffer array<a name="line.52"></a>
-<span class="sourceLineNo">053</span>   * @param directByteBuffer true if we allocate direct buffer<a name="line.53"></a>
-<span class="sourceLineNo">054</span>   */<a name="line.54"></a>
-<span class="sourceLineNo">055</span>  public ByteBufferArray(long capacity, boolean directByteBuffer) {<a name="line.55"></a>
-<span class="sourceLineNo">056</span>    this.bufferSize = DEFAULT_BUFFER_SIZE;<a name="line.56"></a>
-<span class="sourceLineNo">057</span>    if (this.bufferSize &gt; (capacity / 16))<a name="line.57"></a>
-<span class="sourceLineNo">058</span>      this.bufferSize = (int) roundUp(capacity / 16, 32768);<a name="line.58"></a>
-<span class="sourceLineNo">059</span>    this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize);<a name="line.59"></a>
-<span class="sourceLineNo">060</span>    LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity)<a name="line.60"></a>
-<span class="sourceLineNo">061</span>        + ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count="<a name="line.61"></a>
-<span class="sourceLineNo">062</span>        + bufferCount + ", direct=" + directByteBuffer);<a name="line.62"></a>
-<span class="sourceLineNo">063</span>    buffers = new ByteBuffer[bufferCount + 1];<a name="line.63"></a>
-<span class="sourceLineNo">064</span>    locks = new Lock[bufferCount + 1];<a name="line.64"></a>
-<span class="sourceLineNo">065</span>    for (int i = 0; i &lt;= bufferCount; i++) {<a name="line.65"></a>
-<span class="sourceLineNo">066</span>      locks[i] = new ReentrantLock();<a name="line.66"></a>
-<span class="sourceLineNo">067</span>      if (i &lt; bufferCount) {<a name="line.67"></a>
-<span class="sourceLineNo">068</span>        buffers[i] = directByteBuffer ? ByteBuffer.allocateDirect(bufferSize)<a name="line.68"></a>
-<span class="sourceLineNo">069</span>            : ByteBuffer.allocate(bufferSize);<a name="line.69"></a>
-<span class="sourceLineNo">070</span>      } else {<a name="line.70"></a>
-<span class="sourceLineNo">071</span>        buffers[i] = ByteBuffer.allocate(0);<a name="line.71"></a>
-<span class="sourceLineNo">072</span>      }<a name="line.72"></a>
-<span class="sourceLineNo">073</span><a name="line.73"></a>
-<span class="sourceLineNo">074</span>    }<a name="line.74"></a>
-<span class="sourceLineNo">075</span>  }<a name="line.75"></a>
-<span class="sourceLineNo">076</span><a name="line.76"></a>
-<span class="sourceLineNo">077</span>  private long roundUp(long n, long to) {<a name="line.77"></a>
-<span class="sourceLineNo">078</span>    return ((n + to - 1) / to) * to;<a name="line.78"></a>
+<span class="sourceLineNo">021</span>import java.io.IOException;<a name="line.21"></a>
+<span class="sourceLineNo">022</span>import java.nio.ByteBuffer;<a name="line.22"></a>
+<span class="sourceLineNo">023</span>import java.util.concurrent.locks.Lock;<a name="line.23"></a>
+<span class="sourceLineNo">024</span>import java.util.concurrent.locks.ReentrantLock;<a name="line.24"></a>
+<span class="sourceLineNo">025</span><a name="line.25"></a>
+<span class="sourceLineNo">026</span>import org.apache.commons.logging.Log;<a name="line.26"></a>
+<span class="sourceLineNo">027</span>import org.apache.commons.logging.LogFactory;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import org.apache.hadoop.hbase.classification.InterfaceAudience;<a name="line.28"></a>
+<span class="sourceLineNo">029</span>import org.apache.hadoop.hbase.nio.ByteBuff;<a name="line.29"></a>
+<span class="sourceLineNo">030</span>import org.apache.hadoop.hbase.nio.MultiByteBuff;<a name="line.30"></a>
+<span class="sourceLineNo">031</span>import org.apache.hadoop.hbase.nio.SingleByteBuff;<a name="line.31"></a>
+<span class="sourceLineNo">032</span>import org.apache.hadoop.util.StringUtils;<a name="line.32"></a>
+<span class="sourceLineNo">033</span><a name="line.33"></a>
+<span class="sourceLineNo">034</span>/**<a name="line.34"></a>
+<span class="sourceLineNo">035</span> * This class manages an array of ByteBuffers with a default size 4MB. These<a name="line.35"></a>
+<span class="sourceLineNo">036</span> * buffers are sequential and could be considered as a large buffer.It supports<a name="line.36"></a>
+<span class="sourceLineNo">037</span> * reading/writing data from this large buffer with a position and offset<a name="line.37"></a>
+<span class="sourceLineNo">038</span> */<a name="line.38"></a>
+<span class="sourceLineNo">039</span>@InterfaceAudience.Private<a name="line.39"></a>
+<span class="sourceLineNo">040</span>public final class ByteBufferArray {<a name="line.40"></a>
+<span class="sourceLineNo">041</span>  private static final Log LOG = LogFactory.getLog(ByteBufferArray.class);<a name="line.41"></a>
+<span class="sourceLineNo">042</span><a name="line.42"></a>
+<span class="sourceLineNo">043</span>  public static final int DEFAULT_BUFFER_SIZE = 4 * 1024 * 1024;<a name="line.43"></a>
+<span class="sourceLineNo">044</span>  private ByteBuffer buffers[];<a name="line.44"></a>
+<span class="sourceLineNo">045</span>  private Lock locks[];<a name="line.45"></a>
+<span class="sourceLineNo">046</span>  private int bufferSize;<a name="line.46"></a>
+<span class="sourceLineNo">047</span>  private int bufferCount;<a name="line.47"></a>
+<span class="sourceLineNo">048</span>  private ByteBufferAllocator allocator;<a name="line.48"></a>
+<span class="sourceLineNo">049</span>  /**<a name="line.49"></a>
+<span class="sourceLineNo">050</span>   * We allocate a number of byte buffers as the capacity. In order not to out<a name="line.50"></a>
+<span class="sourceLineNo">051</span>   * of the array bounds for the last byte(see {@link ByteBufferArray#multiple}),<a name="line.51"></a>
+<span class="sourceLineNo">052</span>   * we will allocate one additional buffer with capacity 0;<a name="line.52"></a>
+<span class="sourceLineNo">053</span>   * @param capacity total size of the byte buffer array<a name="line.53"></a>
+<span class="sourceLineNo">054</span>   * @param directByteBuffer true if we allocate direct buffer<a name="line.54"></a>
+<span class="sourceLineNo">055</span>   * @param allocator the ByteBufferAllocator that will create the buffers<a name="line.55"></a>
+<span class="sourceLineNo">056</span>   * @throws IOException throws IOException if there is an exception thrown by the allocator<a name="line.56"></a>
+<span class="sourceLineNo">057</span>   */<a name="line.57"></a>
+<span class="sourceLineNo">058</span>  public ByteBufferArray(long capacity, boolean directByteBuffer, ByteBufferAllocator allocator)<a name="line.58"></a>
+<span class="sourceLineNo">059</span>      throws IOException {<a name="line.59"></a>
+<span class="sourceLineNo">060</span>    this.bufferSize = DEFAULT_BUFFER_SIZE;<a name="line.60"></a>
+<span class="sourceLineNo">061</span>    if (this.bufferSize &gt; (capacity / 16))<a name="line.61"></a>
+<span class="sourceLineNo">062</span>      this.bufferSize = (int) roundUp(capacity / 16, 32768);<a name="line.62"></a>
+<span class="sourceLineNo">063</span>    this.bufferCount = (int) (roundUp(capacity, bufferSize) / bufferSize);<a name="line.63"></a>
+<span class="sourceLineNo">064</span>    LOG.info("Allocating buffers total=" + StringUtils.byteDesc(capacity)<a name="line.64"></a>
+<span class="sourceLineNo">065</span>        + ", sizePerBuffer=" + StringUtils.byteDesc(bufferSize) + ", count="<a name="line.65"></a>
+<span class="sourceLineNo">066</span>        + bufferCount + ", direct=" + directByteBuffer);<a name="line.66"></a>
+<span class="sourceLineNo">067</span>    buffers = new ByteBuffer[bufferCount + 1];<a name="line.67"></a>
+<span class="sourceLineNo">068</span>    locks = new Lock[bufferCount + 1];<a name="line.68"></a>
+<span class="sourceLineNo">069</span>    this.allocator = allocator;<a name="line.69"></a>
+<span class="sourceLineNo">070</span>    for (int i = 0; i &lt;= bufferCount; i++) {<a name="line.70"></a>
+<span class="sourceLineNo">071</span>      locks[i] = new ReentrantLock();<a name="line.71"></a>
+<span class="sourceLineNo">072</span>      if (i &lt; bufferCount) {<a name="line.72"></a>
+<span class="sourceLineNo">073</span>        buffers[i] = allocator.allocate(bufferSize, directByteBuffer);<a name="line.73"></a>
+<span class="sourceLineNo">074</span>      } else {<a name="line.74"></a>
+<span class="sourceLineNo">075</span>        // always create on heap<a name="line.75"></a>
+<span class="sourceLineNo">076</span>        buffers[i] = ByteBuffer.allocate(0);<a name="line.76"></a>
+<span class="sourceLineNo">077</span>      }<a name="line.77"></a>
+<span class="sourceLineNo">078</span>    }<a name="line.78"></a>
 <span class="sourceLineNo">079</span>  }<a name="line.79"></a>
 <span class="sourceLineNo">080</span><a name="line.80"></a>
-<span class="sourceLineNo">081</span>  /**<a name="line.81"></a>
-<span class="sourceLineNo">082</span>   * Transfers bytes from this buffer array into the given destination array<a name="line.82"></a>
-<span class="sourceLineNo">083</span>   * @param start start position in the ByteBufferArray<a name="line.83"></a>
-<span class="sourceLineNo">084</span>   * @param len The maximum number of bytes to be written to the given array<a name="line.84"></a>
-<span class="sourceLineNo">085</span>   * @param dstArray The array into which bytes are to be written<a name="line.85"></a>
-<span class="sourceLineNo">086</span>   * @return number of bytes read<a name="line.86"></a>
-<span class="sourceLineNo">087</span>   */<a name="line.87"></a>
-<span class="sourceLineNo">088</span>  public int getMultiple(long start, int len, byte[] dstArray) {<a name="line.88"></a>
-<span class="sourceLineNo">089</span>    return getMultiple(start, len, dstArray, 0);<a name="line.89"></a>
-<span class="sourceLineNo">090</span>  }<a name="line.90"></a>
-<span class="sourceLineNo">091</span><a name="line.91"></a>
-<span class="sourceLineNo">092</span>  /**<a name="line.92"></a>
-<span class="sourceLineNo">093</span>   * Transfers bytes from this buffer array into the given destination array<a name="line.93"></a>
-<span class="sourceLineNo">094</span>   * @param start start offset of this buffer array<a name="line.94"></a>
-<span class="sourceLineNo">095</span>   * @param len The maximum number of bytes to be written to the given array<a name="line.95"></a>
-<span class="sourceLineNo">096</span>   * @param dstArray The array into which bytes are to be written<a name="line.96"></a>
-<span class="sourceLineNo">097</span>   * @param dstOffset The offset within the given array of the first byte to be<a name="line.97"></a>
-<span class="sourceLineNo">098</span>   *          written<a name="line.98"></a>
-<span class="sourceLineNo">099</span>   * @return number of bytes read<a name="line.99"></a>
-<span class="sourceLineNo">100</span>   */<a name="line.100"></a>
-<span class="sourceLineNo">101</span>  public int getMultiple(long start, int len, byte[] dstArray, int dstOffset) {<a name="line.101"></a>
-<span class="sourceLineNo">102</span>    multiple(start, len, dstArray, dstOffset, GET_MULTIPLE_VISTOR);<a name="line.102"></a>
-<span class="sourceLineNo">103</span>    return len;<a name="line.103"></a>
-<span class="sourceLineNo">104</span>  }<a name="line.104"></a>
-<span class="sourceLineNo">105</span><a name="line.105"></a>
-<span class="sourceLineNo">106</span>  private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {<a name="line.106"></a>
-<span class="sourceLineNo">107</span>    @Override<a name="line.107"></a>
-<span class="sourceLineNo">108</span>    public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {<a name="line.108"></a>
-<span class="sourceLineNo">109</span>      bb.get(array, arrayIdx, len);<a name="line.109"></a>
-<span class="sourceLineNo">110</span>    }<a name="line.110"></a>
-<span class="sourceLineNo">111</span>  };<a name="line.111"></a>
-<span class="sourceLineNo">112</span><a name="line.112"></a>
-<span class="sourceLineNo">113</span>  /**<a name="line.113"></a>
-<span class="sourceLineNo">114</span>   * Transfers bytes from the given source array into this buffer array<a name="line.114"></a>
-<span class="sourceLineNo">115</span>   * @param start start offset of this buffer array<a name="line.115"></a>
-<span class="sourceLineNo">116</span>   * @param len The maximum number of bytes to be read from the given array<a name="line.116"></a>
-<span class="sourceLineNo">117</span>   * @param srcArray The array from which bytes are to be read<a name="line.117"></a>
-<span class="sourceLineNo">118</span>   */<a name="line.118"></a>
-<span class="sourceLineNo">119</span>  public void putMultiple(long start, int len, byte[] srcArray) {<a name="line.119"></a>
-<span class="sourceLineNo">120</span>    putMultiple(start, len, srcArray, 0);<a name="line.120"></a>
-<span class="sourceLineNo">121</span>  }<a name="line.121"></a>
-<span class="sourceLineNo">122</span><a name="line.122"></a>
-<span class="sourceLineNo">123</span>  /**<a name="line.123"></a>
-<span class="sourceLineNo">124</span>   * Transfers bytes from the given source array into this buffer array<a name="line.124"></a>
-<span class="sourceLineNo">125</span>   * @param start start offset of this buffer array<a name="line.125"></a>
-<span class="sourceLineNo">126</span>   * @param len The maximum number of bytes to be read from the given array<a name="line.126"></a>
-<span class="sourceLineNo">127</span>   * @param srcArray The array from which bytes are to be read<a name="line.127"></a>
-<span class="sourceLineNo">128</span>   * @param srcOffset The offset within the given array of the first byte to be<a name="line.128"></a>
-<span class="sourceLineNo">129</span>   *          read<a name="line.129"></a>
-<span class="sourceLineNo">130</span>   */<a name="line.130"></a>
-<span class="sourceLineNo">131</span>  public void putMultiple(long start, int len, byte[] srcArray, int srcOffset) {<a name="line.131"></a>
-<span class="sourceLineNo">132</span>    multiple(start, len, srcArray, srcOffset, PUT_MULTIPLE_VISITOR);<a name="line.132"></a>
-<span class="sourceLineNo">133</span>  }<a name="line.133"></a>
-<span class="sourceLineNo">134</span><a name="line.134"></a>
-<span class="sourceLineNo">135</span>  private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {<a name="line.135"></a>
-<span class="sourceLineNo">136</span>    @Override<a name="line.136"></a>
-<span class="sourceLineNo">137</span>    public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {<a name="line.137"></a>
-<span class="sourceLineNo">138</span>      bb.put(array, arrayIdx, len);<a name="line.138"></a>
-<span class="sourceLineNo">139</span>    }<a name="line.139"></a>
-<span class="sourceLineNo">140</span>  };<a name="line.140"></a>
-<span class="sourceLineNo">141</span><a name="line.141"></a>
-<span class="sourceLineNo">142</span>  private interface Visitor {<a name="line.142"></a>
-<span class="sourceLineNo">143</span>    /**<a name="line.143"></a>
-<span class="sourceLineNo">144</span>     * Visit the given byte buffer, if it is a read action, we will transfer the<a name="line.144"></a>
-<span class="sourceLineNo">145</span>     * bytes from the buffer to the destination array, else if it is a write<a name="line.145"></a>
-<span class="sourceLineNo">146</span>     * action, we will transfer the bytes from the source array to the buffer<a name="line.146"></a>
-<span class="sourceLineNo">147</span>     * @param bb byte buffer<a name="line.147"></a>
-<span class="sourceLineNo">148</span>     * @param array a source or destination byte array<a name="line.148"></a>
-<span class="sourceLineNo">149</span>     * @param arrayOffset offset of the byte array<a name="line.149"></a>
-<span class="sourceLineNo">150</span>     * @param len read/write length<a name="line.150"></a>
-<span class="sourceLineNo">151</span>     */<a name="line.151"></a>
-<span class="sourceLineNo">152</span>    void visit(ByteBuffer bb, byte[] array, int arrayOffset, int len);<a name="line.152"></a>
-<span class="sourceLineNo">153</span>  }<a name="line.153"></a>
-<span class="sourceLineNo">154</span><a name="line.154"></a>
-<span class="sourceLineNo">155</span>  /**<a name="line.155"></a>
-<span class="sourceLineNo">156</span>   * Access(read or write) this buffer array with a position and length as the<a name="line.156"></a>
-<span class="sourceLineNo">157</span>   * given array. Here we will only lock one buffer even if it may be need visit<a name="line.157"></a>
-<span class="sourceLineNo">158</span>   * several buffers. The consistency is guaranteed by the caller.<a name="line.158"></a>
-<span class="sourceLineNo">159</span>   * @param start start offset of this buffer array<a name="line.159"></a>
-<span class="sourceLineNo">160</span>   * @param len The maximum number of bytes to be accessed<a name="line.160"></a>
-<span class="sourceLineNo">161</span>   * @param array The array from/to which bytes are to be read/written<a name="line.161"></a>
-<span class="sourceLineNo">162</span>   * @param arrayOffset The offset within the given array of the first byte to<a name="line.162"></a>
-<span class="sourceLineNo">163</span>   *          be read or written<a name="line.163"></a>
-<span class="sourceLineNo">164</span>   * @param visitor implement of how to visit the byte buffer<a name="line.164"></a>
-<span class="sourceLineNo">165</span>   */<a name="line.165"></a>
-<span class="sourceLineNo">166</span>  void multiple(long start, int len, byte[] array, int arrayOffset, Visitor visitor) {<a name="line.166"></a>
-<span class="sourceLineNo">167</span>    assert len &gt;= 0;<a name="line.167"></a>
-<span class="sourceLineNo">168</span>    long end = start + len;<a name="line.168"></a>
-<span class="sourceLineNo">169</span>    int startBuffer = (int) (start / bufferSize), startOffset = (int) (start % bufferSize);<a name="line.169"></a>
-<span class="sourceLineNo">170</span>    int endBuffer = (int) (end / bufferSize), endOffset = (int) (end % bufferSize);<a name="line.170"></a>
-<span class="sourceLineNo">171</span>    assert array.length &gt;= len + arrayOffset;<a name="line.171"></a>
-<span class="sourceLineNo">172</span>    assert startBuffer &gt;= 0 &amp;&amp; startBuffer &lt; bufferCount;<a name="line.172"></a>
-<span class="sourceLineNo">173</span>    assert endBuffer &gt;= 0 &amp;&amp; endBuffer &lt; bufferCount<a name="line.173"></a>
-<span class="sourceLineNo">174</span>        || (endBuffer == bufferCount &amp;&amp; endOffset == 0);<a name="line.174"></a>
-<span class="sourceLineNo">175</span>    if (startBuffer &gt;= locks.length || startBuffer &lt; 0) {<a name="line.175"></a>
-<span class="sourceLineNo">176</span>      String msg = "Failed multiple, start=" + start + ",startBuffer="<a name="line.176"></a>
-<span class="sourceLineNo">177</span>          + startBuffer + ",bufferSize=" + bufferSize;<a name="line.177"></a>
-<span class="sourceLineNo">178</span>      LOG.error(msg);<a name="line.178"></a>
-<span class="sourceLineNo">179</span>      throw new RuntimeException(msg);<a name="line.179"></a>
-<span class="sourceLineNo">180</span>    }<a name="line.180"></a>
-<span class="sourceLineNo">181</span>    int srcIndex = 0, cnt = -1;<a name="line.181"></a>
-<span class="sourceLineNo">182</span>    for (int i = startBuffer; i &lt;= endBuffer; ++i) {<a name="line.182"></a>
-<span class="sourceLineNo">183</span>      Lock lock = locks[i];<a name="line.183"></a>
-<span class="sourceLineNo">184</span>      lock.lock();<a name="line.184"></a>
-<span class="sourceLineNo">185</span>      try {<a name="line.185"></a>
-<span class="sourceLineNo">186</span>        ByteBuffer bb = buffers[i];<a name="line.186"></a>
-<span class="sourceLineNo">187</span>        if (i == startBuffer) {<a name="line.187"></a>
-<span class="sourceLineNo">188</span>          cnt = bufferSize - startOffset;<a name="line.188"></a>
-<span class="sourceLineNo">189</span>          if (cnt &gt; len) cnt = len;<a name="line.189"></a>
-<span class="sourceLineNo">190</span>          bb.limit(startOffset + cnt).position(startOffset);<a name="line.190"></a>
-<span class="sourceLineNo">191</span>        } else if (i == endBuffer) {<a name="line.191"></a>
-<span class="sourceLineNo">192</span>          cnt = endOffset;<a name="line.192"></a>
-<span class="sourceLineNo">193</span>          bb.limit(cnt).position(0);<a name="line.193"></a>
-<span class="sourceLineNo">194</span>        } else {<a name="line.194"></a>
-<span class="sourceLineNo">195</span>          cnt = bufferSize;<a name="line.195"></a>
-<span class="sourceLineNo">196</span>          bb.limit(cnt).position(0);<a name="line.196"></a>
-<span class="sourceLineNo">197</span>        }<a name="line.197"></a>
-<span class="sourceLineNo">198</span>        visitor.visit(bb, array, srcIndex + arrayOffset, cnt);<a name="line.198"></a>
-<span class="sourceLineNo">199</span>        srcIndex += cnt;<a name="line.199"></a>
-<span class="sourceLineNo">200</span>      } finally {<a name="line.200"></a>
-<span class="sourceLineNo">201</span>        lock.unlock();<a name="line.201"></a>
-<span class="sourceLineNo">202</span>      }<a name="line.202"></a>
-<span class="sourceLineNo">203</span>    }<a name="line.203"></a>
-<span class="sourceLineNo">204</span>    assert srcIndex == len;<a name="line.204"></a>
-<span class="sourceLineNo">205</span>  }<a name="line.205"></a>
-<span class="sourceLineNo">206</span><a name="line.206"></a>
-<span class="sourceLineNo">207</span>  /**<a name="line.207"></a>
-<span class="sourceLineNo">208</span>   * Creates a ByteBuff from a given array of ByteBuffers from the given offset to the<a name="line.208"></a>
-<span class="sourceLineNo">209</span>   * length specified. For eg, if there are 4 buffers forming an array each with length 10 and<a name="line.209"></a>
-<span class="sourceLineNo">210</span>   * if we call asSubBuffer(5, 10) then we will create an MBB consisting of two BBs<a name="line.210"></a>
-<span class="sourceLineNo">211</span>   * and the first one be a BB from 'position' 5 to a 'length' 5 and the 2nd BB will be from<a name="line.211"></a>
-<span class="sourceLineNo">212</span>   * 'position' 0 to 'length' 5.<a name="line.212"></a>
-<span class="sourceLineNo">213</span>   * @param offset<a name="line.213"></a>
-<span class="sourceLineNo">214</span>   * @param len<a name="line.214"></a>
-<span class="sourceLineNo">215</span>   * @return a ByteBuff formed from the underlying ByteBuffers<a name="line.215"></a>
-<span class="sourceLineNo">216</span>   */<a name="line.216"></a>
-<span class="sourceLineNo">217</span>  public ByteBuff asSubByteBuff(long offset, int len) {<a name="line.217"></a>
-<span class="sourceLineNo">218</span>    assert len &gt;= 0;<a name="line.218"></a>
-<span class="sourceLineNo">219</span>    long end = offset + len;<a name="line.219"></a>
-<span class="sourceLineNo">220</span>    int startBuffer = (int) (offset / bufferSize), startBufferOffset = (int) (offset % bufferSize);<a name="line.220"></a>
-<span class="sourceLineNo">221</span>    int endBuffer = (int) (end / bufferSize), endBufferOffset = (int) (end % bufferSize);<a name="line.221"></a>
-<span class="sourceLineNo">222</span>    // Last buffer in the array is a dummy one with 0 capacity. Avoid sending back that<a name="line.222"></a>
-<span class="sourceLineNo">223</span>    if (endBuffer == this.bufferCount) {<a name="line.223"></a>
-<span class="sourceLineNo">224</span>      endBuffer--;<a name="line.224"></a>
-<span class="sourceLineNo">225</span>      endBufferOffset = bufferSize;<a name="line.225"></a>
-<span class="sourceLineNo">226</span>    }<a name="line.226"></a>
-<span class="sourceLineNo">227</span>    assert startBuffer &gt;= 0 &amp;&amp; startBuffer &lt; bufferCount;<a name="line.227"></a>
-<span class="sourceLineNo">228</span>    assert endBuffer &gt;= 0 &amp;&amp; endBuffer &lt; bufferCount<a name="line.228"></a>
-<span class="sourceLineNo">229</span>        || (endBuffer == bufferCount &amp;&amp; endBufferOffset == 0);<a name="line.229"></a>
-<span class="sourceLineNo">230</span>    if (startBuffer &gt;= locks.length || startBuffer &lt; 0) {<a name="line.230"></a>
-<span class="sourceLineNo">231</span>      String msg = "Failed subArray, start=" + offset + ",startBuffer=" + startBuffer<a name="line.231"></a>
-<span class="sourceLineNo">232</span>          + ",bufferSize=" + bufferSize;<a name="line.232"></a>
-<span class="sourceLineNo">233</span>      LOG.error(msg);<a name="line.233"></a>
-<span class="sourceLineNo">234</span>      throw new RuntimeException(msg);<a name="line.234"></a>
-<span class="sourceLineNo">235</span>    }<a name="line.235"></a>
-<span class="sourceLineNo">236</span>    int srcIndex = 0, cnt = -1;<a name="line.236"></a>
-<span class="sourceLineNo">237</span>    ByteBuffer[] mbb = new ByteBuffer[endBuffer - startBuffer + 1];<a name="line.237"></a>
-<span class="sourceLineNo">238</span>    for (int i = startBuffer,j=0; i &lt;= endBuffer; ++i,j++) {<a name="line.238"></a>
-<span class="sourceLineNo">239</span>      Lock lock = locks[i];<a name="line.239"></a>
-<span class="sourceLineNo">240</span>      lock.lock();<a name="line.240"></a>
-<span class="sourceLineNo">241</span>      try {<a name="line.241"></a>
-<span class="sourceLineNo">242</span>        ByteBuffer bb = buffers[i];<a name="line.242"></a>
-<span class="sourceLineNo">243</span>        if (i == startBuffer) {<a name="line.243"></a>
-<span class="sourceLineNo">244</span>          cnt = bufferSize - startBufferOffset;<a name="line.244"></a>
-<span class="sourceLineNo">245</span>          if (cnt &gt; len) cnt = len;<a name="line.245"></a>
-<span class="sourceLineNo">246</span>          ByteBuffer dup = bb.duplicate();<a name="line.246"></a>
-<span class="sourceLineNo">247</span>          dup.limit(startBufferOffset + cnt).position(startBufferOffset);<a name="line.247"></a>
-<span class="sourceLineNo">248</span>          mbb[j] = dup.slice();<a name="line.248"></a>
-<span class="sourceLineNo">249</span>        } else if (i == endBuffer) {<a name="line.249"></a>
-<span class="sourceLineNo">250</span>          cnt = endBufferOffset;<a name="line.250"></a>
-<span class="sourceLineNo">251</span>          ByteBuffer dup = bb.duplicate();<a name="line.251"></a>
-<span class="sourceLineNo">252</span>          dup.position(0).limit(cnt);<a name="line.252"></a>
-<span class="sourceLineNo">253</span>          mbb[j] = dup.slice();<a name="line.253"></a>
-<span class="sourceLineNo">254</span>        } else {<a name="line.254"></a>
-<span class="sourceLineNo">255</span>          cnt = bufferSize ;<a name="line.255"></a>
-<span class="sourceLineNo">256</span>          ByteBuffer dup = bb.duplicate();<a name="line.256"></a>
-<span class="sourceLineNo">257</span>          dup.position(0).limit(cnt);<a name="line.257"></a>
-<span class="sourceLineNo">258</span>          mbb[j] = dup.slice();<a name="line.258"></a>
-<span class="sourceLineNo">259</span>        }<a name="line.259"></a>
-<span class="sourceLineNo">260</span>        srcIndex += cnt;<a name="line.260"></a>
-<span class="sourceLineNo">261</span>      } finally {<a name="line.261"></a>
-<span class="sourceLineNo">262</span>        lock.unlock();<a name="line.262"></a>
-<span class="sourceLineNo">263</span>      }<a name="line.263"></a>
-<span class="sourceLineNo">264</span>    }<a name="line.264"></a>
-<span class="sourceLineNo">265</span>    assert srcIndex == len;<a name="line.265"></a>
-<span class="sourceLineNo">266</span>    if (mbb.length &gt; 1) {<a name="line.266"></a>
-<span class="sourceLineNo">267</span>      return new MultiByteBuff(mbb);<a name="line.267"></a>
-<span class="sourceLineNo">268</span>    } else {<a name="line.268"></a>
-<span class="sourceLineNo">269</span>      return new SingleByteBuff(mbb[0]);<a name="line.269"></a>
-<span class="sourceLineNo">270</span>    }<a name="line.270"></a>
-<span class="sourceLineNo">271</span>  }<a name="line.271"></a>
-<span class="sourceLineNo">272</span>}<a name="line.272"></a>
+<span class="sourceLineNo">081</span>  private long roundUp(long n, long to) {<a name="line.81"></a>
+<span class="sourceLineNo">082</span>    return ((n + to - 1) / to) * to;<a name="line.82"></a>
+<span class="sourceLineNo">083</span>  }<a name="line.83"></a>
+<span class="sourceLineNo">084</span><a name="line.84"></a>
+<span class="sourceLineNo">085</span>  /**<a name="line.85"></a>
+<span class="sourceLineNo">086</span>   * Transfers bytes from this buffer array into the given destination array<a name="line.86"></a>
+<span class="sourceLineNo">087</span>   * @param start start position in the ByteBufferArray<a name="line.87"></a>
+<span class="sourceLineNo">088</span>   * @param len The maximum number of bytes to be written to the given array<a name="line.88"></a>
+<span class="sourceLineNo">089</span>   * @param dstArray The array into which bytes are to be written<a name="line.89"></a>
+<span class="sourceLineNo">090</span>   * @return number of bytes read<a name="line.90"></a>
+<span class="sourceLineNo">091</span>   */<a name="line.91"></a>
+<span class="sourceLineNo">092</span>  public int getMultiple(long start, int len, byte[] dstArray) {<a name="line.92"></a>
+<span class="sourceLineNo">093</span>    return getMultiple(start, len, dstArray, 0);<a name="line.93"></a>
+<span class="sourceLineNo">094</span>  }<a name="line.94"></a>
+<span class="sourceLineNo">095</span><a name="line.95"></a>
+<span class="sourceLineNo">096</span>  /**<a name="line.96"></a>
+<span class="sourceLineNo">097</span>   * Transfers bytes from this buffer array into the given destination array<a name="line.97"></a>
+<span class="sourceLineNo">098</span>   * @param start start offset of this buffer array<a name="line.98"></a>
+<span class="sourceLineNo">099</span>   * @param len The maximum number of bytes to be written to the given array<a name="line.99"></a>
+<span class="sourceLineNo">100</span>   * @param dstArray The array into which bytes are to be written<a name="line.100"></a>
+<span class="sourceLineNo">101</span>   * @param dstOffset The offset within the given array of the first byte to be<a name="line.101"></a>
+<span class="sourceLineNo">102</span>   *          written<a name="line.102"></a>
+<span class="sourceLineNo">103</span>   * @return number of bytes read<a name="line.103"></a>
+<span class="sourceLineNo">104</span>   */<a name="line.104"></a>
+<span class="sourceLineNo">105</span>  public int getMultiple(long start, int len, byte[] dstArray, int dstOffset) {<a name="line.105"></a>
+<span class="sourceLineNo">106</span>    multiple(start, len, dstArray, dstOffset, GET_MULTIPLE_VISTOR);<a name="line.106"></a>
+<span class="sourceLineNo">107</span>    return len;<a name="line.107"></a>
+<span class="sourceLineNo">108</span>  }<a name="line.108"></a>
+<span class="sourceLineNo">109</span><a name="line.109"></a>
+<span class="sourceLineNo">110</span>  private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {<a name="line.110"></a>
+<span class="sourceLineNo">111</span>    @Override<a name="line.111"></a>
+<span class="sourceLineNo">112</span>    public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {<a name="line.112"></a>
+<span class="sourceLineNo">113</span>      bb.get(array, arrayIdx, len);<a name="line.113"></a>
+<span class="sourceLineNo">114</span>    }<a name="line.114"></a>
+<span class="sourceLineNo">115</span>  };<a name="line.115"></a>
+<span class="sourceLineNo">116</span><a name="line.116"></a>
+<span class="sourceLineNo">117</span>  /**<a name="line.117"></a>
+<span class="sourceLineNo">118</span>   * Transfers bytes from the given source array into this buffer array<a name="line.118"></a>
+<span class="sourceLineNo">119</span>   * @param start start offset of this buffer array<a name="line.119"></a>
+<span class="sourceLineNo">120</span>   * @param len The maximum number of bytes to be read from the given array<a name="line.120"></a>
+<span class="sourceLineNo">121</span>   * @param srcArray The array from which bytes are to be read<a name="line.121"></a>
+<span class="sourceLineNo">122</span>   */<a name="line.122"></a>
+<span class="sourceLineNo">123</span>  public void putMultiple(long start, int len, byte[] srcArray) {<a name="line.123"></a>
+<span class="sourceLineNo">124</span>    putMultiple(start, len, srcArray, 0);<a name="line.124"></a>
+<span class="sourceLineNo">125</span>  }<a name="line.125"></a>
+<span class="sourceLineNo">126</span><a name="line.126"></a>
+<span class="sourceLineNo">127</span>  /**<a name="line.127"></a>
+<span class="sourceLineNo">128</span>   * Transfers bytes from the given source array into this buffer array<a name="line.128"></a>
+<span class="sourceLineNo">129</span>   * @param start start offset of this buffer array<a name="line.129"></a>
+<span class="sourceLineNo">130</span>   * @param len The maximum number of bytes to be read from the given array<a name="line.130"></a>
+<span class="sourceLineNo">131</span>   * @param srcArray The array from which bytes are to be read<a name="line.131"></a>
+<span class="sourceLineNo">132</span>   * @param srcOffset The offset within the given array of the first byte to be<a name="line.132"></a>
+<span class="sourceLineNo">133</span>   *          read<a name="line.133"></a>
+<span class="sourceLineNo">134</span>   */<a name="line.134"></a>
+<span class="sourceLineNo">135</span>  public void putMultiple(long start, int len, byte[] srcArray, int srcOffset) {<a name="line.135"></a>
+<span class="sourceLineNo">136</span>    multiple(start, len, srcArray, srcOffset, PUT_MULTIPLE_VISITOR);<a name="line.136"></a>
+<span class="sourceLineNo">137</span>  }<a name="line.137"></a>
+<span class="sourceLineNo">138</span><a name="line.138"></a>
+<span class="sourceLineNo">139</span>  private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {<a name="line.139"></a>
+<span class="sourceLineNo">140</span>    @Override<a name="line.140"></a>
+<span class="sourceLineNo">141</span>    public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {<a name="line.141"></a>
+<span class="sourceLineNo">142</span>      bb.put(array, arrayIdx, len);<a name="line.142"></a>
+<span class="sourceLineNo">143</span>    }<a name="line.143"></a>
+<span class="sourceLineNo">144</span>  };<a name="line.144"></a>
+<span class="sourceLineNo">145</span><a name="line.145"></a>
+<span class="sourceLineNo">146</span>  private interface Visitor {<a name="line.146"></a>
+<span class="sourceLineNo">147</span>    /**<a name="line.147"></a>
+<span class="sourceLineNo">148</span>     * Visit the given byte buffer, if it is a read action, we will transfer the<a name="line.148"></a>
+<span class="sourceLineNo">149</span>     * bytes from the buffer to the destination array, else if it is a write<a name="line.149"></a>
+<span class="sourceLineNo">150</span>     * action, we will transfer the bytes from the source array to the buffer<a name="line.150"></a>
+<span class="sourceLineNo">151</span>     * @param bb byte buffer<a name="line.151"></a>
+<span class="sourceLineNo">152</span>     * @param array a source or destination byte array<a name="line.152"></a>
+<span class="sourceLineNo">153</span>     * @param arrayOffset offset of the byte array<a name="line.153"></a>
+<span class="sourceLineNo">154</span>     * @param len read/write length<a name="line.154"></a>
+<span class="sourceLineNo">155</span>     */<a name="line.155"></a>
+<span class="sourceLineNo">156</span>    void visit(ByteBuffer bb, byte[] array, int arrayOffset, int len);<a name="line.156"></a>
+<span class="sourceLineNo">157</span>  }<a name="line.157"></a>
+<span class="sourceLineNo">158</span><a name="line.158"></a>
+<span class="sourceLineNo">159</span>  /**<a name="line.159"></a>
+<span class="sourceLineNo">160</span>   * Access(read or write) this buffer array with a position and length as the<a name="line.160"></a>
+<span class="sourceLineNo">161</span>   * given array. Here we will only lock one buffer even if it may be need visit<a name="line.161"></a>
+<span class="sourceLineNo">162</span>   * several buffers. The consistency is guaranteed by the caller.<a name="line.162"></a>
+<span class="sourceLineNo">163</span>   * @param start start offset of this buffer array<a name="line.163"></a>
+<span class="sourceLineNo">164</span>   * @param len The maximum number of bytes to be accessed<a name="line.164"></a>
+<span class="sourceLineNo">165</span>   * @param array The array from/to which bytes are to be read/written<a name="line.165"></a>
+<span class="sourceLineNo">166</span>   * @param arrayOffset The offset within the given array of the first byte to<a name="line.166"></a>
+<span class="sourceLineNo">167</span>   *          be read or written<a name="line.167"></a>
+<span class="sourceLineNo">168</span>   * @param visitor implement of how to visit the byte buffer<a name="line.168"></a>
+<span class="sourceLineNo">169</span>   */<a name="line.169"></a>
+<span class="sourceLineNo">170</span>  void multiple(long start, int len, byte[] array, int arrayOffset, Visitor visitor) {<a name="line.170"></a>
+<span class="sourceLineNo">171</span>    assert len &gt;= 0;<a name="line.171"></a>
+<span class="sourceLineNo">172</span>    long end = start + len;<a name="line.172"></a>
+<span class="sourceLineNo">173</span>    int startBuffer = (int) (start / bufferSize), startOffset = (int) (start % bufferSize);<a name="line.173"></a>
+<span class="sourceLineNo">174</span>    int endBuffer = (int) (end / bufferSize), endOffset = (int) (end % bufferSize);<a name="line.174"></a>
+<span class="sourceLineNo">175</span>    assert array.length &gt;= len + arrayOffset;<a name="line.175"></a>
+<span class="sourceLineNo">176</span>    assert startBuffer &gt;= 0 &amp;&amp; startBuffer &lt; bufferCount;<a name="line.176"></a>
+<span class="sourceLineNo">177</span>    assert endBuffer &gt;= 0 &amp;&amp; endBuffer &lt; bufferCount<a name="line.177"></a>
+<span class="sourceLineNo">178</span>        || (endBuffer == bufferCount &amp;&amp; endOffset == 0);<a name="line.178"></a>
+<span class="sourceLineNo">179</span>    if (startBuffer &gt;= locks.length || startBuffer &lt; 0) {<a name="line.179"></a>
+<span class="sourceLineNo">180</span>      String msg = "Failed multiple, start=" + start + ",startBuffer="<a name="line.180"></a>
+<span class="sourceLineNo">181</span>          + startBuffer + ",bufferSize=" + bufferSize;<a name="line.181"></a>
+<span class="sourceLineNo">182</span>      LOG.error(msg);<a name="line.182"></a>
+<span class="sourceLineNo">183</span>      throw new RuntimeException(msg);<a name="line.183"></a>
+<span class="sourceLineNo">184</span>    }<a name="line.184"></a>
+<span class="sourceLineNo">185</span>    int srcIndex = 0, cnt = -1;<a name="line.185"></a>
+<span class="sourceLineNo">186</span>    for (int i = startBuffer; i &lt;= endBuffer; ++i) {<a name="line.186"></a>
+<span class="sourceLineNo">187</span>      Lock lock = locks[i];<a name="line.187"></a>
+<span class="sourceLineNo">188</span>      lock.lock();<a name="line.188"></a>
+<span class="sourceLineNo">189</span>      try {<a name="line.189"></a>
+<span class="sourceLineNo">190</span>        ByteBuffer bb = buffers[i];<a name="line.190"></a>
+<span class="sourceLineNo">191</span>        if (i == startBuffer) {<a name="line.191"></a>
+<span class="sourceLineNo">192</span>          cnt = bufferSize - startOffset;<a name="line.192"></a>
+<span class="sourceLineNo">193</span>          if (cnt &gt; len) cnt = len;<a name="line.193"></a>
+<span class="sourceLineNo">194</span>          bb.limit(startOffset + cnt).position(startOffset);<a name="line.194"></a>
+<span class="sourceLineNo">195</span>        } else if (i == endBuffer) {<a name="line.195"></a>
+<span class="sourceLineNo">196</span>          cnt = endOffset;<a name="line.196"></a>
+<span class="sourceLineNo">197</span>          bb.limit(cnt).position(0);<a name="line.197"></a>
+<span class="sourceLineNo">198</span>        } else {<a name="line.198"></a>
+<span class="sourceLineNo">199</span>          cnt = bufferSize;<a name="line.199"></a>
+<span class="sourceLineNo">200</span>          bb.limit(cnt).position(0);<a name="line.200"></a>
+<span class="sourceLineNo">201</span>        }<a name="line.201"></a>
+<span class="sourceLineNo">202</span>        visitor.visit(bb, array, srcIndex + arrayOffset, cnt);<a name="line.202"></a>
+<span class="sourceLineNo">203</span>        srcIndex += cnt;<a name="line.203"></a>
+<span class="sourceLineNo">204</span>      } finally {<a name="line.204"></a>
+<span class="sourceLineNo">205</span>        lock.unlock();<a name="line.205"></a>
+<span class="sourceLineNo">206</span>      }<a name="line.206"></a>
+<span class="sourceLineNo">207</span>    }<a name="line.207"></a>
+<span class="sourceLineNo">208</span>    assert srcIndex == len;<a name="line.208"></a>
+<span class="sourceLineNo">209</span>  }<a name="line.209"></a>
+<span class="sourceLineNo">210</span><a name="line.210"></a>
+<span class="sourceLineNo">211</span>  /**<a name="line.211"></a>
+<span class="sourceLineNo">212</span>   * Creates a ByteBuff from a given array of ByteBuffers from the given offset to the<a name="line.212"></a>
+<span class="sourceLineNo">213</span>   * length specified. For eg, if there are 4 buffers forming an array each with length 10 and<a name="line.213"></a>
+<span class="sourceLineNo">214</span>   * if we call asSubBuffer(5, 10) then we will create an MBB consisting of two BBs<a name="line.214"></a>
+<span class="sourceLineNo">215</span>   * and the first one be a BB from 'position' 5 to a 'length' 5 and the 2nd BB will be from<a name="line.215"></a>
+<span class="sourceLineNo">216</span>   * 'position' 0 to 'length' 5.<a name="line.216"></a>
+<span class="sourceLineNo">217</span>   * @param offset<a name="line.217"></a>
+<span class="sourceLineNo">218</span>   * @param len<a name="line.218"></a>
+<span class="sourceLineNo">219</span>   * @return a ByteBuff formed from the underlying ByteBuffers<a name="line.219"></a>
+<span class="sourceLineNo">220</span>   */<a name="line.220"></a>
+<span class="sourceLineNo">221</span>  public ByteBuff asSubByteBuff(long offset, int len) {<a name="line.221"></a>
+<span class="sourceLineNo">222</span>    assert len &gt;= 0;<a name="line.222"></a>
+<span class="sourceLineNo">223</span>    long end = offset + len;<a name="line.223"></a>
+<span class="sourceLineNo">224</span>    int startBuffer = (int) (offset / bufferSize), startBufferOffset = (int) (offset % bufferSize);<a name="line.224"></a>
+<span class="sourceLineNo">225</span>    int endBuffer = (int) (end / bufferSize), endBufferOffset = (int) (end % bufferSize);<a name="line.225"></a>
+<span class="sourceLineNo">226</span>    // Last buffer in the array is a dummy one with 0 capacity. Avoid sending back that<a name="line.226"></a>
+<span class="sourceLineNo">227</span>    if (endBuffer == this.bufferCount) {<a name="line.227"></a>
+<span class="sourceLineNo">228</span>      endBuffer--;<a name="line.228"></a>
+<span class="sourceLineNo">229</span>      endBufferOffset = bufferSize;<a name="line.229"></a>
+<span class="sourceLineNo">230</span>    }<a name="line.230"></a>
+<span class="sourceLineNo">231</span>    assert startBuffer &gt;= 0 &amp;&amp; startBuffer &lt; bufferCount;<a name="line.231"></a>
+<span class="sourceLineNo">232</span>    assert endBuffer &gt;= 0 &amp;&amp; endBuffer &lt; bufferCount<a name="line.232"></a>
+<span class="sourceLineNo">233</span>        || (endBuffer == bufferCount &amp;&amp; endBufferOffset == 0);<a name="line.233"></a>
+<span class="sourceLineNo">234</span>    if (startBuffer &gt;= locks.length || startBuffer &lt; 0) {<a name="line.234"></a>
+<span class="sourceLineNo">235</span>      String msg = "Failed subArray, start=" + offset + ",startBuffer=" + startBuffer<a name="line.235"></a>
+<span class="sourceLineNo">236</span>          + ",bufferSize=" + bufferSize;<a name="line.236"></a>
+<span class="sourceLineNo">237</span>      LOG.error(msg);<a name="line.237"></a>
+<span class="sourceLineNo">238</span>      throw new RuntimeException(msg);<a name="line.238"></a>
+<span class="sourceLineNo">239</span>    }<a name="line.239"></a>
+<span class="sourceLineNo">240</span>    int srcIndex = 0, cnt = -1;<a name="line.240"></a>
+<span class="sourceLineNo">241</span>    ByteBuffer[] mbb = new ByteBuffer[endBuffer - startBuffer + 1];<a name="line.241"></a>
+<span class="sourceLineNo">242</span>    for (int i = startBuffer,j=0; i &lt;= endBuffer; ++i,j++) {<a name="line.242"></a>
+<span class="sourceLineNo">243</span>      Lock lock = locks[i];<a name="line.243"></a>
+<span class="sourceLineNo">244</span>      lock.lock();<a name="line.244"></a>
+<span class="sourceLineNo">245</span>      try {<a name="line.245"></a>
+<span class="sourceLineNo">246</span>        ByteBuffer bb = buffers[i];<a name="line.246"></a>
+<span class="sourceLineNo">247</span>        if (i == startBuffer) {<a name="line.247"></a>
+<span class="sourceLineNo">248</span>          cnt = bufferSize - startBufferOffset;<a name="line.248"></a>
+<span class="sourceLineNo">249</span>          if (cnt &gt; len) cnt = len;<a name="line.249"></a>
+<span class="sourceLineNo">250</span>          ByteBuffer dup = bb.duplicate();<a name="line.250"></a>
+<span class="sourceLineNo">251</span>          dup.limit(startBufferOffset + cnt).position(startBufferOffset);<a name="line.251"></a>
+<span class="sourceLineNo">252</span>          mbb[j] = dup.slice();<a name="line.252"></a>
+<span class="sourceLineNo">253</span>        } else if (i == endBuffer) {<a name="line.253"></a>
+<span class="sourceLineNo">254</span>          cnt = endBufferOffset;<a name="line.254"></a>
+<span class="sourceLineNo">255</span>          ByteBuffer dup = bb.duplicate();<a name="line.255"></a>
+<span class="sourceLineNo">256</span>          dup.position(0).limit(cnt);<a name="line.256"></a>
+<span class="sourceLineNo">257</span>          mbb[j] = dup.slice();<a name="line.257"></a>
+<span class="sourceLineNo">258</span>        } else {<a name="line.258"></a>
+<span class="sourceLineNo">259</span>          cnt = bufferSize ;<a name="line.259"></a>
+<span class="sourceLineNo">260</span>          ByteBuffer dup = bb.duplicate();<a name="line.260"></a>
+<span class="sourceLineNo">261</span>          dup.position(0).limit(cnt);<a name="line.261"></a>
+<span class="sourceLineNo">262</span>          mbb[j] = dup.slice();<a name="line.262"></a>
+<span class="sourceLineNo">263</span>        }<a name="line.263"></a>
+<span class="sourceLineNo">264</span>        srcIndex += cnt;<a name="line.264"></a>
+<span class="sourceLineNo">265</span>      } finally {<a name="line.265"></a>
+<span class="sourceLineNo">266</span>        lock.unlock();<a name="line.266"></a>
+<span class="sourceLineNo">267</span>      }<a name="line.267"></a>
+<span class="sourceLineNo">268</span>    }<a name="line.268"></a>
+<span class="sourceLineNo">269</span>    assert srcIndex == len;<a name="line.269"></a>
+<span class="sourceLineNo">270</span>    if (mbb.length &gt; 1) {<a name="line.270"></a>
+<span class="sourceLineNo">271</span>      return new MultiByteBuff(mbb);<a name="line.271"></a>
+<span class="sourceLineNo">272</span>    } else {<a name="line.272"></a>
+<span class="sourceLineNo">273</span>      return new SingleByteBuff(mbb[0]);<a name="line.273"></a>
+<span class="sourceLineNo">274</span>    }<a name="line.274"></a>
+<span class="sourceLineNo">275</span>  }<a name="line.275"></a>
+<span class="sourceLineNo">276</span>}<a name="line.276"></a>
 
 
 


Mime
View raw message