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 * http://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 package org.apache.directory.shared.ldap.util;
021
022
023 /**
024 * A dynamically growing byte[].
025 *
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 * @version $Rev$
028 */
029 public class ByteBuffer
030 {
031 /** the default initial buffer size */
032 private static final int DEFAULT_INITIAL_SIZE = 10;
033
034 /** the initial size of the buffer in number of bytes: also increment for allocations */
035 private final int initialSize;
036 /** the position into the buffer */
037 private int pos = 0;
038 /** the bytes of the buffer */
039 private byte[] buf;
040
041
042 public ByteBuffer()
043 {
044 this ( DEFAULT_INITIAL_SIZE );
045 }
046
047
048 public ByteBuffer( int initialSize )
049 {
050 if ( initialSize <= 0 )
051 {
052 throw new IllegalArgumentException( "initialSize must be greater than zero" );
053 }
054 this.initialSize = initialSize;
055 this.buf = new byte[initialSize];
056 }
057
058
059 public final void clear()
060 {
061 pos = 0;
062 }
063
064
065 public final int position()
066 {
067 return pos;
068 }
069
070
071 public final int capacity()
072 {
073 return buf.length;
074 }
075
076
077 public final byte get( int ii )
078 {
079 return buf[ii];
080 }
081
082
083 /**
084 * Get's the bytes, the backing store for this buffer. Note
085 * that you need to use the position index to determine where
086 * to stop reading from this buffer.
087 */
088 public final byte[] buffer()
089 {
090 return buf;
091 }
092
093
094 /**
095 * Get's a copy of the bytes used.
096 */
097 public final byte[] copyOfUsedBytes()
098 {
099 byte[] copy = new byte[pos];
100 System.arraycopy( buf, 0, copy, 0, pos );
101 return copy;
102 }
103
104
105 /**
106 * Appends the bytes to this buffer.
107 */
108 public final void append( byte[] bytes )
109 {
110 for ( byte b : bytes )
111 {
112 append( b );
113 }
114 }
115
116
117 /**
118 * Appends a byte to this buffer.
119 */
120 public final void append( byte bite )
121 {
122 if ( pos >= buf.length )
123 {
124 growBuffer();
125 }
126
127 buf[pos] = bite;
128 pos++;
129 }
130
131
132 /**
133 * Appends an int to this buffer. WARNING: the int is truncated to
134 * a byte value.
135 */
136 public final void append( int val )
137 {
138 if ( pos >= buf.length )
139 {
140 growBuffer();
141 }
142
143 buf[pos] = ( byte ) val;
144 pos++;
145 }
146
147
148 private final void growBuffer()
149 {
150 byte[] copy = new byte[buf.length+initialSize];
151 System.arraycopy( buf, 0, copy, 0, pos );
152 this.buf = copy;
153 }
154 }