1 36 package org.ungoverned.oscar.installer.artifact; 37 38 import java.io.*; 39 import java.net.URL ; 40 import java.net.URLConnection ; 41 42 import org.ungoverned.oscar.installer.Status; 43 import org.ungoverned.oscar.installer.StringProperty; 44 45 public class URLFileArtifact extends AbstractFileArtifact 46 { 47 public URLFileArtifact(StringProperty sourceName, StringProperty destName) 48 { 49 this(sourceName, destName, null); 50 } 51 52 public URLFileArtifact( 53 StringProperty sourceName, StringProperty destName, StringProperty destDir) 54 { 55 this(sourceName, destName, destDir, false); 56 } 57 58 public URLFileArtifact( 59 StringProperty sourceName, StringProperty destName, 60 StringProperty destDir, boolean localize) 61 { 62 super(sourceName, destName, destDir, localize); 63 } 64 65 public InputStream getInputStream(Status status) 66 throws IOException 67 { 68 String fileName = getSourceName().getStringValue(); 69 fileName = (fileName.lastIndexOf('/') > 0) 70 ? fileName.substring(fileName.lastIndexOf('/') + 1) 71 : fileName; 72 73 status.setText("Connecting..."); 74 75 File file = File.createTempFile("oscar-install.tmp", null); 76 file.deleteOnExit(); 77 78 OutputStream os = new FileOutputStream(file); 79 URLConnection conn = new URL (getSourceName().getStringValue()).openConnection(); 80 int total = conn.getContentLength(); 81 InputStream is = conn.getInputStream(); 82 83 int count = 0; 84 for (int len = is.read(s_buffer); len > 0; len = is.read(s_buffer)) 85 { 86 count += len; 87 os.write(s_buffer, 0, len); 88 if (total > 0) 89 { 90 status.setText("Downloading " + fileName 91 + " ( " + count + " bytes of " + total + " )."); 92 } 93 else 94 { 95 status.setText("Downloading " + fileName + " ( " + count + " bytes )."); 96 } 97 } 98 99 os.close(); 100 is.close(); 101 102 return new FileInputStream(file); 103 } 104 105 public String toString() 106 { 107 return "URL FILE: " + getSourceName().getStringValue(); 108 } 109 } | Popular Tags |