📄 iobuffer.html
字号:
<a name="114" href="#114">114</a> <em class="jxr_javadoccomment"> * The underlying {@link ByteBuffer} is reallocated by {@link IoBuffer} behind</em><a name="115" href="#115">115</a> <em class="jxr_javadoccomment"> * the scene, and therefore {@link #buf()} will return a different</em><a name="116" href="#116">116</a> <em class="jxr_javadoccomment"> * {@link ByteBuffer} instance once capacity changes. Please also note</em><a name="117" href="#117">117</a> <em class="jxr_javadoccomment"> * {@link #compact()} or {@link #shrink()} will not decrease the capacity if</em><a name="118" href="#118">118</a> <em class="jxr_javadoccomment"> * the new capacity is less than the {@link #minimumCapacity()} of the buffer.</em><a name="119" href="#119">119</a> <em class="jxr_javadoccomment"> *</em><a name="120" href="#120">120</a> <em class="jxr_javadoccomment"> * <h2>Derived Buffers</h2></em><a name="121" href="#121">121</a> <em class="jxr_javadoccomment"> * <p></em><a name="122" href="#122">122</a> <em class="jxr_javadoccomment"> * Derived buffers are the buffers which were created by</em><a name="123" href="#123">123</a> <em class="jxr_javadoccomment"> * {@link #duplicate()}, {@link #slice()}, or {@link #asReadOnlyBuffer()}.</em><a name="124" href="#124">124</a> <em class="jxr_javadoccomment"> * They are useful especially when you broadcast the same messages to</em><a name="125" href="#125">125</a> <em class="jxr_javadoccomment"> * multiple {@link IoSession}s. Please note that the buffer derived from and</em><a name="126" href="#126">126</a> <em class="jxr_javadoccomment"> * its derived buffers are not both auto-expandable neither auto-shrinkable.</em><a name="127" href="#127">127</a> <em class="jxr_javadoccomment"> * Trying to call {@link #setAutoExpand(boolean)} or {@link #setAutoShrink(boolean)}</em><a name="128" href="#128">128</a> <em class="jxr_javadoccomment"> * with <tt>true</tt> parameter will raise an {@link IllegalStateException}.</em><a name="129" href="#129">129</a> <em class="jxr_javadoccomment"> * </p></em><a name="130" href="#130">130</a> <em class="jxr_javadoccomment"> *</em><a name="131" href="#131">131</a> <em class="jxr_javadoccomment"> * <h2>Changing Buffer Allocation Policy</h2></em><a name="132" href="#132">132</a> <em class="jxr_javadoccomment"> * <p></em><a name="133" href="#133">133</a> <em class="jxr_javadoccomment"> * {@link IoBufferAllocator} interface lets you override the default buffer</em><a name="134" href="#134">134</a> <em class="jxr_javadoccomment"> * management behavior. There are two allocators provided out-of-the-box:</em><a name="135" href="#135">135</a> <em class="jxr_javadoccomment"> * <ul></em><a name="136" href="#136">136</a> <em class="jxr_javadoccomment"> * <li>{@link SimpleBufferAllocator} (default)</li></em><a name="137" href="#137">137</a> <em class="jxr_javadoccomment"> * <li>{@link CachedBufferAllocator}</li></em><a name="138" href="#138">138</a> <em class="jxr_javadoccomment"> * </ul></em><a name="139" href="#139">139</a> <em class="jxr_javadoccomment"> * You can implement your own allocator and use it by calling</em><a name="140" href="#140">140</a> <em class="jxr_javadoccomment"> * {@link #setAllocator(IoBufferAllocator)}.</em><a name="141" href="#141">141</a> <em class="jxr_javadoccomment"> * </p></em><a name="142" href="#142">142</a> <em class="jxr_javadoccomment"> *</em><a name="143" href="#143">143</a> <em class="jxr_javadoccomment"> * @author The Apache MINA Project (dev@mina.apache.org)</em><a name="144" href="#144">144</a> <em class="jxr_javadoccomment"> * @version $Rev: 602109 $, $Date: 2007-12-07 07:40:41 -0700 (Fri, 07 Dec 2007) $</em><a name="145" href="#145">145</a> <em class="jxr_javadoccomment"> */</em><a name="146" href="#146">146</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">abstract</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> implements Comparable<IoBuffer> {<a name="147" href="#147">147</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <a href="../../../../org/apache/mina/common/IoBufferAllocator.html">IoBufferAllocator</a> allocator = <strong class="jxr_keyword">new</strong> <a href="../../../../org/apache/mina/common/SimpleBufferAllocator.html">SimpleBufferAllocator</a>();<a name="148" href="#148">148</a> <a name="149" href="#149">149</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">final</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> EMPTY_DIRECT_BUFFER = allocator.allocate(0,<a name="150" href="#150">150</a> <strong class="jxr_keyword">true</strong>);<a name="151" href="#151">151</a> <a name="152" href="#152">152</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">final</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> EMPTY_HEAP_BUFFER = allocator.allocate(0,<a name="153" href="#153">153</a> false);<a name="154" href="#154">154</a> <a name="155" href="#155">155</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">boolean</strong> useDirectBuffer = false;<a name="156" href="#156">156</a> <a name="157" href="#157">157</a> <em class="jxr_javadoccomment">/**</em><a name="158" href="#158">158</a> <em class="jxr_javadoccomment"> * An immutable empty buffer.</em><a name="159" href="#159">159</a> <em class="jxr_javadoccomment"> */</em><a name="160" href="#160">160</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">final</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> EMPTY_BUFFER = wrap(<strong class="jxr_keyword">new</strong> byte[0]);<a name="161" href="#161">161</a> <a name="162" href="#162">162</a> <em class="jxr_javadoccomment">/**</em><a name="163" href="#163">163</a> <em class="jxr_javadoccomment"> * Returns the allocator used by existing and new buffers</em><a name="164" href="#164">164</a> <em class="jxr_javadoccomment"> */</em><a name="165" href="#165">165</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <a href="../../../../org/apache/mina/common/IoBufferAllocator.html">IoBufferAllocator</a> getAllocator() {<a name="166" href="#166">166</a> <strong class="jxr_keyword">return</strong> allocator;<a name="167" href="#167">167</a> }<a name="168" href="#168">168</a> <a name="169" href="#169">169</a> <em class="jxr_javadoccomment">/**</em><a name="170" href="#170">170</a> <em class="jxr_javadoccomment"> * Sets the allocator used by existing and new buffers</em><a name="171" href="#171">171</a> <em class="jxr_javadoccomment"> */</em><a name="172" href="#172">172</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">void</strong> setAllocator(<a href="../../../../org/apache/mina/common/IoBufferAllocator.html">IoBufferAllocator</a> newAllocator) {<a name="173" href="#173">173</a> <strong class="jxr_keyword">if</strong> (newAllocator == <strong class="jxr_keyword">null</strong>) {<a name="174" href="#174">174</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> NullPointerException(<span class="jxr_string">"allocator"</span>);<a name="175" href="#175">175</a> }<a name="176" href="#176">176</a> <a name="177" href="#177">177</a> <a href="../../../../org/apache/mina/common/IoBufferAllocator.html">IoBufferAllocator</a> oldAllocator = allocator;<a name="178" href="#178">178</a> <a name="179" href="#179">179</a> allocator = newAllocator;<a name="180" href="#180">180</a> <a name="181" href="#181">181</a> <strong class="jxr_keyword">if</strong> (<strong class="jxr_keyword">null</strong> != oldAllocator) {<a name="182" href="#182">182</a> oldAllocator.dispose();<a name="183" href="#183">183</a> }<a name="184" href="#184">184</a> }<a name="185" href="#185">185</a> <a name="186" href="#186">186</a> <em class="jxr_javadoccomment">/**</em><a name="187" href="#187">187</a> <em class="jxr_javadoccomment"> * Returns <tt>true</tt> if and only if a direct buffer is allocated</em><a name="188" href="#188">188</a> <em class="jxr_javadoccomment"> * by default when the type of the new buffer is not specified. The</em><a name="189" href="#189">189</a> <em class="jxr_javadoccomment"> * default value is <tt>false</tt>.</em><a name="190" href="#190">190</a> <em class="jxr_javadoccomment"> */</em><a name="191" href="#191">191</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">boolean</strong> isUseDirectBuffer() {<a name="192" href="#192">192</a> <strong class="jxr_keyword">return</strong> useDirectBuffer;<a name="193" href="#193">193</a> }<a name="194" href="#194">194</a> <a name="195" href="#195">195</a> <em class="jxr_javadoccomment">/**</em><a name="196" href="#196">196</a> <em class="jxr_javadoccomment"> * Sets if a direct buffer should be allocated by default when the</em><a name="197" href="#197">197</a> <em class="jxr_javadoccomment"> * type of the new buffer is not specified. The default value is</em><a name="198" href="#198">198</a> <em class="jxr_javadoccomment"> * <tt>false</tt>.</em><a name="199" href="#199">199</a> <em class="jxr_javadoccomment"> */</em><a name="200" href="#200">200</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <strong class="jxr_keyword">void</strong> setUseDirectBuffer(<strong class="jxr_keyword">boolean</strong> useDirectBuffer) {<a name="201" href="#201">201</a> IoBuffer.useDirectBuffer = useDirectBuffer;<a name="202" href="#202">202</a> }<a name="203" href="#203">203</a> <a name="204" href="#204">204</a> <em class="jxr_javadoccomment">/**</em><a name="205" href="#205">205</a> <em class="jxr_javadoccomment"> * Returns the direct or heap buffer which is capable to store the</em><a name="206" href="#206">206</a> <em class="jxr_javadoccomment"> * specified amount of bytes.</em><a name="207" href="#207">207</a> <em class="jxr_javadoccomment"> *</em><a name="208" href="#208">208</a> <em class="jxr_javadoccomment"> * @param capacity the capacity of the buffer</em><a name="209" href="#209">209</a> <em class="jxr_javadoccomment"> *</em><a name="210" href="#210">210</a> <em class="jxr_javadoccomment"> * @see #setUseDirectBuffer(boolean)</em><a name="211" href="#211">211</a> <em class="jxr_javadoccomment"> */</em><a name="212" href="#212">212</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> allocate(<strong class="jxr_keyword">int</strong> capacity) {<a name="213" href="#213">213</a> <strong class="jxr_keyword">return</strong> allocate(capacity, useDirectBuffer);<a name="214" href="#214">214</a> }<a name="215" href="#215">215</a> <a name="216" href="#216">216</a> <em class="jxr_javadoccomment">/**</em><a name="217" href="#217">217</a> <em class="jxr_javadoccomment"> * Returns the buffer which is capable of the specified size.</em><a name="218" href="#218">218</a> <em class="jxr_javadoccomment"> *</em><a name="219" href="#219">219</a> <em class="jxr_javadoccomment"> * @param capacity the capacity of the buffer</em><a name="220" href="#220">220</a> <em class="jxr_javadoccomment"> * @param direct <tt>true</tt> to get a direct buffer,</em><a name="221" href="#221">221</a> <em class="jxr_javadoccomment"> * <tt>false</tt> to get a heap buffer.</em><a name="222" href="#222">222</a> <em class="jxr_javadoccomment"> */</em><a name="223" href="#223">223</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">static</strong> <a href="../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> allocate(<strong class="jxr_keyword">int</strong> capacity, <strong class="jxr_keyword">boolean</strong> direct) {<a name="224" href="#224">224</a> <strong class="jxr_keyword">if</strong> (capacity == 0) {<a name="225" href="#225">225</a> <strong class="jxr_keyword">return</strong> direct ? EMPTY_DIRECT_BUFFER : EMPTY_HEAP_BUFFER;<a name="226" href="#226">226</a> }<a name="227" href="#227">227</a> <a name="228" href="#228">228</a> <strong class="jxr_keyword">if</strong> (capacity < 0) {<a name="229" href="#229">229</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalArgumentException(<span class="jxr_string">"capacity: "</span> + capacity);<a name="230" href="#230">230</a> }<a name="231" href="#231">231</a> <a name="232" href="#232">232</a> <strong class="jxr_keyword">return</strong> allocator.allocate(capacity, direct);<a name="233" href="#233">233</a> }<a name="234" href="#234">234</a> <a name="235" href="#235">235</a> <em class="jxr_javadoccomment">/**</em><a name="236" href="#236">236</a> <em class="jxr_javadoccomment"> * Wraps the specified NIO {@link ByteBuffer} into MINA buffer.</em>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -