1 /* 2 * Copyright (C) 2021 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 package android.ddm; 17 18 import org.apache.harmony.dalvik.ddmc.ChunkHandler; 19 20 import java.nio.ByteBuffer; 21 22 /** 23 * Contains utility methods for chunk serialization and deserialization. 24 */ 25 public abstract class DdmHandle extends ChunkHandler { 26 27 /** 28 * Utility function to copy a String out of a ByteBuffer. 29 * 30 * This is here because multiple chunk handlers can make use of it, 31 * and there's nowhere better to put it. 32 */ getString(ByteBuffer buf, int len)33 public static String getString(ByteBuffer buf, int len) { 34 char[] data = new char[len]; 35 for (int i = 0; i < len; i++) { 36 data[i] = buf.getChar(); 37 } 38 return new String(data); 39 } 40 41 /** 42 * Utility function to copy a String into a ByteBuffer. 43 */ putString(ByteBuffer buf, String str)44 public static void putString(ByteBuffer buf, String str) { 45 int len = str.length(); 46 for (int i = 0; i < len; i++) { 47 buf.putChar(str.charAt(i)); 48 } 49 } 50 51 } 52