Home | History | Annotate | Download | only in os
      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 
     17 package android.os;
     18 
     19 import java.util.HashMap;
     20 
     21 /**
     22  * Helper class for writing pooled strings into a Parcel.  It must be used
     23  * in conjunction with {@link android.os.PooledStringReader}.  This really needs
     24  * to be pushed in to Parcel itself, but doing that is...  complicated.
     25  * @hide
     26  */
     27 public class PooledStringWriter {
     28     private final Parcel mOut;
     29 
     30     /**
     31      * Book-keeping for writing pooled string objects, mapping strings we have
     32      * written so far to their index in the pool.  We deliberately use HashMap
     33      * here since performance is critical, we expect to be doing lots of adds to
     34      * it, and it is only a temporary object so its overall memory footprint is
     35      * not a signifciant issue.
     36      */
     37     private final HashMap<String, Integer> mPool;
     38 
     39     /**
     40      * Book-keeping for writing pooling string objects, indicating where we
     41      * started writing the pool, which is where we need to ultimately store
     42      * how many strings are in the pool.
     43      */
     44     private int mStart;
     45 
     46     /**
     47      * Next available index in the pool.
     48      */
     49     private int mNext;
     50 
     51     public PooledStringWriter(Parcel out) {
     52         mOut = out;
     53         mPool = new HashMap<>();
     54         mStart = out.dataPosition();
     55         out.writeInt(0); // reserve space for final pool size.
     56     }
     57 
     58     public void writeString(String str) {
     59         final Integer cur = mPool.get(str);
     60         if (cur != null) {
     61             mOut.writeInt(cur);
     62         } else {
     63             mPool.put(str, mNext);
     64             mOut.writeInt(-(mNext+1));
     65             mOut.writeString(str);
     66             mNext++;
     67         }
     68     }
     69 
     70     public int getStringCount() {
     71         return mPool.size();
     72     }
     73 
     74     public void finish() {
     75         final int pos = mOut.dataPosition();
     76         mOut.setDataPosition(mStart);
     77         mOut.writeInt(mNext);
     78         mOut.setDataPosition(pos);
     79     }
     80 }
     81