/ app / src / main / java / com / reandroid / arsc / item / ByteItem.java
ByteItem.java
 1  /*
 2   *  Copyright (C) 2022 github.com/REAndroid
 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 com.reandroid.arsc.item;
17  
18  import com.reandroid.arsc.base.Creator;
19  import com.reandroid.arsc.base.DirectStreamReader;
20  import com.reandroid.utils.HexUtil;
21  
22  public class ByteItem extends BlockItem implements IntegerReference, DirectStreamReader {
23  
24      public ByteItem() {
25          super(1);
26      }
27  
28      public boolean getBit(int index){
29          return ((getByte()>>index) & 0x1) == 1;
30      }
31      public void putBit(int index, boolean bit){
32          int val= getByte();
33          int left=val>>index;
34          if(bit){
35              left=left|0x1;
36          }else {
37              left=left & 0xFE;
38          }
39          left=left<<index;
40          index=8-index;
41          int right=(0xFF>>index) & val;
42          val=left|right;
43          set((byte) val);
44      }
45      public void set(byte value) {
46          getBytesInternal()[0] = value;
47      }
48      public byte getByte() {
49          return getBytesInternal()[0];
50      }
51      @Override
52      public int get() {
53          return getByte() & 0xff;
54      }
55      @Override
56      public void set(int value) {
57          set((byte) value);
58      }
59      public String toHex(){
60          return HexUtil.toHex2(getByte());
61      }
62      @Override
63      public String toString(){
64          return String.valueOf(getByte());
65      }
66  
67      public static final Creator<ByteItem> CREATOR = new Creator<ByteItem>() {
68          @Override
69          public ByteItem[] newArrayInstance(int length) {
70              return new ByteItem[length];
71          }
72          @Override
73          public ByteItem newInstance() {
74              return new ByteItem();
75          }
76      };
77  }