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

📄 iobuffer.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one *  or more contributor license agreements.  See the NOTICE file *  distributed with this work for additional information *  regarding copyright ownership.  The ASF licenses this file *  to you under the Apache License, Version 2.0 (the *  "License"); you may not use this file except in compliance *  with the License.  You may obtain a copy of the License at * *    http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, *  software distributed under the License is distributed on an *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *  KIND, either express or implied.  See the License for the *  specific language governing permissions and limitations *  under the License. * */package org.apache.mina.core.buffer;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.BufferOverflowException;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.nio.CharBuffer;import java.nio.DoubleBuffer;import java.nio.FloatBuffer;import java.nio.IntBuffer;import java.nio.LongBuffer;import java.nio.ReadOnlyBufferException;import java.nio.ShortBuffer;import java.nio.charset.CharacterCodingException;import java.nio.charset.CharsetDecoder;import java.nio.charset.CharsetEncoder;import java.util.EnumSet;import java.util.Set;import org.apache.mina.core.session.IoSession;/** * A byte buffer used by MINA applications. * <p> * This is a replacement for {@link ByteBuffer}. Please refer to * {@link ByteBuffer} documentation for preliminary usage.  MINA does * not use NIO {@link ByteBuffer} 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.  {@link IoBuffer} introduces  * <tt>autoExpand</tt> property.  If <tt>autoExpand</tt> property is true, you * never get {@link BufferOverflowException} or * {@link IndexOutOfBoundsException} (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 {@link ByteBuffer} is reallocated by {@link IoBuffer} 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.  {@link IoBuffer} provides * <tt>autoShrink</tt> property to take care of this issue.  If  * <tt>autoShrink</tt> is turned on, {@link IoBuffer} halves the capacity * of the buffer when {@link #compact()} is invoked and only 1/4 or less of * the current capacity is being used. * <p> * You can also {@link #shrink()} method manually to shrink the capacity of * the buffer. * <p>  * The underlying {@link ByteBuffer} is reallocated by {@link IoBuffer} behind * the scene, and therefore {@link #buf()} will return a different * {@link ByteBuffer} instance once capacity changes.  Please also note * {@link #compact()} or {@link #shrink()} will not decrease the capacity if * the new capacity is less than the {@link #minimumCapacity()} of the buffer. * * <h2>Derived Buffers</h2> * <p> * Derived buffers are the buffers which were created by * {@link #duplicate()}, {@link #slice()}, or {@link #asReadOnlyBuffer()}. * They are useful especially when you broadcast the same messages to * multiple {@link IoSession}s.  Please note that the buffer derived from and * its derived buffers are not both auto-expandable neither auto-shrinkable. * Trying to call {@link #setAutoExpand(boolean)} or {@link #setAutoShrink(boolean)} * with <tt>true</tt> parameter will raise an {@link IllegalStateException}. * </p> * * <h2>Changing Buffer Allocation Policy</h2> * <p> * {@link IoBufferAllocator} interface lets you override the default buffer * management behavior.  There are two allocators provided out-of-the-box: * <ul> * <li>{@link SimpleBufferAllocator} (default)</li> * <li>{@link CachedBufferAllocator}</li> * </ul> * You can implement your own allocator and use it by calling * {@link #setAllocator(IoBufferAllocator)}. * </p> * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 748525 $, $Date: 2009-02-27 14:45:31 +0100 (Fri, 27 Feb 2009) $ */public abstract class IoBuffer implements Comparable<IoBuffer> {    /** The allocator used to create new buffers */    private static IoBufferAllocator allocator = new SimpleBufferAllocator();    /** A flag indicating which type of buffer we are using : heap or direct */    private static boolean useDirectBuffer = false;    /**     * Returns the allocator used by existing and new buffers     */    public static IoBufferAllocator getAllocator() {        return allocator;    }    /**     * Sets the allocator used by existing and new buffers     */    public static void setAllocator(IoBufferAllocator newAllocator) {        if (newAllocator == null) {            throw new NullPointerException("allocator");        }        IoBufferAllocator oldAllocator = allocator;        allocator = newAllocator;        if (null != oldAllocator) {            oldAllocator.dispose();        }    }    /**     * Returns <tt>true</tt> if and only if a direct buffer is allocated     * by default when the type of the new buffer is not specified.  The     * default value is <tt>false</tt>.     */    public static boolean isUseDirectBuffer() {        return useDirectBuffer;    }    /**     * Sets if a direct buffer should be allocated by default when the     * type of the new buffer is not specified.  The default value is     * <tt>false</tt>.     */    public static void setUseDirectBuffer(boolean useDirectBuffer) {        IoBuffer.useDirectBuffer = useDirectBuffer;    }    /**     * Returns the direct or heap buffer which is capable to store the     * specified amount of bytes.     *     * @param capacity the capacity of the buffer     *     * @see #setUseDirectBuffer(boolean)     */    public static IoBuffer allocate(int capacity) {        return allocate(capacity, useDirectBuffer);    }    /**     * Returns the buffer which is capable of the specified size.     *     * @param capacity the capacity of the buffer     * @param direct   <tt>true</tt> to get a direct buffer,     *                 <tt>false</tt> to get a heap buffer.     */    public static IoBuffer allocate(int capacity, boolean direct) {        if (capacity < 0) {            throw new IllegalArgumentException("capacity: " + capacity);        }        return allocator.allocate(capacity, direct);    }    /**     * Wraps the specified NIO {@link ByteBuffer} into MINA buffer.     */    public static IoBuffer wrap(ByteBuffer nioBuffer) {        return allocator.wrap(nioBuffer);    }    /**     * Wraps the specified byte array into MINA heap buffer.     */    public static IoBuffer wrap(byte[] byteArray) {        return wrap(ByteBuffer.wrap(byteArray));    }    /**     * Wraps the specified byte array into MINA heap buffer.     */    public static IoBuffer wrap(byte[] byteArray, int offset, int length) {        return wrap(ByteBuffer.wrap(byteArray, offset, length));    }    /**     * Normalizes the specified capacity of the buffer to power of 2,     * which is often helpful for optimal memory usage and performance.      * If it is greater than or equal to {@link Integer#MAX_VALUE}, it     * returns {@link Integer#MAX_VALUE}.  If it is zero, it returns zero.     */    protected static int normalizeCapacity(int requestedCapacity) {        switch (requestedCapacity) {        case 0:        case 1 << 0:        case 1 << 1:        case 1 << 2:        case 1 << 3:        case 1 << 4:        case 1 << 5:        case 1 << 6:        case 1 << 7:        case 1 << 8:        case 1 << 9:        case 1 << 10:        case 1 << 11:        case 1 << 12:        case 1 << 13:        case 1 << 14:        case 1 << 15:        case 1 << 16:        case 1 << 17:        case 1 << 18:        case 1 << 19:        case 1 << 21:        case 1 << 22:        case 1 << 23:        case 1 << 24:        case 1 << 25:        case 1 << 26:        case 1 << 27:        case 1 << 28:        case 1 << 29:        case 1 << 30:        case Integer.MAX_VALUE:            return requestedCapacity;        }        int newCapacity = 1;        while (newCapacity < requestedCapacity) {            newCapacity <<= 1;            if (newCapacity < 0) {                return Integer.MAX_VALUE;            }        }        return newCapacity;    }        /**     * Creates a new instance.  This is an empty constructor.     */    protected IoBuffer() {    }    /**     * Declares this buffer and all its derived buffers are not used anymore     * so that it can be reused by some {@link IoBufferAllocator} implementations.     * It is not mandatory to call this method, but you might want to invoke this     * method for maximum performance.      */    public abstract void free();    /**     * Returns the underlying NIO buffer instance.     */    public abstract ByteBuffer buf();    /**     * @see ByteBuffer#isDirect()     */    public abstract boolean isDirect();    /**     * returns <tt>true</tt> if and only if this buffer is derived from other buffer     * via {@link #duplicate()}, {@link #slice()} or {@link #asReadOnlyBuffer()}.     */    public abstract boolean isDerived();    /**     * @see ByteBuffer#isReadOnly()     */    public abstract boolean isReadOnly();    /**     * Returns the minimum capacity of this buffer which is used to determine     * the new capacity of the buffer shrunk by {@link #compact()} and     * {@link #shrink()} operation.  The default value is the initial capacity     * of the buffer.     */    public abstract int minimumCapacity();    /**     * Sets the minimum capacity of this buffer which is used to determine     * the new capacity of the buffer shrunk by {@link #compact()} and     * {@link #shrink()} operation.  The default value is the initial capacity     * of the buffer.     */    public abstract IoBuffer minimumCapacity(int minimumCapacity);    /**     * @see ByteBuffer#capacity()     */    public abstract int capacity();    /**     * Increases the capacity of this buffer.  If the new capacity is less than     * or equal to the current capacity, this method returns silently.  If the     * new capacity is greater than the current capacity, the buffer is     * reallocated while retaining the position, limit, mark and the content     * of the buffer.     */    public abstract IoBuffer capacity(int newCapacity);    /**     * Returns <tt>true</tt> if and only if <tt>autoExpand</tt> is turned on.     */    public abstract boolean isAutoExpand();    /**     * Turns on or off <tt>autoExpand</tt>.     */    public abstract IoBuffer setAutoExpand(boolean autoExpand);    /**     * Returns <tt>true</tt> if and only if <tt>autoShrink</tt> is turned on.     */    public abstract boolean isAutoShrink();    /**     * Turns on or off <tt>autoShrink</tt>.     */    public abstract IoBuffer setAutoShrink(boolean autoShrink);    /**     * Changes the capacity and limit of this buffer so this buffer get     * the specified <tt>expectedRemaining</tt> room from the current position.     * This method works even if you didn't set <tt>autoExpand</tt> to     * <tt>true</tt>.     */    public abstract IoBuffer expand(int expectedRemaining);    /**     * Changes the capacity and limit of this buffer so this buffer get     * the specified <tt>expectedRemaining</tt> room from the specified     * <tt>position</tt>.     * This method works even if you didn't set <tt>autoExpand</tt> to     * <tt>true</tt>.     */    public abstract IoBuffer expand(int position, int expectedRemaining);    /**     * Changes the capacity of this buffer so this buffer occupies as less     * memory as possible while retaining the position, limit and the     * buffer content between the position and limit.  The capacity of the     * buffer never becomes less than {@link #minimumCapacity()}.     * The mark is discarded once the capacity changes.     */    public abstract IoBuffer shrink();    /**     * @see java.nio.Buffer#position()     */    public abstract int position();    /**     * @see java.nio.Buffer#position(int)     */    public abstract IoBuffer position(int newPosition);    /**     * @see java.nio.Buffer#limit()     */    public abstract int limit();    /**     * @see java.nio.Buffer#limit(int)     */    public abstract IoBuffer limit(int newLimit);    /**     * @see java.nio.Buffer#mark()     */    public abstract IoBuffer mark();    /**     * Returns the position of the current mark.  This method returns <tt>-1</tt> if no     * mark is set.     */    public abstract int markValue();    /**     * @see java.nio.Buffer#reset()     */    public abstract IoBuffer reset();    /**     * @see java.nio.Buffer#clear()     */    public abstract IoBuffer clear();    /**     * Clears this buffer and fills its content with <tt>NUL</tt>.     * The position is set to zero, the limit is set to the capacity,     * and the mark is discarded.     */    public abstract IoBuffer sweep();    /**double     * Clears this buffer and fills its content with <tt>value</tt>.     * The position is set to zero, the limit is set to the capacity,     * and the mark is discarded.     */    public abstract IoBuffer sweep(byte value);

⌨️ 快捷键说明

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