|
1 | 1 | package lombok.eclipse.dependencies;
|
2 | 2 |
|
3 | 3 | import java.io.BufferedInputStream;
|
| 4 | +import java.io.ByteArrayInputStream; |
4 | 5 | import java.io.File;
|
| 6 | +import java.io.FileOutputStream; |
5 | 7 | import java.io.FilenameFilter;
|
6 | 8 | import java.io.IOException;
|
7 | 9 | import java.io.InputStream;
|
8 | 10 | import java.io.InputStreamReader;
|
| 11 | +import java.io.OutputStream; |
9 | 12 | import java.net.HttpURLConnection;
|
10 | 13 | import java.net.MalformedURLException;
|
11 | 14 | import java.net.URL;
|
12 |
| -import java.nio.file.Files; |
13 |
| -import java.nio.file.Path; |
14 |
| -import java.nio.file.Paths; |
15 |
| -import java.nio.file.StandardCopyOption; |
| 15 | +import java.nio.charset.StandardCharsets; |
16 | 16 | import java.util.Arrays;
|
17 | 17 | import java.util.regex.Matcher;
|
18 | 18 | import java.util.regex.Pattern;
|
@@ -70,18 +70,34 @@ private static String readUrlAsString(String url) throws MalformedURLException,
|
70 | 70 | }
|
71 | 71 |
|
72 | 72 | private static void downloadFile(String filename, String repositoryUrl, String target) throws IOException {
|
73 |
| - Files.createDirectories(Paths.get(target)); |
74 |
| - Path targetFile = Paths.get(target, filename); |
75 |
| - if (Files.exists(targetFile)) { |
| 73 | + new File(target).mkdirs(); |
| 74 | + File targetFile = new File(target, filename); |
| 75 | + if (targetFile.exists()) { |
76 | 76 | System.out.println("File '" + filename + "' already exists");
|
77 | 77 | return;
|
78 | 78 | }
|
79 | 79 | System.out.print("Downloading '" + filename + "'... ");
|
| 80 | + InputStream in = null; |
| 81 | + OutputStream out = null; |
80 | 82 | try {
|
81 |
| - Files.copy(getStreamForUrl(repositoryUrl + filename), targetFile, StandardCopyOption.REPLACE_EXISTING); |
| 83 | + in = getStreamForUrl(repositoryUrl + filename); |
| 84 | + out = new FileOutputStream(targetFile); |
| 85 | + copy(in, out); |
82 | 86 | System.out.println("[done]");
|
83 | 87 | } catch(IOException e) {
|
84 | 88 | System.out.println("[error]");
|
| 89 | + } finally { |
| 90 | + if (in != null) try { in.close(); } catch (Exception ignore) {} |
| 91 | + if (out != null) out.close(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static void copy(InputStream from, OutputStream to) throws IOException { |
| 96 | + byte[] b = new byte[4096]; |
| 97 | + while (true) { |
| 98 | + int r = from.read(b); |
| 99 | + if (r == -1) return; |
| 100 | + to.write(b, 0, r); |
85 | 101 | }
|
86 | 102 | }
|
87 | 103 |
|
@@ -126,6 +142,12 @@ private static void writeEclipseLibrary(String target, String eclipseVersion) th
|
126 | 142 | sb.append("</library>\n");
|
127 | 143 | sb.append("</eclipse-userlibraries>\n");
|
128 | 144 |
|
129 |
| - Files.writeString(Paths.get(target, eclipseVersion + ".userlibraries"), sb.toString()); |
| 145 | + OutputStream out = null; |
| 146 | + try { |
| 147 | + out = new FileOutputStream(new File(target, eclipseVersion + ".userlibraries")); |
| 148 | + copy(new ByteArrayInputStream(sb.toString().getBytes(StandardCharsets.UTF_8)), out); |
| 149 | + } finally { |
| 150 | + if (out != null) out.close(); |
| 151 | + } |
130 | 152 | }
|
131 | 153 | }
|
0 commit comments