Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 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.location.cts.asn1.base;
     18 
     19 /**
     20  */
     21 public class PacketBuilder {
     22   private BitStream bitStream = new BitStream();
     23 
     24   public void append(BitStream appendix) {
     25     if (appendix.beginsByteAligned()) {
     26       bitStream.spoolToByteBoundary();
     27     }
     28     int bitsToAppend = appendix.getBitCount();
     29     byte[] bytes = appendix.getPaddedBytes();
     30     for (int i = 0; bitsToAppend >= 8; i++, bitsToAppend -= 8) {
     31       bitStream.appendByte(bytes[i]);
     32     }
     33     if (bitsToAppend != 0) {
     34       byte highBits = bytes[bytes.length - 1];
     35       byte lowBits = (byte) ((highBits & 0xFF)
     36                              >> (8 - bitsToAppend));
     37       bitStream.appendLowBits(bitsToAppend, lowBits);
     38     }
     39   }
     40 
     41   public byte[] getPaddedBytes() {
     42     return bitStream.getPaddedBytes();
     43   }
     44 
     45   public int getBitCount() {
     46     return bitStream.getBitCount();
     47   }
     48 
     49   public void appendAll(Iterable<BitStream> bitStreams) {
     50     for (BitStream bitStream : bitStreams) {
     51       append(bitStream);
     52     }
     53   }
     54 }
     55