001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.api.asn1.util;
022
023import java.nio.ByteBuffer;
024
025/**
026 * A buffer used to store an encoding PDU. It's auto-extended, and
027 * filled by the end.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class Asn1Buffer
032{
033    /** The buffer default size */
034    private static final int DEFAULT_SIZE = 1024;
035
036    /** The current position in the buffer */
037    private int pos = 0;
038
039    /** A buffer to store the encoded PDU */
040    private byte[] buffer;
041    
042    
043    /**
044     * Creates a new Asn1Buffer instance
045     */
046    public Asn1Buffer()
047    {
048        buffer = new byte[DEFAULT_SIZE];
049    }
050
051
052    /**
053     * @return The current position in the buffer
054     */
055    public int getPos()
056    {
057        return pos;
058    }
059
060
061    /**
062     * Set the current position in the buffer
063     * 
064     * @param pos The position to move the buffer to
065     */
066    public void setPos( int pos )
067    {
068        this.pos = pos;
069    }
070
071
072    /**
073     * Store a byte at the current position in the buffer
074     *
075     * @param b The byte to store
076     */
077    public void put( byte b )
078    {
079        if ( pos == buffer.length )
080        {
081            // The buffer needs to be reallocated, its too small
082            extend( 1 );
083        }
084
085        pos++;
086        buffer[buffer.length - pos] = b;
087    }
088
089
090    /**
091     * Store some bytes at the current position in the buffer
092     *
093     * @param bytes The bytes to store
094     */
095    public void put( byte[] bytes )
096    {
097        if ( pos + bytes.length > buffer.length )
098        {
099            // The buffer needs to be reallocated, its too small
100            extend( bytes.length );
101        }
102
103
104        pos += bytes.length;
105        System.arraycopy( bytes, 0, buffer, buffer.length - pos, bytes.length );
106    }
107
108
109    /**
110     * Extend the buffer
111     * 
112     * @param size The new buffer size
113     */
114    private void extend( int size )
115    {
116        // The buffer needs to be reallocated, it's too small
117        int newSize = ( ( size + buffer.length ) / DEFAULT_SIZE ) * DEFAULT_SIZE;
118
119        if ( size % DEFAULT_SIZE != 0 )
120        {
121            newSize += DEFAULT_SIZE;
122        }
123
124        byte[] newBuffer = new byte[newSize];
125        System.arraycopy( buffer, 0, newBuffer, newSize - buffer.length, buffer.length );
126
127        buffer = newBuffer;
128    }
129
130
131    /**
132     * @return The stored encoded PDU.
133     */
134    public ByteBuffer getBytes()
135    {
136        ByteBuffer result = ByteBuffer.allocate( pos );
137
138        result.put( buffer, buffer.length - pos, pos );
139        result.flip();
140
141        return result;
142    }
143
144
145    /**
146     * @return The buffer size (ie the maximum number of bytes that can be
147     * added to this bffder before it gets extended).
148     */
149    public int getSize()
150    {
151        return buffer.length;
152    }
153
154
155    /**
156     * Clear the position, emptying the buffer. If it has grown, reallocate it
157     * to its initial size.
158     */
159    public void clear()
160    {
161        if ( buffer.length > DEFAULT_SIZE )
162        {
163            buffer = new byte[DEFAULT_SIZE];
164        }
165
166        pos = 0;
167    }
168
169
170    /**
171     * {@inheritDoc}
172     */
173    @Override
174    public String toString()
175    {
176        return "[" + buffer.length + ", " + pos + "] '"
177            + Asn1StringUtils.dumpBytes( buffer, buffer.length - pos, pos ) + '\'';
178    }
179}