1 /*
  2  * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.asmtools.jcoder;
 24 
 25 /**
 26  * Compiles just 1 source file
 27  */
 28 class ByteBuffer extends java.io.OutputStream {
 29 
 30     String myname;
 31     /**
 32      * The buffer where elements are stored.
 33      */
 34     byte data[];
 35     /**
 36      * The number of elements in the buffer.
 37      */
 38     int length;
 39     /**
 40      * The size of the increment. If it is 0 the size of the the buffer is doubled
 41      * everytime it needs to grow.
 42      */
 43     protected int capacityIncrement;
 44 
 45     /**
 46      * Constructs an empty vector with the specified storage capacity and the specified
 47      * capacityIncrement.
 48      *
 49      * @param initialCapacity the initial storage capacity of the vector
 50      * @param capacityIncrement how much to increase the element's size by.
 51      */
 52     public ByteBuffer(int initialCapacity, int capacityIncrement) {
 53 //      super();
 54         this.data = new byte[initialCapacity];
 55         this.capacityIncrement = capacityIncrement;
 56     }
 57 
 58     /**
 59      * Constructs an empty vector with the specified storage capacity.
 60      *
 61      * @param initialCapacity the initial storage capacity of the vector
 62      */
 63     public ByteBuffer(int initialCapacity) {
 64         this(initialCapacity, 0);
 65     }
 66 
 67     /**
 68      * Constructs an empty vector.
 69      */
 70     public ByteBuffer() {
 71         this(30);
 72     }
 73 
 74     /**
 75      * Constructs a full vector.
 76      */
 77     public ByteBuffer(byte data[], int capacityIncrement) {
 78         this.length = data.length;
 79         this.data = data;
 80         this.capacityIncrement = capacityIncrement;
 81     }
 82 
 83     /**
 84      * Constructs a full vector.
 85      */
 86     public ByteBuffer(byte data[]) {
 87         this(data, 0);
 88     }
 89 
 90     /**
 91      * Returns the number of elements in the vector. Note that this is not the same as the
 92      * vector's capacity.
 93      */
 94     public final int size() {
 95         return length;
 96     }
 97 
 98     /**
 99      * Ensures that the vector has at least the specified capacity.
100      *
101      * @param minCapacity the desired minimum capacity
102      */
103     public final synchronized void ensureCapacity(int minCapacity) {
104         int oldCapacity = data.length;
105         if (minCapacity <= oldCapacity) {
106             return;
107         }
108         byte oldData[] = data;
109         int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement) : (oldCapacity * 2);
110         if (newCapacity < minCapacity) {
111             newCapacity = minCapacity;
112         }
113         data = new byte[newCapacity];
114         System.arraycopy(oldData, 0, data, 0, length);
115     }
116 
117     /*======================================*/
118     public void write(int val) {
119         ensureCapacity(length + 1);
120         data[length++] = (byte) val;
121     }
122 
123     public void writeAt(int index, long val, int width) {
124         for (int i = 0; i < width; i++) {
125             data[index + i] = (byte) (val >> (width - 1 - i) * 8);
126         }
127     }
128 
129     public void append(long val, int width) {
130         ensureCapacity(length + width);
131         writeAt(length, val, width);
132         length += width;
133     }
134 
135     /*======================================================*/
136 } // end ByteBuffer
137