PooledStringReader.java revision a83ce1dd2ad3a6b71e90ff4845afc1299fe17b9d
1/* 2 * Copyright (C) 2015 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 17package android.os; 18 19/** 20 * Helper class for reading pooling strings from a Parcel. It must be used 21 * in conjunction with {@link android.os.PooledStringWriter}. This really needs 22 * to be pushed in to Parcel itself, but doing that is... complicated. 23 * @hide 24 */ 25public class PooledStringReader { 26 private final Parcel mIn; 27 28 /** 29 * The pool of strings we have collected so far. 30 */ 31 private final String[] mPool; 32 33 public PooledStringReader(Parcel in) { 34 mIn = in; 35 final int size = in.readInt(); 36 mPool = new String[size]; 37 } 38 39 public String readString() { 40 int idx = mIn.readInt(); 41 if (idx >= 0) { 42 return mPool[idx]; 43 } else { 44 idx = (-idx) - 1; 45 String str = mIn.readString(); 46 mPool[idx] = str; 47 return str; 48 } 49 } 50} 51