-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathTableBlock.java
More file actions
executable file
·899 lines (879 loc) · 31.6 KB
/
TableBlock.java
File metadata and controls
executable file
·899 lines (879 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.chunk;
import com.reandroid.arsc.ARSCLib;
import com.reandroid.arsc.ApkFile;
import com.reandroid.arsc.array.PackageArray;
import com.reandroid.arsc.header.HeaderBlock;
import com.reandroid.arsc.header.InfoHeader;
import com.reandroid.arsc.header.TableHeader;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.arsc.model.ResourceEntry;
import com.reandroid.arsc.model.ResourceName;
import com.reandroid.arsc.pool.TableStringPool;
import com.reandroid.arsc.value.Entry;
import com.reandroid.arsc.value.ResConfig;
import com.reandroid.arsc.value.StagedAliasEntry;
import com.reandroid.arsc.value.ValueItem;
import com.reandroid.common.BytesOutputStream;
import com.reandroid.common.ReferenceResolver;
import com.reandroid.json.JSONConvert;
import com.reandroid.json.JSONObject;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
public class TableBlock extends Chunk<TableHeader>
implements MainChunk, Iterable<PackageBlock>, JSONConvert<JSONObject> {
private final TableStringPool mTableStringPool;
private final PackageArray mPackageArray;
private final List<TableBlock> mFrameWorks;
private ApkFile mApkFile;
private ReferenceResolver referenceResolver;
private PackageBlock mCurrentPackage;
private PackageBlock mEmptyTablePackage;
public TableBlock() {
super(new TableHeader(), 2);
TableHeader header = getHeaderBlock();
this.mTableStringPool = new TableStringPool(true);
this.mPackageArray = new PackageArray(header.getPackageCount());
this.mFrameWorks = new ArrayCollection<>();
addChild(mTableStringPool);
addChild(mPackageArray);
}
// Experimental
public void changePackageId(int packageIdOld, int packageIdNew){
for (PackageBlock packageBlock : this) {
packageBlock.changePackageId(packageIdOld, packageIdNew);
}
}
// Experimental
public Iterator<ValueItem> allValues(){
return new MergingIterator<>(new ComputeIterator<>(getPackages(),
PackageBlock::allValues));
}
public PackageBlock getCurrentPackage(){
return mCurrentPackage;
}
public void setCurrentPackage(PackageBlock packageBlock){
mCurrentPackage = packageBlock;
}
public PackageBlock getPackageBlockByTag(Object tag){
for(PackageBlock packageBlock : this){
if(Objects.equals(tag, packageBlock.getTag())){
return packageBlock;
}
}
return null;
}
public Iterator<ResourceEntry> getResources(){
return new IterableIterator<PackageBlock, ResourceEntry>(getPackages()) {
@Override
public Iterator<ResourceEntry> iterator(PackageBlock element) {
return element.getResources();
}
};
}
public ResourceEntry getResource(int resourceId){
if(resourceId == 0){
return null;
}
Iterator<PackageBlock> iterator = getAllPackages();
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(resourceId);
if(resourceEntry != null){
return resourceEntry;
}
}
int staged = resolveStagedAlias(resourceId, 0);
if(staged == 0 || staged == resourceId){
return null;
}
iterator = getAllPackages();
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(staged);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getResource(PackageBlock context, int resourceId){
if(resourceId == 0){
return null;
}
Iterator<PackageBlock> iterator = getAllPackages(context);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(resourceId);
if(resourceEntry != null){
return resourceEntry;
}
}
int staged = resolveStagedAlias(resourceId, 0);
if(staged == 0 || staged == resourceId){
return null;
}
iterator = getAllPackages(context);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(staged);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getResource(ResourceName resourceName) {
if(resourceName != null) {
return getResource(resourceName.getPackageName(),
resourceName.getType(), resourceName.getName());
}
return null;
}
public ResourceEntry getResource(String packageName, String type, String name){
Iterator<PackageBlock> iterator = getAllPackages(packageName);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(type, name);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getResource(PackageBlock context, String type, String name){
Iterator<PackageBlock> iterator = getAllPackages(context);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(type, name);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getResource(PackageBlock context, String packageName, String type, String name){
Iterator<PackageBlock> iterator = getAllPackages(context, packageName);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(type, name);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getLocalResource(int resourceId){
return getLocalResource( null, resourceId);
}
public ResourceEntry getLocalResource(PackageBlock context, int resourceId){
Iterator<PackageBlock> iterator = getPackages(context);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(resourceId);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getLocalResource(PackageBlock context, String type, String name){
Iterator<PackageBlock> iterator = getPackages(context);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(type, name);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public ResourceEntry getLocalResource(String type, String name){
return getLocalResource((String) null, type, name);
}
public ResourceEntry getLocalResource(String packageName, String type, String name){
Iterator<PackageBlock> iterator = getPackages(packageName);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock.getResource(type, name);
if(resourceEntry != null){
return resourceEntry;
}
}
return null;
}
public Iterator<ResourceEntry> getLocalResources(String type){
return new IterableIterator<PackageBlock, ResourceEntry>(getPackages((String) null)) {
@Override
public Iterator<ResourceEntry> iterator(PackageBlock element) {
return element.getResources(type);
}
};
}
public ResourceEntry getAttrResource(String prefix, String name){
Iterator<PackageBlock> iterator = getAllPackages(prefix);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock
.getAttrResource(name);
if(resourceEntry != null){
return resourceEntry;
}
}
if(prefix != null){
return getAttrResource(null, name);
}
return null;
}
public ResourceEntry getAttrResource(PackageBlock context, String prefix, String name){
Iterator<PackageBlock> iterator = getAllPackages(context, prefix);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock
.getAttrResource(name);
if(resourceEntry != null){
return resourceEntry;
}
}
if(prefix != null){
return getAttrResource(null, name);
}
return null;
}
public ResourceEntry getIdResource(PackageBlock context, String prefix, String name){
Iterator<PackageBlock> iterator = getAllPackages(context, prefix);
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
ResourceEntry resourceEntry = packageBlock
.getIdResource(name);
if(resourceEntry != null){
return resourceEntry;
}
}
if(prefix != null){
return getAttrResource(null, name);
}
return null;
}
public int resolveResourceId(String packageName, String type, String name){
Iterator<Entry> iterator = getEntries(packageName, type, name);
if(iterator.hasNext()){
return iterator.next().getResourceId();
}
return 0;
}
public Entry getEntry(String packageName, String type, String name){
Iterator<PackageBlock> iterator = getAllPackages(packageName);
Entry result = null;
while (iterator.hasNext()){
Entry entry = iterator.next().getEntry(type, name);
if(entry == null){
continue;
}
if(!entry.isNull()){
return entry;
}
if(result == null){
result = entry;
}
}
return result;
}
public Iterator<Entry> getEntries(int resourceId){
return getEntries(resourceId, true);
}
public Iterator<Entry> getEntries(int resourceId, boolean skipNull){
final int packageId = resourceId >>> 24;
final int typeId = (resourceId >> 16) & 0xff;
final int entryId = resourceId & 0xffff;
return new IterableIterator<PackageBlock, Entry>(getAllPackages(packageId)) {
@Override
public Iterator<Entry> iterator(PackageBlock element) {
if(super.getCountValue() > 0){
super.stop();
return null;
}
return element.getEntries(typeId, entryId, skipNull);
}
};
}
public Iterator<Entry> getEntries(String packageName, String type, String name){
return new IterableIterator<PackageBlock, Entry>(getAllPackages(packageName)) {
@Override
public Iterator<Entry> iterator(PackageBlock element) {
if(super.getCountValue() > 0){
super.stop();
return null;
}
return element.getEntries(type, name);
}
};
}
public Iterator<PackageBlock> getPackages(String packageName){
return new FilterIterator<PackageBlock>(getPackages()) {
@Override
public boolean test(PackageBlock packageBlock){
if(packageName != null && packageName.length() > 0){
return packageBlock.packageNameMatches(packageName);
}
return TableBlock.this == packageBlock.getTableBlock();
}
};
}
public Iterator<PackageBlock> getPackages(int packageId){
if(packageId == 0){
return EmptyIterator.of();
}
return new FilterIterator<PackageBlock>(getPackages()) {
@Override
public boolean test(PackageBlock packageBlock){
return packageId == packageBlock.getId();
}
};
}
public Iterator<PackageBlock> getPackages(){
return getPackages((PackageBlock) null);
}
public Iterator<PackageBlock> getPackages(PackageBlock context){
PackageBlock current;
if(context == null){
current = getCurrentPackage();
}else {
current = context;
}
Iterator<PackageBlock> iterator = this.iterator();
if(current == null){
return iterator;
}
return new CombiningIterator<>(
SingleIterator.of(current),
new FilterIterator.Except<>(iterator, current)
);
}
public void removePackage(PackageBlock packageBlock){
getPackageArray().remove(packageBlock);
}
public Iterator<PackageBlock> getAllPackages(){
return getAllPackages((PackageBlock) null);
}
public Iterator<PackageBlock> getAllPackages(PackageBlock context, String packageName){
return new FilterIterator<PackageBlock>(getAllPackages(context)) {
@Override
public boolean test(PackageBlock packageBlock){
if(packageName != null){
return packageBlock.packageNameMatches(packageName);
}
return TableBlock.this == packageBlock.getTableBlock();
}
};
}
public Iterator<PackageBlock> getAllPackages(PackageBlock context){
return new CombiningIterator<>(getPackages(context),
new IterableIterator<TableBlock, PackageBlock>(frameworks()) {
@Override
public Iterator<PackageBlock> iterator(TableBlock element) {
return element.getPackages();
}
});
}
public Iterator<PackageBlock> getAllPackages(int packageId){
return new FilterIterator<PackageBlock>(getAllPackages()) {
@Override
public boolean test(PackageBlock packageBlock){
return packageId == packageBlock.getId();
}
};
}
public Iterator<PackageBlock> getAllPackages(String packageName){
return new FilterIterator<PackageBlock>(getAllPackages()) {
@Override
public boolean test(PackageBlock packageBlock){
if(packageName != null){
return packageBlock.packageNameMatches(packageName);
}
return TableBlock.this == packageBlock.getTableBlock();
}
};
}
public boolean removeUnusedSpecs(){
boolean result = false;
for(PackageBlock packageBlock : this){
if (packageBlock.removeUnusedSpecs()) {
result = true;
}
}
return result;
}
public String refreshFull(){
int sizeOld = getHeaderBlock().getChunkSize();
StringBuilder message = new StringBuilder();
boolean appendOnce = false;
if(getTableStringPool().removeUnusedStrings()){
message.append("Removed unused table strings");
appendOnce = true;
}
for(PackageBlock packageBlock : this){
String packageMessage = packageBlock.refreshFull(false);
if(packageMessage == null){
continue;
}
if(appendOnce){
message.append("\n");
}
message.append("Package: ");
message.append(packageBlock.getName());
message.append("\n ");
packageMessage = packageMessage.replaceAll("\n", "\n ");
message.append(packageMessage);
appendOnce = true;
}
refresh();
int sizeNew = getHeaderBlock().getChunkSize();
if(sizeOld != sizeNew){
if(appendOnce){
message.append("\n");
}
message.append("Table size changed = ");
message.append(sizeOld);
message.append(", ");
message.append(sizeNew);
appendOnce = true;
}
if(appendOnce){
return message.toString();
}
return null;
}
private void linkStringsInternal() {
linkTableStringsInternal(getTableStringPool());
for(PackageBlock packageBlock : this) {
packageBlock.linkSpecStringsInternal(packageBlock.getSpecStringPool());
}
}
public void linkTableStringsInternal(TableStringPool tableStringPool){
for(PackageBlock packageBlock : this){
packageBlock.linkTableStringsInternal(tableStringPool);
}
}
public List<Entry> resolveReference(int referenceId){
return resolveReference(referenceId, null);
}
public List<Entry> resolveReferenceWithConfig(int referenceId, ResConfig resConfig){
ReferenceResolver resolver = this.referenceResolver;
if(resolver == null){
resolver = new ReferenceResolver(this);
this.referenceResolver = resolver;
}
return resolver.resolveWithConfig(referenceId, resConfig);
}
public List<Entry> resolveReference(int referenceId, Predicate<Entry> filter){
ReferenceResolver resolver = this.referenceResolver;
if(resolver == null){
resolver = new ReferenceResolver(this);
this.referenceResolver = resolver;
}
return resolver.resolveAll(referenceId, filter);
}
public Iterator<PackageBlock> iterator(){
return getPackageArray().iterator();
}
public PackageBlock get(int index){
return getPackageArray().get(index);
}
public void clear(){
getPackageArray().destroy();
getStringPool().clear();
clearFrameworks();
refresh();
}
public int size(){
return getPackageArray().size();
}
public boolean isEmpty(){
if(size() == 0){
return true;
}
Iterator<PackageBlock> iterator = getPackages();
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
if(!packageBlock.isEmpty()){
return false;
}
}
return true;
}
public boolean initializeAsEmpty() {
if(isEmpty()) {
setNull(true);
setCurrentPackage(pickOrEmptyPackage());
return true;
}
return false;
}
public boolean isMultiPackage() {
return size() > 1;
}
public PackageBlock pickOne(){
PackageBlock current = getCurrentPackage();
if(current != null && current.getTableBlock() == this){
return current;
}
return getPackageArray().pickOne();
}
public PackageBlock pickOne(int packageId){
return getPackageArray().pickOne(packageId);
}
public PackageBlock pickOrEmptyPackage(){
PackageBlock packageBlock = this.pickOne();
if(packageBlock == null){
packageBlock = this.mEmptyTablePackage;
if(packageBlock == null){
packageBlock = PackageBlock.createEmptyPackage(this);
this.mEmptyTablePackage = packageBlock;
}
}
return packageBlock;
}
public void sortPackages(){
getPackageArray().sort();
}
public Iterable<PackageBlock> listPackages(){
return getPackageArray().listItems();
}
public Iterator<ResConfig> getResConfigs(){
return new MergingIterator<>(new ComputeIterator<>(iterator(),
PackageBlock::getResConfigs));
}
@Override
public TableStringPool getStringPool() {
return mTableStringPool;
}
@Override
public ApkFile getApkFile(){
return mApkFile;
}
@Override
public void setApkFile(ApkFile apkFile){
this.mApkFile = apkFile;
}
@Override
public TableBlock getTableBlock() {
return this;
}
public TableStringPool getTableStringPool(){
return mTableStringPool;
}
public PackageBlock getPackageBlockById(int pkgId){
return getPackageArray().getPackageBlockById(pkgId);
}
public PackageBlock newPackage(int id, String name){
PackageBlock packageBlock = getPackageArray().createNext();
packageBlock.setId(id);
if(name != null){
packageBlock.setName(name);
}
return packageBlock;
}
public PackageBlock getOrCreatePackage(int id, String name){
PackageBlock packageBlockId = getPackageArray()
.getPackageBlockById(id);
if(packageBlockId == null){
return newPackage(id, name);
}
if(name == null){
return packageBlockId;
}
PackageBlock packageBlockName = getPackageArray()
.getPackageBlockByName(name);
if(packageBlockId == packageBlockName){
return packageBlockId;
}
return newPackage(id, name);
}
public PackageArray getPackageArray(){
return mPackageArray;
}
public void trimConfigSizes(int resConfigSize){
for(PackageBlock packageBlock : this) {
packageBlock.trimConfigSizes(resConfigSize);
}
}
private void refreshPackageCount(){
int count = getPackageArray().size();
getHeaderBlock().getPackageCount().set(count);
}
@Override
protected void onChunkRefreshed() {
refreshPackageCount();
}
@Override
protected void onPreRefresh() {
getPackageArray().removeIf(PackageBlock::isEmpty);
super.onPreRefresh();
}
@Override
public void onReadBytes(BlockReader reader) throws IOException {
if(reader.available() == 0){
setNull(true);
return;
}
TableHeader tableHeader = getHeaderBlock();
tableHeader.readBytes(reader);
if(tableHeader.getChunkType() != ChunkType.TABLE){
throw new IOException("Not resource table: " + tableHeader);
}
boolean stringPoolLoaded = false;
InfoHeader infoHeader = InfoHeader.read(reader);
PackageArray packageArray = mPackageArray;
packageArray.clear();
while(infoHeader != null && reader.isAvailable()){
ChunkType chunkType=infoHeader.getChunkType();
if(chunkType==ChunkType.STRING){
if(!stringPoolLoaded){
mTableStringPool.readBytes(reader);
stringPoolLoaded=true;
}
}else if(chunkType==ChunkType.PACKAGE){
PackageBlock packageBlock=packageArray.createNext();
packageBlock.readBytes(reader);
}else {
UnknownChunk unknownChunk=new UnknownChunk();
unknownChunk.readBytes(reader);
addChild(unknownChunk);
}
infoHeader=reader.readHeaderBlock();
}
reader.close();
linkStringsInternal();
}
public void readBytes(File file) throws IOException{
BlockReader reader=new BlockReader(file);
super.readBytes(reader);
}
public void readBytes(InputStream inputStream) throws IOException{
BlockReader reader=new BlockReader(inputStream);
super.readBytes(reader);
}
public final int writeBytes(File file) throws IOException{
if(isNull()){
throw new IOException("Can NOT save null block");
}
File dir=file.getParentFile();
if(dir!=null && !dir.exists()){
dir.mkdirs();
}
OutputStream outputStream=new FileOutputStream(file);
int length = super.writeBytes(outputStream);
outputStream.close();
return length;
}
public int searchResourceIdAlias(int resourceId){
return resolveStagedAlias(resourceId, 0);
}
public int resolveStagedAlias(int stagedResId, int def){
StagedAliasEntry stagedAliasEntry = getStagedAlias(stagedResId);
if(stagedAliasEntry != null){
return stagedAliasEntry.getFinalizedResId();
}
return def;
}
public StagedAliasEntry getStagedAlias(int stagedResId){
Iterator<PackageBlock> iterator = getAllPackages();
while (iterator.hasNext()){
PackageBlock packageBlock = iterator.next();
StagedAliasEntry stagedAliasEntry =
packageBlock.searchByStagedResId(stagedResId);
if(stagedAliasEntry != null){
return stagedAliasEntry;
}
}
return null;
}
public List<TableBlock> getFrameWorks(){
return mFrameWorks;
}
public Iterator<TableBlock> frameworks(){
List<TableBlock> frameworkList = getFrameWorks();
if(frameworkList.size() == 0){
return EmptyIterator.of();
}
return frameworkList.iterator();
}
public boolean isAndroid(){
PackageBlock packageBlock = pickOne();
if(packageBlock == null){
return false;
}
return "android".equals(packageBlock.getName())
&& packageBlock.getId() == 0x01;
}
public boolean hasFramework(){
return getFrameWorks().size() != 0;
}
public void addFrameworks(Iterator<TableBlock> iterator) {
List<TableBlock> frameworkList = CollectionUtil.toList(iterator);
for(TableBlock framework : frameworkList) {
addFramework(framework);
}
}
public void addFramework(TableBlock frameworkTable){
if(frameworkTable != null && !containsFramework(frameworkTable)){
mFrameWorks.add(frameworkTable);
}
}
public boolean containsFramework(TableBlock tableBlock) {
if(tableBlock == null){
return false;
}
if(this.isSimilarTo(tableBlock)) {
return true;
}
for(TableBlock framework : mFrameWorks) {
if(framework.containsFramework(tableBlock)) {
return true;
}
}
return false;
}
public void removeFramework(TableBlock tableBlock){
mFrameWorks.remove(tableBlock);
}
public void clearFrameworks(){
mFrameWorks.clear();
}
public PackageBlock parsePublicXml(XmlPullParser parser) throws IOException,
XmlPullParserException {
PackageBlock packageBlock = newPackage(0, null);
packageBlock.parsePublicXml(parser);
return packageBlock;
}
@Override
public JSONObject toJson() {
JSONObject jsonObject=new JSONObject();
jsonObject.put(ARSCLib.NAME_arsc_lib_version, ARSCLib.getVersion());
jsonObject.put(NAME_packages, getPackageArray().toJson());
return jsonObject;
}
@Override
public void fromJson(JSONObject json) {
getPackageArray().fromJson(json.getJSONArray(NAME_packages));
refresh();
}
public void merge(TableBlock tableBlock){
if(tableBlock == null || tableBlock == this){
return;
}
getStringPool().merge(tableBlock.getStringPool());
getPackageArray().merge(tableBlock.getPackageArray());
refresh();
}
@Override
public byte[] getBytes(){
BytesOutputStream outputStream = new BytesOutputStream(
getHeaderBlock().getChunkSize());
try {
writeBytes(outputStream);
outputStream.close();
} catch (IOException ignored) {
}
return outputStream.toByteArray();
}
public boolean isSimilarTo(TableBlock tableBlock) {
if(tableBlock == this) {
return true;
}
if(tableBlock == null) {
return false;
}
int size = this.size();
if(size != tableBlock.size()) {
return false;
}
for(int i = 0; i < size; i++) {
if(!get(i).isSimilarTo(tableBlock.get(i))){
return false;
}
}
return true;
}
@Override
public String toString(){
StringBuilder builder=new StringBuilder();
builder.append(getClass().getSimpleName());
builder.append(": packages = ");
builder.append(mPackageArray.size());
builder.append(", size = ");
builder.append(getHeaderBlock().getChunkSize());
builder.append(" bytes");
return builder.toString();
}
public static TableBlock load(File file) throws IOException{
return load(new FileInputStream(file));
}
public static TableBlock load(InputStream inputStream) throws IOException{
TableBlock tableBlock=new TableBlock();
tableBlock.readBytes(inputStream);
return tableBlock;
}
public static TableBlock createEmpty() {
TableBlock tableBlock = new TableBlock();
tableBlock.initializeAsEmpty();
return tableBlock;
}
public static boolean isResTableBlock(InputStream inputStream){
try {
HeaderBlock headerBlock= BlockReader.readHeaderBlock(inputStream);
return isResTableBlock(headerBlock);
} catch (IOException ignored) {
return false;
}
}
public static boolean isResTableBlock(BlockReader blockReader){
if(blockReader==null){
return false;
}
try {
HeaderBlock headerBlock = blockReader.readHeaderBlock();
return isResTableBlock(headerBlock);
} catch (IOException ignored) {
return false;
}
}
public static boolean isResTableBlock(HeaderBlock headerBlock){
if(headerBlock==null){
return false;
}
ChunkType chunkType=headerBlock.getChunkType();
return chunkType==ChunkType.TABLE;
}
public static final String FILE_NAME = ObjectsUtil.of("resources.arsc");
public static final String FILE_NAME_JSON = ObjectsUtil.of("resources.arsc.json");
private static final String NAME_packages = ObjectsUtil.of("packages");
public static final String NAME_styled_strings = ObjectsUtil.of("styled_strings");
public static final String JSON_FILE_NAME = ObjectsUtil.of("resources.arsc.json");
public static final String DIRECTORY_NAME = ObjectsUtil.of("resources");
public static final String RES_JSON_DIRECTORY_NAME = ObjectsUtil.of("res-json");
public static final String RES_FILES_DIRECTORY_NAME = ObjectsUtil.of("res-files");
public static final String ATTR_null_table = ObjectsUtil.of("null-table");
}