160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta/* 260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Copyright (C) 2013 The Android Open Source Project 360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * 460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License"); 560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * you may not use this file except in compliance with the License. 660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * You may obtain a copy of the License at 760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * 860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * http://www.apache.org/licenses/LICENSE-2.0 960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * 1060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Unless required by applicable law or agreed to in writing, software 1160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS, 1260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * See the License for the specific language governing permissions and 1460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * limitations under the License. 1560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta */ 1660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 1760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Guptapackage com.android.tools.layoutlib.java; 1860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 1960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Guptaimport java.nio.charset.Charset; 2060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 2160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta/** 2260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Defines the same class as the java.lang.UnsafeByteSequence which was added in 2360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Dalvik VM. This hack, provides a replacement for that class which can't be 2460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * loaded in the standard JVM since it's in the java package and standard JVM 2560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * doesn't have it. 2660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * <p/> 2760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Extracted from API level 18, file: 2860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * platform/libcore/luni/src/main/java/java/lang/UnsafeByteSequence.java 2960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta */ 3060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Guptapublic class UnsafeByteSequence { 3160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta private byte[] bytes; 3260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta private int count; 3360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 3460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public UnsafeByteSequence(int initialCapacity) { 3560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta this.bytes = new byte[initialCapacity]; 3660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 3760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 3860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public int size() { 3960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta return count; 4060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 4160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 4260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta /** 4360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * Moves the write pointer back to the beginning of the sequence, 4460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta * but without resizing or reallocating the buffer. 4560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta */ 4660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public void rewind() { 4760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta count = 0; 4860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 4960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 5060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public void write(byte[] buffer, int offset, int length) { 5160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta if (count + length >= bytes.length) { 5260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta byte[] newBytes = new byte[(count + length) * 2]; 5360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta System.arraycopy(bytes, 0, newBytes, 0, count); 5460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta bytes = newBytes; 5560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 5660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta System.arraycopy(buffer, offset, bytes, count, length); 5760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta count += length; 5860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 5960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 6060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public void write(int b) { 6160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta if (count == bytes.length) { 6260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta byte[] newBytes = new byte[count * 2]; 6360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta System.arraycopy(bytes, 0, newBytes, 0, count); 6460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta bytes = newBytes; 6560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 6660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta bytes[count++] = (byte) b; 6760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 6860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 6960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public byte[] toByteArray() { 7060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta if (count == bytes.length) { 7160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta return bytes; 7260b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 7360b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta byte[] result = new byte[count]; 7460b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta System.arraycopy(bytes, 0, result, 0, count); 7560b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta return result; 7660b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 7760b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta 7860b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta public String toString(Charset cs) { 7960b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta return new String(bytes, 0, count, cs); 8060b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta } 8160b7c0219ab2618fae717fae75af5574cf11893fDeepanshu Gupta} 82