All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.mina.util.byteaccess.CompositeByteArrayRelativeBase Maven / Gradle / Ivy

There is a newer version: 3.0.0-M2
Show newest version
/*
 *  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.util.byteaccess;

import java.nio.ByteOrder;

import org.apache.mina.util.byteaccess.ByteArray.Cursor;
import org.apache.mina.util.byteaccess.CompositeByteArray.CursorListener;

/**
 * Provides common functionality between the
 * CompositeByteArrayRelativeReader and
 * CompositeByteArrayRelativeWriter.
 * 
 * @author Apache MINA Project
 */
abstract class CompositeByteArrayRelativeBase {

    /**
     * The underlying CompositeByteArray.
     */
    protected final CompositeByteArray cba;

    /**
     * A cursor of the underlying CompositeByteArray. This
     * cursor is never moved directly; its position only changes through calls
     * to relative read or write methods.
     */
    protected final Cursor cursor;

    /**
     * 
     * Creates a new instance of CompositeByteArrayRelativeBase.
     *
     * @param cba
     *  The {@link CompositeByteArray} that will be the base for this class
     */
    public CompositeByteArrayRelativeBase(CompositeByteArray cba) {
        this.cba = cba;
        cursor = cba.cursor(cba.first(), new CursorListener() {
            
            /**
             * {@inheritDoc}
             */
            @Override
            public void enteredFirstComponent(int componentIndex, ByteArray component) {
                // Do nothing.
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void enteredLastComponent(int componentIndex, ByteArray component) {
                assert false;
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void enteredNextComponent(int componentIndex, ByteArray component) {
                cursorPassedFirstComponent();
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void enteredPreviousComponent(int componentIndex, ByteArray component) {
                assert false;
            }

        });
    }

    /**
     * @return The number of remaining bytes
     */
    public final int getRemaining() {
        return cursor.getRemaining();
    }

    /**
     * @return TRUE if there are some more bytes
     */
    public final boolean hasRemaining() {
        return cursor.hasRemaining();
    }

    /**
     * @return The used byte order (little of big indian)
     */
    public ByteOrder order() {
        return cba.order();
    }

    /**
     * Make a ByteArray available for access at the end of this object.
     * 
     * @param ba The ByteArray to append
     */
    public final void append(ByteArray ba) {
        cba.addLast(ba);
    }

    /**
     * Free all resources associated with this object.
     */
    public final void free() {
        cba.free();
    }

    /**
     * @return the index that will be used for the next access.
     */
    public final int getIndex() {
        return cursor.getIndex();
    }

    /**
     * @return the index after the last byte that can be accessed.
     */
    public final int last() {
        return cba.last();
    }

    /**
     * Called whenever the cursor has passed from the cba's
     * first component. As the first component is no longer used, this provides
     * a good opportunity for subclasses to perform some action on it (such as
     * freeing it).
     */
    protected abstract void cursorPassedFirstComponent();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy