IntegerItem.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.Block; 19 import com.reandroid.arsc.base.Creator; 20 import com.reandroid.arsc.base.DirectStreamReader; 21 import com.reandroid.utils.HexUtil; 22 23 public class IntegerItem extends BlockItem implements ReferenceItem, DirectStreamReader { 24 25 private final boolean bigEndian; 26 private int mCache; 27 28 public IntegerItem(boolean bigEndian){ 29 super(4); 30 this.bigEndian = bigEndian; 31 } 32 public IntegerItem(){ 33 this(false); 34 } 35 public IntegerItem(int value){ 36 this(false); 37 set(value); 38 } 39 40 @Override 41 public void set(int value) { 42 if(value == mCache){ 43 return; 44 } 45 46 mCache = value; 47 byte[] bytes = getBytesInternal(); 48 if (bigEndian) { 49 putBigEndianInteger(bytes, 0, value); 50 } else { 51 putInteger(bytes, 0, value); 52 } 53 } 54 @Override 55 public int get(){ 56 return mCache; 57 } 58 @Override 59 public <T1 extends Block> T1 getReferredParent(Class<T1> parentClass){ 60 return getParentInstance(parentClass); 61 } 62 public long unsignedLong(){ 63 return get() & 0x00000000ffffffffL; 64 } 65 public String toHex(){ 66 return HexUtil.toHex8(get()); 67 } 68 @Override 69 protected void onBytesChanged() { 70 int i; 71 byte[] bytes = getBytesInternal(); 72 if (bigEndian) { 73 i = getBigEndianInteger(bytes, 0); 74 } else { 75 i = getInteger(bytes, 0); 76 } 77 mCache = i; 78 } 79 @Override 80 public String toString(){ 81 return String.valueOf(get()); 82 } 83 84 public static final Creator<IntegerItem> CREATOR = new Creator<IntegerItem>() { 85 @Override 86 public IntegerItem[] newArrayInstance(int length) { 87 return new IntegerItem[length]; 88 } 89 @Override 90 public IntegerItem newInstance() { 91 return new IntegerItem(false); 92 } 93 }; 94 95 public static final Creator<IntegerItem> CREATOR_BIG_ENDIAN = new Creator<IntegerItem>() { 96 @Override 97 public IntegerItem[] newArrayInstance(int length) { 98 return new IntegerItem[length]; 99 } 100 @Override 101 public IntegerItem newInstance() { 102 return new IntegerItem(true); 103 } 104 }; 105 }