ShortArrayBlock.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.common.IntegerArray; 19 20 public class ShortArrayBlock extends BlockItem implements IntegerArray { 21 public ShortArrayBlock() { 22 super(0); 23 } 24 public boolean contains(int value){ 25 int size = size(); 26 for(int i = 0; i < size; i++){ 27 if(value == get(i)){ 28 return true; 29 } 30 } 31 return false; 32 } 33 public void clear(){ 34 setSize(0); 35 } 36 public void add(int value){ 37 int current = size(); 38 setSize(current + 1); 39 put(current, value); 40 } 41 public void add(IntegerArray integerArray){ 42 if(IntegerArray.isEmpty(integerArray)){ 43 return; 44 } 45 int old = size(); 46 int size = old + integerArray.size(); 47 setSize(size); 48 for(int i = 0; i < size; i++){ 49 put(old + i, integerArray.get(i)); 50 } 51 } 52 public void add(int[] values){ 53 if(IntegerArray.isEmpty(values)){ 54 return; 55 } 56 int old = size(); 57 int size = old + values.length; 58 setSize(size); 59 for(int i = 0; i < size; i++){ 60 put(old + i, values[i]); 61 } 62 } 63 public void set(int[] values){ 64 if(IntegerArray.isEmpty(values)){ 65 setSize(0); 66 return; 67 } 68 int size = values.length; 69 setSize(size); 70 for(int i =0; i < size; i++){ 71 put(i, values[i]); 72 } 73 } 74 public int[] toArray(){ 75 return IntegerArray.toArray(this); 76 } 77 public void fill(int value){ 78 int size = size(); 79 for(int i = 0; i < size; i++){ 80 put(i, value); 81 } 82 } 83 public void ensureArraySize(int size){ 84 int current = size(); 85 if(current >= size){ 86 return; 87 } 88 setSize(size); 89 } 90 public void setSize(int size){ 91 if(size < 0){ 92 size = 0; 93 } 94 setBytesLength(size * 2); 95 } 96 @Override 97 public int get(int index){ 98 if(index < 0 || index >= size()){ 99 return 0; 100 } 101 return getShortUnsigned(getBytesInternal(), index * 2); 102 } 103 @Override 104 public int size(){ 105 return getBytesLength() / 2; 106 } 107 public void put(int index, int value){ 108 putShort(getBytesInternal(), index * 2, value); 109 } 110 @Override 111 public String toString(){ 112 return IntegerArray.toString(this); 113 } 114 }