001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2016, 2017  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.httpcodec.util;
020
021/**
022 * A dynamically growing byte array.
023 */
024public class DynamicByteArray {
025
026        private byte[] bytes;
027        private int position;
028
029        /**
030         * Creates the array with the given initial size.
031         * 
032         * @param initialSize the initial size
033         */
034        public DynamicByteArray(int initialSize) {
035                if (initialSize < 128) {
036                        initialSize = 128;
037                }
038                bytes = new byte[initialSize];
039                position = 0;
040        }
041
042        /**
043         * Appends the given byte, growing the array if necessary.
044         * 
045         * @param data the byte to append
046         */
047        public void append(byte data) {
048                if (position >= bytes.length) {
049                        byte[] newBytes = new byte[(int)(bytes.length * 1.3)];
050                        System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
051                        bytes = newBytes;
052                }
053                bytes[position++] = data;
054        }
055
056        /**
057         * Appends the given bytes, growing the array if necessary.
058         * 
059         * @param data an array of bytes
060         * @param offset the first byte to append
061         * @param length the number of bytes to append
062         */
063        public void append(byte[] data, int offset, int length) {
064                if (bytes.length - position < length) {
065                        byte[] newBytes = new byte[(int)((bytes.length + length) * 1.3)];
066                        System.arraycopy(bytes, 0, newBytes, 0, bytes.length);
067                        bytes = newBytes;
068                }
069                System.arraycopy(data, offset, bytes, position, length);
070        }
071
072        /**
073         * Returns the current position (number of bytes in the array).
074         * 
075         * @return the position
076         */
077        public int position() {
078                return position;
079        }
080        
081        /**
082         * Resets the position to 0. 
083         */
084        public void clear() {
085                position = 0;
086        }
087        
088        /**
089         * Returns the internal storage for the bytes. 
090         * 
091         * @return the storage
092         */
093        public byte[] array() {
094                return bytes;
095        }
096}