jdk/src/java.base/share/classes/sun/reflect/ByteVectorImpl.java
changeset 38062 430e0a96ef1f
parent 38061 5fe046aef3b9
parent 38060 954c9575f653
child 38064 0e7c67a6ad89
child 38065 025c784d9333
equal deleted inserted replaced
38061:5fe046aef3b9 38062:430e0a96ef1f
     1 /*
       
     2  * Copyright (c) 2001, 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.reflect;
       
    27 
       
    28 class ByteVectorImpl implements ByteVector {
       
    29     private byte[] data;
       
    30     private int pos;
       
    31 
       
    32     public ByteVectorImpl() {
       
    33         this(100);
       
    34     }
       
    35 
       
    36     public ByteVectorImpl(int sz) {
       
    37         data = new byte[sz];
       
    38         pos = -1;
       
    39     }
       
    40 
       
    41     public int getLength() {
       
    42         return pos + 1;
       
    43     }
       
    44 
       
    45     public byte get(int index) {
       
    46         if (index >= data.length) {
       
    47             resize(index);
       
    48             pos = index;
       
    49         }
       
    50         return data[index];
       
    51     }
       
    52 
       
    53     public void put(int index, byte value) {
       
    54         if (index >= data.length) {
       
    55             resize(index);
       
    56             pos = index;
       
    57         }
       
    58         data[index] = value;
       
    59     }
       
    60 
       
    61     public void add(byte value) {
       
    62         if (++pos >= data.length) {
       
    63             resize(pos);
       
    64         }
       
    65         data[pos] = value;
       
    66     }
       
    67 
       
    68     public void trim() {
       
    69         if (pos != data.length - 1) {
       
    70             byte[] newData = new byte[pos + 1];
       
    71             System.arraycopy(data, 0, newData, 0, pos + 1);
       
    72             data = newData;
       
    73         }
       
    74     }
       
    75 
       
    76     public byte[] getData() {
       
    77         return data;
       
    78     }
       
    79 
       
    80     private void resize(int minSize) {
       
    81         if (minSize <= 2 * data.length) {
       
    82             minSize = 2 * data.length;
       
    83         }
       
    84         byte[] newData = new byte[minSize];
       
    85         System.arraycopy(data, 0, newData, 0, data.length);
       
    86         data = newData;
       
    87     }
       
    88 }