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

com.abubusoft.kripton.common.DynamicByteBuffer Maven / Gradle / Ivy

There is a newer version: 8.2.0-rc.4
Show newest version
/*
 * Copyright 2013-2014 Richard M. Hightower
 * Licensed 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.
 *
 * __________                              _____          __   .__
 * \______   \ ____   ____   ____   /\    /     \ _____  |  | _|__| ____    ____
 *  |    |  _//  _ \ /  _ \ /    \  \/   /  \ /  \\__  \ |  |/ /  |/    \  / ___\
 *  |    |   (  <_> |  <_> )   |  \ /\  /    Y    \/ __ \|    <|  |   |  \/ /_/  >
 *  |______  /\____/ \____/|___|  / \/  \____|__  (____  /__|_ \__|___|  /\___  /
 *         \/                   \/              \/     \/     \/       \//_____/
 *      ____.                     ___________   _____    ______________.___.
 *     |    |____ ___  _______    \_   _____/  /  _  \  /   _____/\__  |   |
 *     |    \__  \\  \/ /\__  \    |    __)_  /  /_\  \ \_____  \  /   |   |
 * /\__|    |/ __ \\   /  / __ \_  |        \/    |    \/        \ \____   |
 * \________(____  /\_/  (____  / /_______  /\____|__  /_______  / / ______|
 *               \/           \/          \/         \/        \/  \/
 */

package com.abubusoft.kripton.common;

import java.io.ByteArrayInputStream;

public class DynamicByteBuffer {

    public static DynamicByteBuffer create(byte[] buffer ) {
        DynamicByteBuffer buf = new DynamicByteBuffer( buffer.length );
        buf.buffer = buffer;
        return buf;
    }
    
    public static DynamicByteBuffer create() {
        return new DynamicByteBuffer(2048);
    }


    public static DynamicByteBuffer create( int capacity ) {
        return new DynamicByteBuffer( capacity );
    }

    public static DynamicByteBuffer createExact( final int capacity ) {
        return new DynamicByteBuffer( capacity ) {
            public DynamicByteBuffer add( byte[] chars ) {
                DynamicByteBufferHelper._idx( buffer, length, chars );
                length += chars.length;
                return this;
            }
        };
    }


    protected byte[] buffer;

    protected int capacity = 16;

    protected int length = 0;

    protected DynamicByteBuffer() {
        init();
    }


    protected DynamicByteBuffer( int capacity ) {
        this.capacity = capacity;
        init();
    }

    public DynamicByteBuffer add( byte value ) {

        if ( 1 + length < capacity ) {
            DynamicByteBufferHelper.idx( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer );
            capacity = buffer.length;

            DynamicByteBufferHelper.idx( buffer, length, value );
        }

        length += 1;

        return this;

    }


