root / src / FileManager.java
History | View | Annotate | Download (4.35 KB)
1 | 3 | up20160792 | import java.io.*; |
---|---|---|---|
2 | import java.nio.file.*; |
||
3 | import java.security.*; |
||
4 | import java.nio.charset.*; |
||
5 | |||
6 | public class FileManager { |
||
7 | |||
8 | public static int chunkSize = 64000; |
||
9 | |||
10 | // Save byte array chunk to path
|
||
11 | public static void saveChunk(byte[] bytes, String path, int chunkN) throws FileNotFoundException, IOException { |
||
12 | |||
13 | String fileName = path + Integer.toString(chunkN); |
||
14 | |||
15 | try (FileOutputStream stream = new FileOutputStream(fileName)) { |
||
16 | stream.write(bytes); |
||
17 | } |
||
18 | |||
19 | } |
||
20 | |||
21 | // Hash a string with SHA256
|
||
22 | public static String hash(String input) { |
||
23 | |||
24 | try {
|
||
25 | |||
26 | byte[] bytes = digestHashing(input); |
||
27 | return bytesToHex(bytes);
|
||
28 | |||
29 | } catch (Exception e) { |
||
30 | |||
31 | e.printStackTrace(); |
||
32 | |||
33 | } |
||
34 | |||
35 | return null; |
||
36 | |||
37 | } |
||
38 | |||
39 | // Use MessageDigest to hash in bytes
|
||
40 | private static byte[] digestHashing(String input) throws NoSuchAlgorithmException { |
||
41 | |||
42 | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
||
43 | return digest.digest(input.getBytes(StandardCharsets.UTF_8));
|
||
44 | |||
45 | } |
||
46 | |||
47 | // Use bytes from MessageDigest to a String
|
||
48 | private static String bytesToHex(byte[] hash) { |
||
49 | |||
50 | StringBuffer hexString = new StringBuffer(); |
||
51 | |||
52 | for (int i = 0; i < hash.length; i++) { |
||
53 | |||
54 | String hex = Integer.toHexString(0xff & hash[i]); |
||
55 | |||
56 | if (hex.length() == 1) |
||
57 | hexString.append('0');
|
||
58 | |||
59 | hexString.append(hex); |
||
60 | |||
61 | } |
||
62 | |||
63 | return hexString.toString();
|
||
64 | |||
65 | } |
||
66 | |||
67 | // Delete directory and files
|
||
68 | public static void deleteDir(File file) { |
||
69 | File[] contents = file.listFiles(); |
||
70 | if (contents != null) { |
||
71 | for (File f : contents) { |
||
72 | if (!Files.isSymbolicLink(f.toPath())) {
|
||
73 | deleteDir(f); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | file.delete(); |
||
78 | } |
||
79 | |||
80 | // Read all bytes of file(generic use)
|
||
81 | public static byte[] readFile(String path) { |
||
82 | |||
83 | Path filePath = Paths.get(path); |
||
84 | |||
85 | byte[] bytes = null; |
||
86 | |||
87 | try {
|
||
88 | bytes = Files.readAllBytes(filePath); |
||
89 | } catch (Exception e) { |
||
90 | e.printStackTrace(); |
||
91 | } |
||
92 | |||
93 | return bytes;
|
||
94 | } |
||
95 | |||
96 | // Split file in chunks
|
||
97 | public static byte[][] split(byte[] fileBytes) { |
||
98 | |||
99 | // Number of chunks needed is always integer division plus 1 which may be
|
||
100 | // partially used or empty
|
||
101 | int nChunks = 1 + fileBytes.length / chunkSize; |
||
102 | |||
103 | // Total bytes
|
||
104 | int bytes = fileBytes.length;
|
||
105 | int rBytes = 0; |
||
106 | |||
107 | // Chunks seperated
|
||
108 | byte[][] chunks = new byte[nChunks][]; |
||
109 | for (int i = 0; i < nChunks; i++) { |
||
110 | |||
111 | // Calculate proper size
|
||
112 | int sizeToCopy = chunkSize;
|
||
113 | if (bytes < sizeToCopy)
|
||
114 | sizeToCopy = bytes; |
||
115 | |||
116 | // Chunk with proper size
|
||
117 | chunks[i] = new byte[sizeToCopy]; |
||
118 | |||
119 | // Copy to chunk
|
||
120 | System.arraycopy(fileBytes, rBytes, chunks[i], 0, sizeToCopy); |
||
121 | |||
122 | // Remove form total bytes
|
||
123 | bytes -= sizeToCopy; |
||
124 | rBytes += sizeToCopy; |
||
125 | |||
126 | } |
||
127 | return chunks;
|
||
128 | } |
||
129 | |||
130 | // Merge chunks into single byte array
|
||
131 | public static byte[] merge(byte[][] chunks) throws IOException { |
||
132 | |||
133 | // Total bytes
|
||
134 | int tBytes = 0; |
||
135 | for (int i = 0; i < chunks.length; i++) { |
||
136 | tBytes += chunks[i].length; |
||
137 | } |
||
138 | |||
139 | byte[] mergedFile = new byte[tBytes]; |
||
140 | |||
141 | // Written bytes count
|
||
142 | int nBytes = 0; |
||
143 | |||
144 | // Write bytes
|
||
145 | for (int i = 0; i < chunks.length; i++) { |
||
146 | System.arraycopy(chunks[i], 0, mergedFile, nBytes, chunks[i].length); |
||
147 | nBytes += chunks[i].length; |
||
148 | } |
||
149 | |||
150 | return mergedFile;
|
||
151 | } |
||
152 | |||
153 | // Test main for split and merge file
|
||
154 | public static void main(String[] args) { |
||
155 | |||
156 | String fileName = args[0]; |
||
157 | Path filePath = Paths.get(fileName); |
||
158 | byte[] fileBytes = null; |
||
159 | |||
160 | try {
|
||
161 | fileBytes = Files.readAllBytes(filePath); |
||
162 | } catch (Exception e) { |
||
163 | e.printStackTrace(); |
||
164 | } |
||
165 | |||
166 | byte[][] chunks = split(fileBytes); |
||
167 | |||
168 | try {
|
||
169 | fileBytes = merge(chunks); |
||
170 | } catch (Exception e) { |
||
171 | e.printStackTrace(); |
||
172 | } |
||
173 | |||
174 | } |
||
175 | |||
176 | } |