Кто может помочь? превышает время выполнения почему то.
Хотя фильм Гарри поттер в avi (1.5гб) разбитый на части, собирается в один файл за 11 секунд.
package com.javarush.task.task18.task1825;
import java.io.*;
import java.util.*;
/*
Собираем файл
*/
public class Solution {
private volatile static Map<Integer,byte[]> allBytes;
public static void main(String[] args) throws IOException, InterruptedException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Set<String> filePieces = new TreeSet<>();
String filePath = "";
while(true){
String fileName = reader.readLine();
if(filePath.length()<1){
filePath = fileName.substring(0,fileName.lastIndexOf(".part"));
}
if(fileName.equals("end")) break;
filePieces.add(fileName);
}
reader.close();
allBytes = new HashMap<>();
readPiecesParallel(filePieces);
while(allBytes.size()!=filePieces.size()){
Thread.sleep(2000);
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
for (int i = 1; i <= allBytes.size(); i++) {
bos.write(allBytes.get(i));
bos.flush();
}
bos.flush();
bos.close();
}
private static void readPiecesParallel(Set<String> filePieces) {
for (String fileName:filePieces) {
ReadFileThread thread = new ReadFileThread(fileName);
thread.start();
}
}
private static class ReadFileThread extends Thread{
private String fileName;
private int partNumber;
public ReadFileThread(String fileName){
this.fileName = fileName;
}
@Override
public void run() {
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(this.fileName));
String str = fileName.substring(0,fileName.lastIndexOf("."));
partNumber = Integer.parseInt(str.substring(str.lastIndexOf(".part")).substring(5));
byte[] buff = new byte[bis.available()];
while (bis.available()>0){
bis.read(buff);
}
synchronized (allBytes){
allBytes.put(partNumber,buff);
}
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
}