    public DynamicByteBuffer add( byte[] array ) {
        if ( array.length + this.length < capacity ) {
            DynamicByteBufferHelper._idx( buffer, length, array );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + array.length );
            capacity = buffer.length;

            DynamicByteBufferHelper._idx( buffer, length, array );

        }
        length += array.length;
        return this;
    }


    public DynamicByteBuffer add( final byte[] array, final int length ) {
        if ( ( this.length + length ) < capacity ) {
            DynamicByteBufferHelper._idx( buffer, this.length, array, length );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + length );
            capacity = buffer.length;

            DynamicByteBufferHelper._idx( buffer, length, array, length );

        }
        this.length += length;
        return this;
    }


    public DynamicByteBuffer add( byte[] array, final int offset, final int length ) {
        if ( ( this.length + length ) < capacity ) {
            DynamicByteBufferHelper._idx( buffer, length, array, offset, length );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + length );
            capacity = buffer.length;

            DynamicByteBufferHelper._idx( buffer, length, array, offset, length );

        }
        this.length += length;
        return this;
    }


    public DynamicByteBuffer add( char value ) {

        if ( 2 + length < capacity ) {
            DynamicByteBufferHelper.charTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 2 );
            capacity = buffer.length;

            DynamicByteBufferHelper.charTo( buffer, length, value );
        }

        length += 2;
        return this;


    }


    public DynamicByteBuffer add( double value ) {

        if ( 8 + length < capacity ) {
            DynamicByteBufferHelper.doubleTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 8 );
            capacity = buffer.length;

            DynamicByteBufferHelper.doubleTo( buffer, length, value );
        }

        length += 8;
        return this;

    }

    public DynamicByteBuffer add( float value ) {

        if ( 4 + length < capacity ) {
            DynamicByteBufferHelper.floatTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 4 );
            capacity = buffer.length;

            DynamicByteBufferHelper.floatTo( buffer, length, value );
        }

        length += 4;
        return this;


    }

    public DynamicByteBuffer add( int value ) {

        if ( 4 + length < capacity ) {
            DynamicByteBufferHelper.intTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 4 );
            capacity = buffer.length;

            DynamicByteBufferHelper.intTo( buffer, length, value );
        }

        length += 4;
        return this;


    }

    public DynamicByteBuffer add( long value ) {

        if ( 8 + length < capacity ) {
            DynamicByteBufferHelper.longTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 8 );
            capacity = buffer.length;

            DynamicByteBufferHelper.longTo( buffer, length, value );
        }

        length += 8;
        return this;

    }

    public DynamicByteBuffer add( short value ) {

        if ( 2 + length < capacity ) {
            DynamicByteBufferHelper.shortTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 2 );
            capacity = buffer.length;

            DynamicByteBufferHelper.shortTo( buffer, length, value );
        }

        length += 2;
        return this;


    }

    public DynamicByteBuffer add( String str ) {
        this.add( DynamicByteBufferHelper.bytes( str ) );
        return this;

    }


    public DynamicByteBuffer addByte( int value ) {
        this.add( ( byte ) value );
        return this;
    }


    public void addUnsignedByte( short value ) {
        if ( 1 + length < capacity ) {
            DynamicByteBufferHelper.unsignedByteTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 1 );
            capacity = buffer.length;

            DynamicByteBufferHelper.unsignedByteTo( buffer, length, value );
        }

        length += 1;

    }

    public DynamicByteBuffer addUnsignedInt( long value ) {

        if ( 4 + length < capacity ) {
            DynamicByteBufferHelper.unsignedIntTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 4 );
            capacity = buffer.length;

            DynamicByteBufferHelper.unsignedIntTo( buffer, length, value );
        }

        length += 4;
        return this;

    }

    public void addUnsignedShort( int value ) {

        if ( 2 + length < capacity ) {
            DynamicByteBufferHelper.unsignedShortTo( buffer, length, value );
        } else {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + 2 );
            capacity = buffer.length;

            DynamicByteBufferHelper.unsignedShortTo( buffer, length, value );
        }

        length += 2;


    }

    private void doWriteDoubleArray( double[] values, int byteSize ) {
        if ( !( byteSize + length < capacity ) ) {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + byteSize );
        }
        for ( int index = 0; index < values.length; index++ ) {
            this.add( values[ index ] );
        }
    }

    private void doWriteFloatArray( float[] values, int byteSize ) {
        if ( !( byteSize + length < capacity ) ) {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + byteSize );
        }
        for ( int index = 0; index < values.length; index++ ) {
            this.add( values[ index ] );
        }
    }

    private void doWriteIntArray( int[] values, int byteSize ) {
        if ( !( byteSize + length < capacity ) ) {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + byteSize );
        }
        for ( int index = 0; index < values.length; index++ ) {
            this.add( values[ index ] );
        }
    }

    private void doWriteLongArray( long[] values, int byteSize ) {
        if ( !( byteSize + length < capacity ) ) {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + byteSize );
        }
        for ( int index = 0; index < values.length; index++ ) {
            this.add( values[ index ] );
        }
    }

    private void doWriteShortArray( short[] values, int byteSize ) {
        if ( !( byteSize + length < capacity ) ) {
            buffer = DynamicByteBufferHelper.grow( buffer, buffer.length * 2 + byteSize );
        }
        for ( int index = 0; index < values.length; index++ ) {
            this.add( values[ index ] );
        }
    }

    private void init() {
        buffer = new byte[ capacity ];
    }

    public ByteArrayInputStream input() {
    	return new ByteArrayInputStream(this.buffer);        
    }

    public int len() {
        return length;
    }

    public byte[] readAndReset() {
        byte[] bytes = this.buffer;
        this.buffer = null;
        return bytes;
    }

    public byte[] readForRecycle() {
        this.length = 0;
        return this.buffer;
    }

    public byte[] slc( int startIndex, int endIndex ) {
        return DynamicByteBufferHelper.slc( this.buffer, startIndex, endIndex );
    }

    public byte[] toBytes() {
        return DynamicByteBufferHelper.slc( this.buffer, 0, length );
    }

    public String toString() {
        int len = len();

        char[] chars = new char[ buffer.length ];
        for ( int index = 0; index < chars.length; index++ ) {
            chars[ index ] = ( char ) buffer[ index ];
        }
        return new String( chars, 0, len );
        //return new String ( this.buffer, 0, len, StandardCharsets.UTF_8 );
    }

    public void write( byte[] b ) {
        this.add( b );
    }

    public void write( byte[] b, int off, int len ) {
        this.add( b, len );
    }

    public void write( int b ) {
        this.addByte( b );
    }

    public void writeBoolean( boolean v ) {
        if ( v == true ) {
            this.addByte( 1 );
        } else {
            this.addByte( 0 );
        }
    }

    public void writeByte( byte v ) {
        this.addByte( v );
    }

    public void writeChar( char v ) {

        this.add( v );
    }

    public void writeDouble( double v ) {
        this.add( v );
    }

    public void writeFloat( float v ) {
        this.add( v );
    }

    public void writeInt( int v ) {
        this.add( v );
    }

    public void writeLargeByteArray( byte[] bytes ) {
        this.add( bytes.length );
        this.add( bytes );
    }

    public void writeLargeDoubleArray( double[] values ) {
        int byteSize = values.length * 8 + 4;
        this.add( values.length );
        doWriteDoubleArray( values, byteSize );


    }

    public void writeLargeFloatArray( float[] values ) {
        int byteSize = values.length * 4 + 4;
        this.add( values.length );
        doWriteFloatArray( values, byteSize );

    }

    public void writeLargeIntArray( int[] values ) {
        int byteSize = values.length * 4 + 4;
        this.add( values.length );
        doWriteIntArray( values, byteSize );
    }

    public void writeLargeLongArray( long[] values ) {
        int byteSize = values.length * 8 + 4;
        this.add( values.length );
        doWriteLongArray( values, byteSize );
    }


    public void writeLargeShortArray( short[] values ) {
        int byteSize = values.length * 2 + 4;
        this.add( values.length );
        doWriteShortArray( values, byteSize );
    }


    public void writeLargeString( String s ) {
        final byte[] bytes = DynamicByteBufferHelper.bytes( s );
        this.add( bytes.length );
        this.add( bytes );
    }

    public void writeLong( long v ) {
        this.add( v );
    }

    public void writeMediumByteArray( byte[] bytes ) {
        this.addUnsignedShort( bytes.length );
        this.add( bytes );
    }

    public void writeMediumDoubleArray( double[] values ) {
        int byteSize = values.length * 8 + 2;
        this.addUnsignedShort( values.length );
        doWriteDoubleArray( values, byteSize );

    }

    public void writeMediumFloatArray( float[] values ) {
        int byteSize = values.length * 4 + 2;
        this.addUnsignedShort( values.length );
        doWriteFloatArray( values, byteSize );

    }

    public void writeMediumIntArray( int[] values ) {
        int byteSize = values.length * 4 + 2;
        this.addUnsignedShort( values.length );
        doWriteIntArray( values, byteSize );
    }

    public void writeMediumLongArray( long[] values ) {
        int byteSize = values.length * 8 + 2;
        this.addUnsignedShort( values.length );
        doWriteLongArray( values, byteSize );
    }

    public void writeMediumShortArray( short[] values ) {
        int byteSize = values.length * 2 + 2;
        this.addUnsignedShort( values.length );
        doWriteShortArray( values, byteSize );
    }

    public void writeMediumString( String s ) {
        final byte[] bytes = DynamicByteBufferHelper.bytes( s );
        this.addUnsignedShort( bytes.length );
        this.add( bytes );
    }

    public void writeShort( short v ) {
        this.add( v );
    }

    public void writeSmallByteArray( byte[] bytes ) {
        this.addUnsignedByte( ( short ) bytes.length );
        this.add( bytes );
    }

    public void writeSmallDoubleArray( double[] values ) {
        int byteSize = values.length * 8 + 1;
        this.addUnsignedByte( ( short ) values.length );
        doWriteDoubleArray( values, byteSize );

    }

    public void writeSmallFloatArray( float[] values ) {
        int byteSize = values.length * 4 + 1;
        this.addUnsignedByte( ( short ) values.length );
        doWriteFloatArray( values, byteSize );
    }

    public void writeSmallIntArray( int[] values ) {
        int byteSize = values.length * 4 + 1;
        this.addUnsignedByte( ( short ) values.length );
        doWriteIntArray( values, byteSize );
    }

    public void writeSmallLongArray( long[] values ) {
        int byteSize = values.length * 8 + 1;
        this.addUnsignedByte( ( short ) values.length );
        doWriteLongArray( values, byteSize );
    }

    public void writeSmallShortArray( short[] values ) {
        int byteSize = values.length * 2 + 1;
        this.addUnsignedByte( ( short ) values.length );
        doWriteShortArray( values, byteSize );
    }

    public void writeSmallString( String s ) {
        final byte[] bytes = DynamicByteBufferHelper.bytes( s );
        this.addUnsignedByte( ( short ) bytes.length );
        this.add( bytes );
    }

    public void writeUnsignedByte( short v ) {
        this.addUnsignedByte( v );
    }

    public void writeUnsignedInt( long v ) {
        this.addUnsignedInt( v );
    }

    public void writeUnsignedShort( int v ) {
        this.addUnsignedShort( v );
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy