-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtility.java
More file actions
83 lines (76 loc) · 3.33 KB
/
FileUtility.java
File metadata and controls
83 lines (76 loc) · 3.33 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
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
public class FileUtility {
private static ApplicationProperties properties;
private static int pieceSize;
private static int fileSize;
private static String filePath;
public static int peerId;
static{
properties = ApplicationProperties.getInstance();
pieceSize = Integer.parseInt(properties.get("PieceSize"));
fileSize = Integer.parseInt(properties.get("FileSize"));
peerId = peerProcess.peerId;
//System.out.println("Printing P2PMain id " + PeerProcess.peerId);
filePath = ".\\peer_" + peerId + "\\" + properties.get("FileName");
createEmptyFile();
}
public static synchronized void createEmptyFile(){
File file = new File("peer_" + peerId);
if(file.exists()){
filePath = file.getPath() + "/" + properties.get("FileName");
return;
}
file.mkdir();
filePath = file.getPath() + "/" + properties.get("FileName");
try(OutputStream stream = new FileOutputStream(filePath)){
byte data = 0;
int fileSize = Integer.parseInt(properties.get("FileSize"));
for(int i = 0; i < fileSize; i++) {
stream.write(data);
}
}catch(Exception e){
Logger.logInfo("exception occurred while creating new file: " + e.getMessage());
}
}
public static synchronized byte[] getPieceBytes(int pieceIndex){
int pieceLength = Math.min(fileSize - pieceIndex * pieceSize, pieceSize);
byte[] piece = new byte[pieceLength];
try(RandomAccessFile file = new RandomAccessFile(filePath,"rw")){
FileChannel channel = file.getChannel();
int lockStartIndex = pieceIndex * pieceSize;
int lockEndIndex = pieceIndex * pieceSize + pieceLength;
channel.lock(lockStartIndex,lockEndIndex,false);
ByteBuffer buff = ByteBuffer.allocate(pieceLength);
channel.read(buff,pieceIndex * pieceSize);
buff.rewind();
buff.get(piece);
//System.out.println("piece : "+new String(piece, StandardCharsets.UTF_8));
buff.clear();
channel.close();
}catch(Exception ex){
Logger.logInfo("exception occurred while retrieve piece(" + pieceIndex + ") from the file : " +
ex.getMessage());
ex.printStackTrace();
}
return piece;
}
public static synchronized void writePieceToFile(int pieceIndex, byte[] data){
//int pieceLength = Math.min(fileSize - pieceIndex * pieceSize, pieceSize);
try(RandomAccessFile file = new RandomAccessFile(filePath, "rw")){
FileChannel channel = file.getChannel();
ByteBuffer buff = ByteBuffer.wrap(data);
int lockStartIndex = pieceIndex * pieceSize;
int lockEndIndex = pieceIndex * pieceSize + data.length;
FileLock lock = channel.lock(lockStartIndex, lockEndIndex,false);
channel.write(buff, (long) pieceIndex * pieceSize);
buff.clear();
channel.close();
}catch(Exception e){
Logger.logInfo("Exception occurred while writing piece("+ pieceIndex + ") to file : "
+ e.getMessage());
}
}
}