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

📄 compositebytearray.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     */    public byte get( int index )    {        return cursor( index ).get();    }    /**     * @inheritDoc     */    public void put( int index, byte b )    {        cursor( index ).put( b );    }    /**     * @inheritDoc     */    public void get( int index, IoBuffer bb )    {        cursor( index ).get( bb );    }    /**     * @inheritDoc     */    public void put( int index, IoBuffer bb )    {        cursor( index ).put( bb );    }    /**     * @inheritDoc     */    public int first()    {        return bas.firstByte();    }    /**     * @inheritDoc     */    public int last()    {        return bas.lastByte();    }    /**     * This method should be called prior to adding any component     * <code>ByteArray</code> to a composite.     *     * @param ba     *  The component to add.     */    private void addHook( ByteArray ba )    {        // Check first() is zero, otherwise cursor might not work.        // TODO: Remove this restriction?        if ( ba.first() != 0 )        {            throw new IllegalArgumentException( "Cannot add byte array that doesn't start from 0: " + ba.first() );        }        // Check order.        if ( order == null )        {            order = ba.order();        }        else if ( !order.equals( ba.order() ) )        {            throw new IllegalArgumentException( "Cannot add byte array with different byte order: " + ba.order() );        }    }    /**     * @inheritDoc     */    public ByteOrder order()    {        if ( order == null )        {            throw new IllegalStateException( "Byte order not yet set." );        }        return order;    }    /**     * @inheritDoc     */    public void order( ByteOrder order )    {        if ( order == null || !order.equals( this.order ) )        {            this.order = order;            if ( !bas.isEmpty() )            {                for ( Node node = bas.getFirst(); node.hasNextNode(); node = node.getNextNode() )                {                    node.getByteArray().order( order );                }            }        }    }    /**     * @inheritDoc     */    public short getShort( int index )    {        return cursor( index ).getShort();    }    /**     * @inheritDoc     */    public void putShort( int index, short s )    {        cursor( index ).putShort( s );    }    /**     * @inheritDoc     */    public int getInt( int index )    {        return cursor( index ).getInt();    }    /**     * @inheritDoc     */    public void putInt( int index, int i )    {        cursor( index ).putInt( i );    }    /**     * @inheritDoc     */    public long getLong( int index )    {        return cursor( index ).getLong();    }    /**     * @inheritDoc     */    public void putLong( int index, long l )    {        cursor( index ).putLong( l );    }    /**     * @inheritDoc     */    public float getFloat( int index )    {        return cursor( index ).getFloat();    }    /**     * @inheritDoc     */    public void putFloat( int index, float f )    {        cursor( index ).putFloat( f );    }    /**     * @inheritDoc     */    public double getDouble( int index )    {        return cursor( index ).getDouble();    }    /**     * @inheritDoc     */    public void putDouble( int index, double d )    {        cursor( index ).putDouble( d );    }    /**     * @inheritDoc     */    public char getChar( int index )    {        return cursor( index ).getChar();    }    /**     * @inheritDoc     */    public void putChar( int index, char c )    {        cursor( index ).putChar( c );    }    private class CursorImpl implements Cursor    {        private int index;        private final CursorListener listener;        private Node componentNode;        // Index of start of current component.        private int componentIndex;        // Cursor within current component.        private ByteArray.Cursor componentCursor;        public CursorImpl()        {            this( 0, null );        }        public CursorImpl( int index )        {            this( index, null );        }        public CursorImpl( CursorListener listener )        {            this( 0, listener );        }        public CursorImpl( int index, CursorListener listener )        {            this.index = index;            this.listener = listener;        }        /**         * @inheritDoc         */        public int getIndex()        {            return index;        }        /**         * @inheritDoc         */        public void setIndex( int index )        {            checkBounds( index, 0 );            this.index = index;        }        /**         * @inheritDoc         */        public void skip( int length )        {            setIndex( index + length );        }        /**         * @inheritDoc         */        public ByteArray slice( int length )        {            CompositeByteArray slice = new CompositeByteArray( byteArrayFactory );            int remaining = length;            while ( remaining > 0 )            {                prepareForAccess( remaining );                int componentSliceSize = Math.min( remaining, componentCursor.getRemaining() );                ByteArray componentSlice = componentCursor.slice( componentSliceSize );                slice.addLast( componentSlice );                index += componentSliceSize;                remaining -= componentSliceSize;            }            return slice;        }        /**         * @inheritDoc         */        public ByteOrder order()        {            return CompositeByteArray.this.order();        }        private void prepareForAccess( int accessSize )        {            // Handle removed node. Do this first so we can remove the reference            // even if bounds checking fails.            if ( componentNode != null && componentNode.isRemoved() )            {                componentNode = null;                componentCursor = null;            }            // Bounds checks            checkBounds( index, accessSize );            // Remember the current node so we can later tell whether or not we            // need to create a new cursor.            Node oldComponentNode = componentNode;            // Handle missing node.            if ( componentNode == null )            {                int basMidpoint = ( last() - first() ) / 2 + first();                if ( index <= basMidpoint )                {                    // Search from the start.                    componentNode = bas.getFirst();                    componentIndex = first();                    if ( listener != null )                    {                        listener.enteredFirstComponent( componentIndex, componentNode.getByteArray() );                    }                }                else                {                    // Search from the end.                    componentNode = bas.getLast();                    componentIndex = last() - componentNode.getByteArray().last();                    if ( listener != null )                    {                        listener.enteredLastComponent( componentIndex, componentNode.getByteArray() );                    }                }            }            // Go back, if necessary.            while ( index < componentIndex )            {                componentNode = componentNode.getPreviousNode();                componentIndex -= componentNode.getByteArray().last();                if ( listener != null )                {                    listener.enteredPreviousComponent( componentIndex, componentNode.getByteArray() );                }            }            // Go forward, if necessary.            while ( index >= componentIndex + componentNode.getByteArray().length() )            {                componentIndex += componentNode.getByteArray().last();                componentNode = componentNode.getNextNode();                if ( listener != null )                {                    listener.enteredNextComponent( componentIndex, componentNode.getByteArray() );                }            }            // Update the cursor.            int internalComponentIndex = index - componentIndex;            if ( componentNode == oldComponentNode )            {                // Move existing cursor.                componentCursor.setIndex( internalComponentIndex );            }            else            {                // Create new cursor.                componentCursor = componentNode.getByteArray().cursor( internalComponentIndex );            }        }        /**         * @inheritDoc         */        public int getRemaining()        {            return last() - index + 1;        }        /**         * @inheritDoc         */        public boolean hasRemaining()        {            return getRemaining() > 0;        }        /**         * @inheritDoc         */        public byte get()

⌨️ 快捷键说明

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