@@ -33,118 +33,136 @@ public class MergeTask extends Task<Boolean> {
33
33
private final String saveToPath ;
34
34
private final String filePath ;
35
35
36
+ private File splitFile ;
37
+
38
+ private File [] chunkFiles ;
39
+ private long chunksTotalSize ;
40
+ private File resultFile ;
41
+
36
42
public MergeTask (String filePath , String saveToPath ) {
37
43
this .filePath = filePath ;
38
44
this .saveToPath = saveToPath ;
39
45
logPrinter = Log .getPrinter (EModule .SPLIT_MERGE_TOOL );
40
46
}
41
47
@ Override
42
48
protected Boolean call () {
43
- logPrinter . print ( "Merge file: " + filePath , EMsgType . INFO );
44
-
45
- File folder = new File (filePath );
49
+ try {
50
+ logPrinter . print ( "Merge file: " + filePath , EMsgType . INFO );
51
+ splitFile = new File (filePath );
46
52
47
- long cnkTotalSize = 0 ;
53
+ collectChunks ();
54
+ validateChunks ();
55
+ sortChunks ();
56
+ calculateChunksSizeSum ();
48
57
49
- File [] chunkFiles = folder .listFiles ((file , s ) -> s .matches ("^[0-9][0-9]$" ));
58
+ createFile ();
59
+ mergeChunksToFile ();
60
+ validateFile ();
50
61
51
- if (chunkFiles == null || chunkFiles .length == 0 ){
52
- logPrinter .print ("Selected folder doesn't have any chunks. Nothing to do here." , EMsgType .FAIL );
62
+ logPrinter .print ("Merge task complete!" , EMsgType .INFO );
63
+ logPrinter .close ();
64
+ return true ;
65
+ }
66
+ catch (Exception e ){
67
+ logPrinter .print (e .getMessage (), EMsgType .FAIL );
53
68
logPrinter .close ();
54
69
return false ;
55
70
}
71
+ }
72
+
73
+ private void collectChunks (){
74
+ chunkFiles = splitFile .listFiles ((file , s ) -> s .matches ("^[0-9][0-9]$" ));
75
+ }
76
+
77
+ private void validateChunks () throws Exception {
78
+ if (chunkFiles == null || chunkFiles .length == 0 ){
79
+ throw new Exception ("Selected folder doesn't have any chunks. Nothing to do here." );
80
+ }
81
+ }
56
82
83
+ private void sortChunks (){
57
84
Arrays .sort (chunkFiles );
85
+ }
58
86
87
+ private void calculateChunksSizeSum (){
59
88
logPrinter .print ("Next files will be merged in following order: " , EMsgType .INFO );
60
89
for (File cnk : chunkFiles ){
61
90
logPrinter .print (" " +cnk .getName (), EMsgType .INFO );
62
- cnkTotalSize += cnk .length ();
91
+ chunksTotalSize += cnk .length ();
63
92
}
93
+ }
64
94
65
- double chunkPercent = ( 4194240.0 / ( cnkTotalSize / 100.0 ) / 100.0 );
66
- long totalSizeCnt = 0 ;
95
+ private void createFile () throws Exception {
96
+ final String splitFileName = splitFile . getName () ;
67
97
68
- File resultFile = new File (saveToPath +File .separator +"!_" +folder . getName () );
69
- //*******
70
- for (int i = 0 ; ; i ++){
98
+ resultFile = new File (saveToPath +File .separator +"!_" +splitFileName );
99
+
100
+ for (int i = 0 ; i < 50 ; i ++){
71
101
if (this .isCancelled ()){
72
- logPrinter .print ("Split task interrupted!" , EMsgType .PASS );
73
- logPrinter .close ();
74
- return false ;
102
+ throw new InterruptedException ("Split task interrupted!" );
75
103
}
76
104
77
105
if (resultFile .exists ()){
78
- if (i >= 50 ){
79
- logPrinter .print ("Can't create new file." , EMsgType .FAIL );
80
- logPrinter .close ();
81
- return false ;
82
- }
83
-
84
106
logPrinter .print ("Trying to create a good new file..." , EMsgType .WARNING );
85
- resultFile = new File (saveToPath +File .separator +"!_" +i +"_" +folder . getName () );
107
+ resultFile = new File (saveToPath +File .separator +"!_" +i +"_" +splitFileName );
86
108
continue ;
87
109
}
110
+
88
111
logPrinter .print ("Save results to: " +resultFile .getAbsolutePath (), EMsgType .INFO );
89
- break ;
112
+ return ;
90
113
}
91
- //*******
114
+ throw new Exception ("Can't create new file." );
115
+ }
92
116
93
- try {
94
- BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (resultFile ));
95
-
96
- BufferedInputStream bis ;
97
- byte [] chunk ;
98
- int readBytesCnt ;
99
-
100
- for (File cnk : chunkFiles ){
101
- bis = new BufferedInputStream (new FileInputStream (cnk ));
102
- while (true ){
103
-
104
- if (this .isCancelled ()){
105
- bos .close ();
106
- bis .close ();
107
- boolean isDeleted = resultFile .delete ();
108
- logPrinter .print ("Split task interrupted and file " +(isDeleted ?"deleted." :"is not deleted." ), EMsgType .PASS );
109
- logPrinter .close ();
110
- return false ;
111
- }
112
-
113
- chunk = new byte [4194240 ];
114
- readBytesCnt = bis .read (chunk );
115
-
116
- logPrinter .updateProgress (chunkPercent * totalSizeCnt );
117
- totalSizeCnt ++;
118
-
119
- if (readBytesCnt < 4194240 ){
120
- if (readBytesCnt > 0 )
121
- bos .write (chunk , 0 , readBytesCnt );
122
- break ;
123
- }
124
-
125
- bos .write (chunk );
117
+ private void mergeChunksToFile () throws Exception {
118
+ double chunkPercent = (4194240.0 / (chunksTotalSize / 100.0 ) / 100.0 );
119
+ long totalSizeCnt = 0 ;
120
+
121
+ BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (resultFile ));
122
+
123
+ BufferedInputStream bis ;
124
+ byte [] chunk ;
125
+ int readBytesCnt ;
126
+
127
+ for (File chunkFile : chunkFiles ){
128
+ bis = new BufferedInputStream (new FileInputStream (chunkFile ));
129
+ while (true ){
130
+
131
+ if (this .isCancelled ()){
132
+ bos .close ();
133
+ bis .close ();
134
+ boolean isDeleted = resultFile .delete ();
135
+ throw new InterruptedException ("Merge task interrupted and file "
136
+ + (isDeleted ? "deleted." : "is not deleted." ));
126
137
}
127
- bis .close ();
128
- }
129
- bos .close ();
130
- //=============== let's check what we have ==============
131
- long resultFileSize = resultFile .length ();
132
- logPrinter .print ("Total chunks size: " + cnkTotalSize , EMsgType .INFO );
133
- logPrinter .print ("Merged file size: " + resultFileSize , EMsgType .INFO );
134
-
135
- if (cnkTotalSize != resultFileSize ){
136
- logPrinter .print ("Sizes are different! Do NOT use this file for installations!" , EMsgType .FAIL );
137
- return false ;
138
+
139
+ chunk = new byte [4194240 ];
140
+ readBytesCnt = bis .read (chunk );
141
+
142
+ logPrinter .updateProgress (chunkPercent * totalSizeCnt );
143
+ totalSizeCnt ++;
144
+
145
+ if (readBytesCnt < 4194240 ){
146
+ if (readBytesCnt > 0 )
147
+ bos .write (chunk , 0 , readBytesCnt );
148
+ break ;
149
+ }
150
+
151
+ bos .write (chunk );
138
152
}
139
- logPrinter .print ("Sizes are the same! Split file should be good!" , EMsgType .PASS );
140
- }
141
- catch (Exception e ){
142
- e .printStackTrace ();
143
- logPrinter .print ("Error: " +e .getMessage (), EMsgType .FAIL );
153
+ bis .close ();
144
154
}
155
+ bos .close ();
156
+ }
157
+
158
+ private void validateFile () throws Exception {
159
+ long resultFileSize = resultFile .length ();
160
+ logPrinter .print ("Total chunks size: " + chunksTotalSize , EMsgType .INFO );
161
+ logPrinter .print ("Merged file size: " + resultFileSize , EMsgType .INFO );
162
+
163
+ if (chunksTotalSize != resultFileSize )
164
+ throw new Exception ("Sizes are different! Do NOT use this file for installations!" );
145
165
146
- logPrinter .print ("Merge task complete!" , EMsgType .INFO );
147
- logPrinter .close ();
148
- return true ;
166
+ logPrinter .print ("Sizes are the same! Resulting file should be good!" , EMsgType .PASS );
149
167
}
150
168
}
0 commit comments