Changeset 30436 in osm for applications/editors/josm/plugins/opendata/src
- Timestamp:
- 2014-05-09T05:21:14+02:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/SevenZipReader.java
r30340 r30436 35 35 public SevenZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) throws IOException { 36 36 super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser); 37 OutputStream out = null; 38 try { 39 // Write entire 7z file as a temp file on disk as we need random access later, and "in" can be a network stream 40 File tmpFile = File.createTempFile("7z_", ".7z", OdUtils.createTempDir()); 41 out = new FileOutputStream(tmpFile); 37 // Write entire 7z file as a temp file on disk as we need random access later, and "in" can be a network stream 38 File tmpFile = File.createTempFile("7z_", ".7z", OdUtils.createTempDir()); 39 try (OutputStream out = new FileOutputStream(tmpFile)) { 42 40 Utils.copyStream(in, out); 43 Utils.close(out); 44 out = null; 45 IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r"); 46 if (archive.Open(random) != 0) { 47 String message = "Unable to open 7z archive: "+tmpFile.getPath(); 48 Main.warn(message); 49 random.close(); 50 if (!tmpFile.delete()) { 51 tmpFile.deleteOnExit(); 52 } 53 throw new IOException(message); 41 } 42 IInStream random = new MyRandomAccessFile(tmpFile.getPath(), "r"); 43 if (archive.Open(random) != 0) { 44 String message = "Unable to open 7z archive: "+tmpFile.getPath(); 45 Main.warn(message); 46 random.close(); 47 if (!tmpFile.delete()) { 48 tmpFile.deleteOnExit(); 54 49 } 55 } finally { 56 Utils.close(out); 50 throw new IOException(message); 57 51 } 58 52 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleDownloadTask.java
r30340 r30436 22 22 import org.openstreetmap.josm.plugins.opendata.OdPlugin; 23 23 import org.openstreetmap.josm.tools.CheckParameterUtil; 24 import org.openstreetmap.josm.tools.Utils;25 24 import org.xml.sax.SAXException; 26 25 … … 92 91 93 92 protected void download(ModuleInformation pi, File file) throws ModuleDownloadException{ 94 OutputStream out = null;95 InputStream in = null;96 93 try { 97 94 if (pi.downloadlink == null) { … … 108 105 downloadConnection.connect(); 109 106 } 110 in = downloadConnection.getInputStream(); 111 out = new FileOutputStream(file); 112 byte[] buffer = new byte[8192]; 113 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 114 out.write(buffer, 0, read); 107 try ( 108 InputStream in = downloadConnection.getInputStream(); 109 OutputStream out = new FileOutputStream(file) 110 ) { 111 byte[] buffer = new byte[8192]; 112 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 113 out.write(buffer, 0, read); 114 } 115 115 } 116 out.close();117 in.close();118 116 } catch(MalformedURLException e) { 119 117 String msg = tr("Warning: Cannot download module ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink); … … 125 123 throw new ModuleDownloadException(e); 126 124 } finally { 127 Utils.close(in);128 125 synchronized(this) { 129 126 downloadConnection = null; 130 127 } 131 Utils.close(out);132 128 } 133 129 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadLocalModuleInformationTask.java
r30340 r30436 174 174 175 175 protected void processLocalModuleInformationFile(File file) throws ModuleListParseException{ 176 FileInputStream fin = null; 177 try { 178 fin = new FileInputStream(file); 176 try (FileInputStream fin = new FileInputStream(file)) { 179 177 List<ModuleInformation> pis = new ModuleListParser().parse(fin); 180 178 for (ModuleInformation pi : pis) { … … 187 185 } catch(IOException e) { 188 186 throw new ModuleListParseException(e); 189 } finally {190 Utils.close(fin);191 187 } 192 188 } -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadRemoteModuleInformationTask.java
r30340 r30436 33 33 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 34 34 import org.openstreetmap.josm.tools.ImageProvider; 35 import org.openstreetmap.josm.tools.Utils;36 35 import org.xml.sax.SAXException; 37 36 … … 142 141 */ 143 142 protected String downloadModuleList(String site, ProgressMonitor monitor) { 144 BufferedReader in = null;145 143 StringBuilder sb = new StringBuilder(); 146 144 try { … … 159 157 connection.setRequestProperty("Accept-Charset", "utf-8"); 160 158 } 161 in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 162 String line; 163 while((line = in.readLine()) != null) { 164 sb.append(line).append("\n"); 165 } 166 return sb.toString(); 159 try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) { 160 String line; 161 while((line = in.readLine()) != null) { 162 sb.append(line).append("\n"); 163 } 164 return sb.toString(); 165 } 167 166 } catch(MalformedURLException e) { 168 167 if (canceled) return null; … … 180 179 connection = null; 181 180 } 182 Utils.close(in);183 181 monitor.finishTask(); 184 182 } … … 192 190 */ 193 191 protected void downloadModuleIcons(String site, File destFile, ProgressMonitor monitor) { 194 InputStream in = null;195 OutputStream out = null;196 192 try { 197 193 site = site.replaceAll("%<(.*)>", ""); … … 207 203 connection.setRequestProperty("Host", url.getHost()); 208 204 } 209 in = connection.getInputStream(); 210 out = new FileOutputStream(destFile); 211 byte[] buffer = new byte[8192]; 212 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 213 out.write(buffer, 0, read); 214 } 215 out.close(); 216 in.close(); 205 try ( 206 InputStream in = connection.getInputStream(); 207 OutputStream out = new FileOutputStream(destFile) 208 ) { 209 byte[] buffer = new byte[8192]; 210 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { 211 out.write(buffer, 0, read); 212 } 213 } 217 214 } catch(MalformedURLException e) { 218 215 if (canceled) return; … … 230 227 connection = null; 231 228 } 232 Utils.close(in);233 229 monitor.finishTask(); 234 230 }
Note:
See TracChangeset
for help on using the changeset viewer.