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 import com.google.common.collect.ImmutableList;
     20 
     21 import java.nio.ByteBuffer;
     22 import java.util.Collection;
     23 
     24 /**
     25  * Implements ASN.1 functionality.
     26  *
     27  */
     28 public class Asn1Boolean extends Asn1Object {
     29   private static final Collection<Asn1Tag> possibleFirstTags =
     30       ImmutableList.of(Asn1Tag.BOOLEAN);
     31 
     32   private boolean value;
     33 
     34   public static Collection<Asn1Tag> getPossibleFirstTags() {
     35     return possibleFirstTags;
     36   }
     37 
     38   @Override Asn1Tag getDefaultTag() {
     39     return Asn1Tag.BOOLEAN;
     40   }
     41 
     42   @Override int getBerValueLength() {
     43     return 1;
     44   }
     45 
     46   @Override void encodeBerValue(ByteBuffer buf) {
     47     buf.put(value ? (byte) 0xFF : (byte) 0x00);
     48   }
     49 
     50   @Override void decodeBerValue(ByteBuffer buf) {
     51     if (buf.remaining() != 1) {
     52       throw new IllegalArgumentException("Invalid remaining bytes: " + buf.remaining());
     53     }
     54     value = (buf.get() != 0);
     55   }
     56 
     57   public boolean getValue() {
     58     return value;
     59   }
     60 
     61   public void setValue(boolean value) {
     62     this.value = value;
     63   }
     64 
     65   private Iterable<BitStream> encodePerImpl() {
     66     BitStream result = new BitStream();
     67     result.appendBit(value);
     68     return ImmutableList.of(result);
     69   }
     70 
     71   @Override public Iterable<BitStream> encodePerUnaligned() {
     72     return encodePerImpl();
     73   }
     74 
     75   @Override public Iterable<BitStream> encodePerAligned() {
     76     return encodePerImpl();
     77   }
     78 
     79   @Override public void decodePerUnaligned(BitStreamReader reader) {
     80     value = reader.readBit();
     81   }
     82 
     83   @Override public void decodePerAligned(BitStreamReader reader) {
     84     value = reader.readBit();
     85   }
     86 }
     87