Home | History | Annotate | Download | only in java
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tools.layoutlib.java;
     18 
     19 import java.nio.charset.Charset;
     20 
     21 /**
     22  * Defines the same class as the java.lang.UnsafeByteSequence which was added in
     23  * Dalvik VM. This hack, provides a replacement for that class which can't be
     24  * loaded in the standard JVM since it's in the java package and standard JVM
     25  * doesn't have it.
     26  * <p/>
     27  * Extracted from API level 18, file:
     28  * platform/libcore/luni/src/main/java/java/lang/UnsafeByteSequence.java
     29  */
     30 public class UnsafeByteSequence {
     31     private byte[] bytes;
     32     private int count;
     33 
     34     public UnsafeByteSequence(int initialCapacity) {
     35         this.bytes = new byte[initialCapacity];
     36     }
     37 
     38     public int size() {
     39         return count;
     40     }
     41 
     42     /**
     43      * Moves the write pointer back to the beginning of the sequence,
     44      * but without resizing or reallocating the buffer.
     45      */
     46     public void rewind() {
     47         count = 0;
     48     }
     49 
     50     public void write(byte[] buffer, int offset, int length) {
     51         if (count + length >= bytes.length) {
     52             byte[] newBytes = new byte[(count + length) * 2];
     53             System.arraycopy(bytes, 0, newBytes, 0, count);
     54             bytes = newBytes;
     55         }
     56         System.arraycopy(buffer, offset, bytes, count, length);
     57         count += length;
     58     }
     59 
     60     public void write(int b) {
     61         if (count == bytes.length) {
     62             byte[] newBytes = new byte[count * 2];
     63             System.arraycopy(bytes, 0, newBytes, 0, count);
     64             bytes = newBytes;
     65         }
     66         bytes[count++] = (byte) b;
     67     }
     68 
     69     public byte[] toByteArray() {
     70         if (count == bytes.length) {
     71             return bytes;
     72         }
     73         byte[] result = new byte[count];
     74         System.arraycopy(bytes, 0, result, 0, count);
     75         return result;
     76     }
     77 
     78     public String toString(Charset cs) {
     79         return new String(bytes, 0, count, cs);
     80     }
     81 }
     82