@@ -92,7 +92,11 @@ public void execute() throws BuildException {
9292// }
9393
9494 //download(path, jdk, platform, bits, version, update, build);
95- download ();
95+ try {
96+ download ();
97+ } catch (IOException e ) {
98+ throw new BuildException (e );
99+ }
96100
97101 /*
98102 downloadJRE("linux-i586.tar.gz");
@@ -116,7 +120,7 @@ public void execute() throws BuildException {
116120// static void download(String path, //File folder, String filename,
117121// boolean jdk, String platform, String bits,
118122// int version, int update, int build) {
119- void download () {
123+ void download () throws IOException {
120124 //HttpURLConnection.setFollowRedirects(true);
121125 String filename = (jdk ? "jdk" : "jre" ) +
122126 (update == 0 ?
@@ -134,55 +138,62 @@ void download() {
134138 (update == 0 ?
135139 String .format ("%d-b%02d/" , version , build ) :
136140 String .format ("%du%d-b%02d/" , version , update , build )) + filename ;
137- System .out .println (url );
141+ // System.out.println(url);
138142
139- try {
140- HttpURLConnection conn =
143+ HttpURLConnection conn =
141144 (HttpURLConnection ) new URL (url ).openConnection ();
142- //conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
145+ //conn.setRequestProperty("Cookie", "name1=value1; name2=value2");
146+ conn .setRequestProperty ("Cookie" , COOKIE );
147+ //conn.setRequestProperty("Cookie", "gpw_e24=http://www.oracle.com/");
148+
149+ //printHeaders(conn);
150+ //conn.connect();
151+ if (conn .getResponseCode () == 302 ) {
152+ Map <String , List <String >> headers = conn .getHeaderFields ();
153+ List <String > location = headers .get ("Location" );
154+ if (location .size () == 1 ) {
155+ url = location .get (0 );
156+ } else {
157+ throw new RuntimeException ("Got " + location .size () + " locations." );
158+ }
159+ List <String > cookies = headers .get ("Set-Cookie" );
160+ conn = (HttpURLConnection ) new URL (url ).openConnection ();
161+ for (String cookie : cookies ) {
162+ conn .setRequestProperty ("Cookie" , cookie );
163+ }
143164 conn .setRequestProperty ("Cookie" , COOKIE );
144- //conn.setRequestProperty("Cookie", "gpw_e24=http://www.oracle.com/");
145-
146- //printHeaders(conn);
147- //conn.connect();
148- if (conn .getResponseCode () == 302 ) {
149- Map <String , List <String >> headers = conn .getHeaderFields ();
150- List <String > location = headers .get ("Location" );
151- if (location .size () == 1 ) {
152- url = location .get (0 );
153- } else {
154- throw new RuntimeException ("Got " + location .size () + " locations." );
155- }
156- List <String > cookies = headers .get ("Set-Cookie" );
157- conn = (HttpURLConnection ) new URL (url ).openConnection ();
158- for (String cookie : cookies ) {
159- conn .setRequestProperty ("Cookie" , cookie );
160- }
161- conn .setRequestProperty ("Cookie" , COOKIE );
162- conn .connect ();
165+ conn .connect ();
166+ }
167+
168+ if (conn .getResponseCode () == 200 ) {
169+ InputStream input = conn .getInputStream ();
170+ BufferedInputStream bis = new BufferedInputStream (input );
171+ File outputFile = new File (path ); //folder, filename);
172+ File tempFile = File .createTempFile ("download" , "" , outputFile .getParentFile ());
173+ BufferedOutputStream output =
174+ new BufferedOutputStream (new FileOutputStream (tempFile ));
175+ int c = bis .read ();
176+ while (c != -1 ) {
177+ output .write (c );
178+ c = bis .read ();
163179 }
180+ bis .close ();
181+ output .flush ();
182+ output .close ();
164183
165- if (conn .getResponseCode () == 200 ) {
166- InputStream input = conn .getInputStream ();
167- BufferedInputStream bis = new BufferedInputStream (input );
168- File outputFile = new File (path ); //folder, filename);
169- BufferedOutputStream output =
170- new BufferedOutputStream (new FileOutputStream (outputFile ));
171- int c = bis .read ();
172- while (c != -1 ) {
173- output .write (c );
174- c = bis .read ();
184+ if (outputFile .exists ()) {
185+ if (!outputFile .delete ()) {
186+ throw new BuildException ("Could not delete old download: " + outputFile .getAbsolutePath ());
175187 }
176- bis .close ();
177- output .flush ();
178- output .close ();
179- } else {
180- printHeaders (conn );
181- System .exit (1 );
182188 }
183-
184- } catch (Exception e ) {
185- e .printStackTrace ();
189+ if (!tempFile .renameTo (outputFile )) {
190+ throw new BuildException (String .format ("Could not rename %s to %s" ,
191+ tempFile .getAbsolutePath (),
192+ outputFile .getAbsolutePath ()));
193+ }
194+ } else {
195+ printHeaders (conn );
196+ System .exit (1 );
186197 }
187198 }
188199
0 commit comments