1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.sevenz;
20
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.LinkedList;
24
25 import org.apache.commons.lang3.ArrayUtils;
26
27
28
29
30 final class Folder {
31
32 static final Folder[] EMPTY_FOLDER_ARRAY = {};
33
34
35 Coder[] coders;
36
37
38
39
40 long totalInputStreams;
41
42
43 long totalOutputStreams;
44
45
46 BindPair[] bindPairs;
47
48
49 long[] packedStreams;
50
51
52 long[] unpackSizes;
53
54
55 boolean hasCrc;
56
57
58 long crc;
59
60
61
62
63 int numUnpackSubStreams;
64
65 int findBindPairForInStream(final int index) {
66 if (bindPairs != null) {
67 for (int i = 0; i < bindPairs.length; i++) {
68 if (bindPairs[i].inIndex == index) {
69 return i;
70 }
71 }
72 }
73 return -1;
74 }
75
76 int findBindPairForOutStream(final int index) {
77 if (bindPairs != null) {
78 for (int i = 0; i < bindPairs.length; i++) {
79 if (bindPairs[i].outIndex == index) {
80 return i;
81 }
82 }
83 }
84 return -1;
85 }
86
87
88
89
90
91
92
93
94 Iterable<Coder> getOrderedCoders() throws IOException {
95 if (ArrayUtils.isEmpty(packedStreams) || ArrayUtils.isEmpty(coders)) {
96 return Collections.emptyList();
97 }
98 final LinkedList<Coder> list = new LinkedList<>();
99 int current = (int) packedStreams[0];
100 while (current >= 0 && current < coders.length) {
101 if (list.contains(coders[current])) {
102 throw new IOException("folder uses the same coder more than once in coder chain");
103 }
104 list.addLast(coders[current]);
105 final int pair = findBindPairForOutStream(current);
106 current = pair != -1 ? (int) bindPairs[pair].inIndex : -1;
107 }
108 return list;
109 }
110
111 long getUnpackSize() {
112 if (totalOutputStreams == 0) {
113 return 0;
114 }
115 for (int i = (int) totalOutputStreams - 1; i >= 0; i--) {
116 if (findBindPairForOutStream(i) < 0) {
117 return unpackSizes[i];
118 }
119 }
120 return 0;
121 }
122
123 long getUnpackSizeForCoder(final Coder coder) {
124 if (coders != null) {
125 for (int i = 0; i < coders.length; i++) {
126 if (coders[i] == coder) {
127 return unpackSizes[i];
128 }
129 }
130 }
131 return 0;
132 }
133
134 @Override
135 public String toString() {
136 return "Folder with " + coders.length + " coders, " + totalInputStreams + " input streams, " + totalOutputStreams + " output streams, "
137 + bindPairs.length + " bind pairs, " + packedStreams.length + " packed streams, " + unpackSizes.length + " unpack sizes, "
138 + (hasCrc ? "with CRC " + crc : "without CRC") + " and " + numUnpackSubStreams + " unpack streams";
139 }
140 }