Revision 2
Updated some classes
FolderManager.java | ||
---|---|---|
38 | 38 |
File file = new File(name); |
39 | 39 |
file.mkdirs(); |
40 | 40 |
} |
41 |
|
|
42 |
public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException { |
|
43 |
ArrayList<Chunk> chunks = new ArrayList<>(); |
|
41 | 44 |
|
45 |
for (int i = 0; i <= chunksNumber; i++) { |
|
46 |
byte[] information = loadFile(new File(path + "/" + i)); |
|
47 |
Chunk chunk = new Chunk("", i, 1, information); |
|
48 |
chunks.add(chunk); |
|
49 |
} |
|
50 |
|
|
51 |
return chunks; |
|
52 |
} |
|
53 |
|
|
42 | 54 |
synchronized public static boolean backupFile(String name, String path, byte[] information) throws IOException { |
43 | 55 |
if (getFreeMemory() < information.length) { |
44 | 56 |
System.out.println("Without space to backup file"); |
... | ... | |
65 | 77 |
return information; |
66 | 78 |
} |
67 | 79 |
|
68 |
public static ArrayList<Chunk> loadChunks(String path, int chunksNumber) throws IOException { |
|
69 |
ArrayList<Chunk> chunks = new ArrayList<>(); |
|
70 |
|
|
71 |
for (int i = 0; i <= chunksNumber; i++) { |
|
72 |
byte[] information = loadFile(new File(path + "/" + i)); |
|
73 |
Chunk chunk = new Chunk("", i, 1, information); |
|
74 |
chunks.add(chunk); |
|
75 |
} |
|
76 |
|
|
77 |
return chunks; |
|
78 |
} |
|
79 |
|
|
80 | 80 |
public static ArrayList<Chunk> fileSplit(byte[] fileInformation, String fileID, int replication) { |
81 | 81 |
ArrayList<Chunk> chunks; |
82 | 82 |
int numChunks; |
... | ... | |
110 | 110 |
} |
111 | 111 |
|
112 | 112 |
|
113 |
public String getChunkPath(String fileID, int chunkNo) { |
|
114 |
return getChunksPath() + fileID + "/" + chunkNo; |
|
115 |
} |
|
116 |
|
|
117 | 113 |
public byte[] loadChunk(String fileID, int chunkNo) { |
118 | 114 |
byte[] chunkinformation = null; |
119 | 115 |
String chunkPath = getChunksPath() + fileID + "/" + chunkNo; |
... | ... | |
127 | 123 |
return chunkinformation; |
128 | 124 |
} |
129 | 125 |
|
130 |
|
|
131 |
public String getRootPath() { |
|
132 |
return path; |
|
133 |
} |
|
134 |
|
|
135 |
public String getChunksPath() { |
|
136 |
return path + Utils.CHUNKS; |
|
137 |
} |
|
138 |
|
|
139 |
public String getRestoresPath() { |
|
140 |
return path + Utils.RESTORES; |
|
141 |
} |
|
142 |
|
|
143 |
public FileManager getDatabase() { |
|
144 |
return fileManager; |
|
145 |
} |
|
146 |
|
|
147 | 126 |
public void deleteChunk(String fileID, int chunkNo) { |
148 |
String chunkPath = getChunkPath(fileID, chunkNo);
|
|
127 |
String chunkPath = getChunksPath() + fileID + "/" + chunkNo;
|
|
149 | 128 |
File file = new File(chunkPath); |
150 | 129 |
|
151 | 130 |
long chunkSize = file.length(); |
... | ... | |
154 | 133 |
fileManager.deleteChunk(fileID, chunkNo); |
155 | 134 |
} |
156 | 135 |
|
157 |
public static long getMaxMemory() { |
|
158 |
return maxMemory; |
|
159 |
} |
|
160 |
|
|
161 |
public static void setMaxMemory(int maxMemory) { |
|
162 |
FolderManager.maxMemory = maxMemory; |
|
163 |
} |
|
164 |
|
|
165 |
public static long getUsedMemory() { |
|
166 |
return FolderManager.usedMemory; |
|
167 |
} |
|
168 |
|
|
169 |
public static long getFreeMemory() { |
|
170 |
return maxMemory - usedMemory; |
|
171 |
} |
|
172 |
|
|
173 | 136 |
private static void decUsedMemory(long n) { |
174 | 137 |
usedMemory -= n; |
175 | 138 |
if (usedMemory < 0) { |
... | ... | |
187 | 150 |
System.out.println("Used memory: " + usedMemory + " / " + maxMemory); |
188 | 151 |
return true; |
189 | 152 |
} |
153 |
|
|
154 |
// Getters and setters |
|
155 |
|
|
156 |
public static long getMaxMemory() { return maxMemory; } |
|
157 |
|
|
158 |
public static void setMaxMemory(int maxMemory) { FolderManager.maxMemory = maxMemory; } |
|
159 |
|
|
160 |
public static long getUsedMemory() { return FolderManager.usedMemory; } |
|
161 |
|
|
162 |
public static long getFreeMemory() { return maxMemory - usedMemory; } |
|
163 |
|
|
164 |
public String getRootPath() { return path; } |
|
165 |
|
|
166 |
public String getChunksPath() { return path + Utils.CHUNKS; } |
|
167 |
|
|
168 |
public String getRestoresPath() { return path + Utils.RESTORES; } |
|
169 |
|
|
170 |
public FileManager getDatabase() { return fileManager; } |
|
171 |
|
|
172 |
public Peer getPeer() { return peer; } |
|
173 |
|
|
174 |
public void setPeer(Peer peer) { this.peer = peer; } |
|
175 |
|
|
176 |
public String getPath() { return path; } |
|
177 |
|
|
178 |
public void setPath(String path) { this.path = path; } |
|
179 |
|
|
180 |
public FileManager getFileManager() { return fileManager; } |
|
181 |
|
|
182 |
public void setFileManager(FileManager fileManager) { this.fileManager = fileManager; } |
|
183 |
|
|
184 |
public static int getChunkSizeLimit() { return chunkSizeLimit; } |
|
185 |
|
|
186 |
public static void setChunkSizeLimit(int chunkSizeLimit) { FolderManager.chunkSizeLimit = chunkSizeLimit; } |
|
187 |
|
|
188 |
public static void setMaxMemory(long maxMemory) { FolderManager.maxMemory = maxMemory; } |
|
189 |
|
|
190 |
public static void setUsedMemory(long usedMemory) { FolderManager.usedMemory = usedMemory; } |
|
190 | 191 |
} |
Also available in: Unified diff