103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta/* 203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Copyright (C) 2013 The Android Open Source Project 303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * 403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License"); 503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * you may not use this file except in compliance with the License. 603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * You may obtain a copy of the License at 703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * 803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * http://www.apache.org/licenses/LICENSE-2.0 903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * 1003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software 1103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS, 1203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * See the License for the specific language governing permissions and 1403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * limitations under the License. 1503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta */ 1603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 1703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptapackage com.android.tools.layoutlib.java; 1803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 1903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptaimport java.nio.charset.Charset; 2003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 2103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta/** 2203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Defines the same class as the java.lang.UnsafeByteSequence which was added in 2303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Dalvik VM. This hack, provides a replacement for that class which can't be 2403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * loaded in the standard JVM since it's in the java package and standard JVM 2503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * doesn't have it. 2603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * <p/> 2703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Extracted from API level 18, file: 2803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * platform/libcore/luni/src/main/java/java/lang/UnsafeByteSequence.java 2903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta */ 3003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Guptapublic class UnsafeByteSequence { 3103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta private byte[] bytes; 3203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta private int count; 3303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 3403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public UnsafeByteSequence(int initialCapacity) { 3503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta this.bytes = new byte[initialCapacity]; 3603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 3703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 3803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public int size() { 3903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta return count; 4003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 4103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 4203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta /** 4303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * Moves the write pointer back to the beginning of the sequence, 4403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta * but without resizing or reallocating the buffer. 4503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta */ 4603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public void rewind() { 4703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta count = 0; 4803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 4903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 5003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public void write(byte[] buffer, int offset, int length) { 5103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta if (count + length >= bytes.length) { 5203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta byte[] newBytes = new byte[(count + length) * 2]; 5303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta System.arraycopy(bytes, 0, newBytes, 0, count); 5403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta bytes = newBytes; 5503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 5603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta System.arraycopy(buffer, offset, bytes, count, length); 5703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta count += length; 5803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 5903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 6003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public void write(int b) { 6103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta if (count == bytes.length) { 6203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta byte[] newBytes = new byte[count * 2]; 6303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta System.arraycopy(bytes, 0, newBytes, 0, count); 6403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta bytes = newBytes; 6503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 6603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta bytes[count++] = (byte) b; 6703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 6803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 6903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public byte[] toByteArray() { 7003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta if (count == bytes.length) { 7103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta return bytes; 7203a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 7303a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta byte[] result = new byte[count]; 7403a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta System.arraycopy(bytes, 0, result, 0, count); 7503a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta return result; 7603a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 7703a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta 7803a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta public String toString(Charset cs) { 7903a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta return new String(bytes, 0, count, cs); 8003a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta } 8103a057c1af9ca3f125c7924bf0b78da52223d8d3Deepanshu Gupta} 82