⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iobuffer.html

📁 MINA+API 关于MINA的API
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc (build 1.6.0_04) on Tue Feb 19 10:50:42 MST 2008 --><META http-equiv="Content-Type" content="text/html; charset=UTF-8"><TITLE>IoBuffer (Apache MINA 2.0.0-M1 API Documentation)</TITLE><META NAME="date" CONTENT="2008-02-19"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){    if (location.href.indexOf('is-external=true') == -1) {        parent.document.title="IoBuffer (Apache MINA 2.0.0-M1 API Documentation)";    }}</SCRIPT><NOSCRIPT></NOSCRIPT></HEAD><BODY BGCOLOR="white" onload="windowTitle();"><HR><!-- ========= START OF TOP NAVBAR ======= --><A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="Skip navigation links"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY=""><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/IoBuffer.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="../../../../org/apache/mina/common/IoAcceptor.html" title="interface in org.apache.mina.common"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../../../org/apache/mina/common/IoBufferAllocator.html" title="interface in org.apache.mina.common"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../../../index.html?org/apache/mina/common/IoBuffer.html" target="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="IoBuffer.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;&nbsp;<SCRIPT type="text/javascript">  <!--  if(window==top) {    document.writeln('<A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A>');  }  //--></SCRIPT><NOSCRIPT>  <A HREF="../../../../allclasses-noframe.html"><B>All Classes</B></A></NOSCRIPT></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL:&nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">org.apache.mina.common</FONT><BR>Class IoBuffer</H2><PRE><A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">java.lang.Object</A>  <IMG SRC="../../../../resources/inherit.gif" ALT="extended by "><B>org.apache.mina.common.IoBuffer</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html?is-external=true" title="class or interface in java.lang">Comparable</A>&lt;<A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common">IoBuffer</A>&gt;</DD></DL><DL><DT><B>Direct Known Subclasses:</B> <DD><A HREF="../../../../org/apache/mina/common/AbstractIoBuffer.html" title="class in org.apache.mina.common">AbstractIoBuffer</A>, <A HREF="../../../../org/apache/mina/common/IoBufferWrapper.html" title="class in org.apache.mina.common">IoBufferWrapper</A></DD></DL><HR><DL><DT><PRE>public abstract class <B>IoBuffer</B><DT>extends <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html?is-external=true" title="class or interface in java.lang">Object</A><DT>implements <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html?is-external=true" title="class or interface in java.lang">Comparable</A>&lt;<A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common">IoBuffer</A>&gt;</DL></PRE><P>A byte buffer used by MINA applications. <p> This is a replacement for <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A>. Please refer to <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A> documentation for preliminary usage.  MINA does not use NIO <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A> directly for two reasons: <ul> <li>It doesn't provide useful getters and putters such as <code>fill</code>, <code>get/putString</code>, and <code>get/putAsciiInt()</code> enough.</li> <li>It is difficult to write variable-length data due to its fixed capacity</li> </ul> </p> <h2>Allocation</h2> <p> You can allocate a new heap buffer. <pre> IoBuffer buf = IoBuffer.allocate(1024, false); </pre> you can also allocate a new direct buffer: <pre> IoBuffer buf = IoBuffer.allocate(1024, true); </pre> or you can set the default buffer type. <pre> // Allocate heap buffer by default. IoBuffer.setUseDirectBuffer(false); // A new heap buffer is returned. IoBuffer buf = IoBuffer.allocate(1024); </pre> </p> <h2>Wrapping existing NIO buffers and arrays</h2> <p> This class provides a few <tt>wrap(...)</tt> methods that wraps any NIO buffers and byte arrays. <h2>AutoExpand</h2> <p> Writing variable-length data using NIO <tt>ByteBuffers</tt> is not really easy, and it is because its size is fixed.  <A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common"><CODE>IoBuffer</CODE></A> introduces  <tt>autoExpand</tt> property.  If <tt>autoExpand</tt> property is true, you never get <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/BufferOverflowException.html?is-external=true" title="class or interface in java.nio"><CODE>BufferOverflowException</CODE></A> or <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IndexOutOfBoundsException.html?is-external=true" title="class or interface in java.lang"><CODE>IndexOutOfBoundsException</CODE></A> (except when index is negative). It automatically expands its capacity and limit value.  For example: <pre> String greeting = messageBundle.getMessage( "hello" ); IoBuffer buf = IoBuffer.allocate( 16 ); // Turn on autoExpand (it is off by default) buf.setAutoExpand( true ); buf.putString( greeting, utf8encoder ); </pre> The underlying <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A> is reallocated by <A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common"><CODE>IoBuffer</CODE></A> behind the scene if the encoded data is larger than 16 bytes in the example above. Its capacity will double, and its limit will increase to the last position the string is written. </p>  <h2>AutoShrink</h2> <p> You might also want to decrease the capacity of the buffer when most of the allocated memory area is not being used.  <A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common"><CODE>IoBuffer</CODE></A> provides <tt>autoShrink</tt> property to take care of this issue.  If  <tt>autoShrink</tt> is turned on, <A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common"><CODE>IoBuffer</CODE></A> halves the capacity of the buffer when <A HREF="../../../../org/apache/mina/common/IoBuffer.html#compact()"><CODE>compact()</CODE></A> is invoked and only 1/4 or less of the current capacity is being used. <p> You can also <A HREF="../../../../org/apache/mina/common/IoBuffer.html#shrink()"><CODE>shrink()</CODE></A> method manually to shrink the capacity of the buffer. <p>  The underlying <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A> is reallocated by <A HREF="../../../../org/apache/mina/common/IoBuffer.html" title="class in org.apache.mina.common"><CODE>IoBuffer</CODE></A> behind the scene, and therefore <A HREF="../../../../org/apache/mina/common/IoBuffer.html#buf()"><CODE>buf()</CODE></A> will return a different <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html?is-external=true" title="class or interface in java.nio"><CODE>ByteBuffer</CODE></A> instance once capacity changes.  Please also note <A HREF="../../../../org/apache/mina/common/IoBuffer.html#compact()"><CODE>compact()</CODE></A> or <A HREF="../../../../org/apache/mina/common/IoBuffer.html#shrink()"><CODE>shrink()</CODE></A> will not decrease the capacity if the new capacity is less than the <A HREF="../../../../org/apache/mina/common/IoBuffer.html#minimumCapacity()"><CODE>minimumCapacity()</CODE></A> of the buffer. <h2>Derived Buffers</h2> <p> Derived buffers are the buffers which were created by <A HREF="../../../../org/apache/mina/common/IoBuffer.html#duplicate()"><CODE>duplicate()</CODE></A>, <A HREF="../../../../org/apache/mina/common/IoBuffer.html#slice()"><CODE>slice()</CODE></A>, or <A HREF="../../../../org/apache/mina/common/IoBuffer.html#asReadOnlyBuffer()"><CODE>asReadOnlyBuffer()</CODE></A>. They are useful especially when you broadcast the same messages to multiple <A HREF="../../../../org/apache/mina/common/IoSession.html" title="interface in org.apache.mina.common"><CODE>IoSession</CODE></A>s.  Please note that the buffer derived from and its derived buffers are not both auto-expandable neither auto-shrinkable. Trying to call <A HREF="../../../../org/apache/mina/common/IoBuffer.html#setAutoExpand(boolean)"><CODE>setAutoExpand(boolean)</CODE></A> or <A HREF="../../../../org/apache/mina/common/IoBuffer.html#setAutoShrink(boolean)"><CODE>setAutoShrink(boolean)</CODE></A> with <tt>true</tt> parameter will raise an <A HREF="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IllegalStateException.html?is-external=true" title="class or interface in java.lang"><CODE>IllegalStateException</CODE></A>. </p> <h2>Changing Buffer Allocation Policy</h2> <p> <A HREF="../../../../org/apache/mina/common/IoBufferAllocator.html" title="interface in org.apache.mina.common"><CODE>IoBufferAllocator</CODE></A> interface lets you override the default buffer management behavior.  There are two allocators provided out-of-the-box: <ul> <li><A HREF="../../../../org/apache/mina/common/SimpleBufferAllocator.html" title="class in org.apache.mina.common"><CODE>SimpleBufferAllocator</CODE></A> (default)</li> <li><A HREF="../../../../org/apache/mina/common/CachedBufferAllocator.html" title="class in org.apache.mina.common"><CODE>CachedBufferAllocator</CODE></A></li> </ul> You can implement your own allocator and use it by calling <A HREF="../../../../org/apache/mina/common/IoBuffer.html#setAllocator(org.apache.mina.common.IoBufferAllocator)"><CODE>setAllocator(IoBufferAllocator)</CODE></A>. </p><P><P><DL><DT><B>Version:</B></DT>  <DD>$Rev: 602109 $, $Date: 2007-12-07 07:40:41 -0700 (Fri, 07 Dec 2007) $</DD><DT><B>Author:</B></DT>  <DD>The Apache MINA Project (dev@mina.apache.org)</DD></DL><HR><P><!-- =========== FIELD SUMMARY =========== --><A NAME="field_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -