|
2 | 2 |
|
3 | 3 | import java.io.ByteArrayOutputStream;
|
4 | 4 | import java.io.File;
|
5 |
| -import java.nio.file.Path; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.IOException; |
6 | 7 | import java.nio.file.Files;
|
| 8 | +import java.nio.file.Path; |
7 | 9 | import java.security.MessageDigest;
|
8 | 10 | import java.security.NoSuchAlgorithmException;
|
9 |
| -import java.io.IOException; |
10 | 11 |
|
11 | 12 | interface BazelSourceFileTarget {
|
12 | 13 | String getName();
|
13 | 14 | byte[] getDigest();
|
14 | 15 | }
|
15 | 16 |
|
16 | 17 | class BazelSourceFileTargetImpl implements BazelSourceFileTarget {
|
17 |
| - |
18 | 18 | private String name;
|
19 | 19 | private byte[] digest;
|
20 | 20 |
|
21 |
| - BazelSourceFileTargetImpl(String name, byte[] digest, Path workingDirectory) throws IOException, NoSuchAlgorithmException { |
| 21 | + BazelSourceFileTargetImpl(String name, byte[] digest, Path workingDirectory) |
| 22 | + throws IOException, NoSuchAlgorithmException { |
22 | 23 | this.name = name;
|
23 |
| - byte[] data = null; |
| 24 | + MessageDigest finalDigest = MessageDigest.getInstance("SHA-256"); |
24 | 25 | if (workingDirectory != null && name.startsWith("//")) {
|
25 | 26 | String filenameSubstring = name.substring(2);
|
26 | 27 | String filenamePath = filenameSubstring.replaceFirst(":", "/");
|
27 | 28 | File sourceFile = new File(workingDirectory.toString(), filenamePath);
|
28 | 29 | if (sourceFile.isFile() && sourceFile.canRead()) {
|
29 |
| - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
30 |
| - outputStream.write(Files.readAllBytes(sourceFile.toPath())); |
31 |
| - outputStream.write(digest); |
32 |
| - data = outputStream.toByteArray(); |
33 |
| - outputStream.close(); |
| 30 | + byte[] buffer = new byte[16384]; |
| 31 | + FileInputStream in = new FileInputStream(sourceFile); |
| 32 | + int rc = in.read(buffer); |
| 33 | + while (rc != -1) { |
| 34 | + finalDigest.update(buffer, 0, rc); |
| 35 | + rc = in.read(buffer); |
| 36 | + } |
34 | 37 | }
|
35 |
| - } else { |
36 |
| - data = digest; |
37 |
| - } |
38 |
| - MessageDigest finalDigest = MessageDigest.getInstance("SHA-256"); |
39 |
| - if (data != null) { |
40 |
| - finalDigest.update(data); |
41 | 38 | }
|
| 39 | + finalDigest.update(digest); |
42 | 40 | finalDigest.update(name.getBytes());
|
43 | 41 | this.digest = finalDigest.digest();
|
44 | 42 | }
|
|
0 commit comments