1 /*
  2  * Copyright (c) 2017, 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 
 24 package jdk.experimental.bytecode;
 25 
 26 import java.util.function.Consumer;
 27 
 28 public class GrowableByteBuffer {
 29 
 30     public GrowableByteBuffer() {
 31     }
 32 
 33     byte[] elems = new byte[64];
 34     int offset = 0;
 35 
 36     public GrowableByteBuffer writeByte(int b) {
 37         return writeBytes(1, b);
 38     }
 39 
 40     public GrowableByteBuffer writeChar(int x) {
 41         return writeBytes(2, x);
 42     }
 43 
 44     public GrowableByteBuffer writeInt(int x) {
 45         return writeBytes(4, x);
 46     }
 47 
 48     public GrowableByteBuffer writeFloat(float x) {
 49         return writeInt(Float.floatToIntBits(x));
 50     }
 51 
 52     public GrowableByteBuffer writeLong(long x) {
 53         return writeBytes(8, x);
 54     }
 55 
 56     public GrowableByteBuffer writeDouble(double x) {
 57         writeLong(Double.doubleToLongBits(x));
 58         return this;
 59     }
 60 
 61     public GrowableByteBuffer writeBytes(byte[] barr) {
 62         expandIfNeeded(barr.length);
 63         System.arraycopy(barr, 0, elems, offset, barr.length);
 64         offset += barr.length;
 65         return this;
 66     }
 67 
 68     public GrowableByteBuffer writeBytes(GrowableByteBuffer bb) {
 69         expandIfNeeded(bb.offset);
 70         System.arraycopy(bb.elems, 0, elems, offset, bb.offset);
 71         offset += bb.offset;
 72         return this;
 73     }
 74 
 75     public GrowableByteBuffer withOffset(int offset, Consumer<GrowableByteBuffer> actions) {
 76         int prevOffset = this.offset;
 77         this.offset = offset;
 78         actions.accept(this);
 79         this.offset = prevOffset;
 80         return this;
 81     }
 82 
 83     private GrowableByteBuffer writeBytes(int size, long x) {
 84         expandIfNeeded(size);
 85         for (int i = 0; i < size; i++) {
 86             elems[offset++] = (byte) ((x >> 8 * (size - i - 1)) & 0xFF);
 87         }
 88         return this;
 89     }
 90 
 91     void expandIfNeeded(int increment) {
 92         if (offset + increment > elems.length) {
 93             int newsize = elems.length * 2;
 94             while (offset + increment > newsize) {
 95                 newsize *= 2;
 96             }
 97             byte[] newelems = new byte[newsize];
 98             System.arraycopy(elems, 0, newelems, 0, offset);
 99             elems = newelems;
100         }
101     }
102 
103     public byte[] bytes() {
104         byte[] bytes = new byte[offset];
105         System.arraycopy(elems, 0, bytes, 0, offset);
106         return bytes;
107     }
108 }