Ignore:
Timestamp:
2014-05-01T02:34:43+02:00 (10 years ago)
Author:
Don-vip
Message:

see #8465 - global use of try-with-resources, according to

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java

    r6643 r7033  
    8383     */
    8484    public void download() throws DownloadException {
    85         OutputStream out = null;
    86         InputStream in = null;
    8785        try {
    8886            if (mkdir) {
     
    105103            progressMonitor.subTask(tr("Downloading File {0}: {1} bytes...", file.getName(),size));
    106104
    107             in = downloadConnection.getInputStream();
    108             out = new FileOutputStream(file);
    109             byte[] buffer = new byte[32768];
    110             int count=0;
    111             int p1=0, p2=0;
    112             for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    113                 out.write(buffer, 0, read);
    114                 count+=read;
    115                 if (canceled) break;
    116                 p2 = 100 * count / size;
    117                 if (p2!=p1) {
    118                     progressMonitor.setTicks(p2);
    119                     p1=p2;
     105            try (
     106                InputStream in = downloadConnection.getInputStream();
     107                OutputStream out = new FileOutputStream(file)
     108            ) {
     109                byte[] buffer = new byte[32768];
     110                int count=0;
     111                int p1=0, p2=0;
     112                for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
     113                    out.write(buffer, 0, read);
     114                    count+=read;
     115                    if (canceled) break;
     116                    p2 = 100 * count / size;
     117                    if (p2!=p1) {
     118                        progressMonitor.setTicks(p2);
     119                        p1=p2;
     120                    }
    120121                }
    121122            }
    122             Utils.close(out);
    123123            if (!canceled) {
    124124                Main.info(tr("Download finished"));
     
    139139        } finally {
    140140            closeConnectionIfNeeded();
    141             Utils.close(out);
    142141        }
    143142    }
     
    170169     */
    171170    public static void unzipFileRecursively(File file, String dir) throws IOException {
    172         OutputStream os = null;
    173         InputStream is = null;
    174         ZipFile zf = null;
    175         try {
    176             zf = new ZipFile(file);
    177             Enumeration<?> es = zf.entries();
    178             ZipEntry ze;
     171        try (ZipFile zf = new ZipFile(file)) {
     172            Enumeration<? extends ZipEntry> es = zf.entries();
    179173            while (es.hasMoreElements()) {
    180                 ze = (ZipEntry) es.nextElement();
     174                ZipEntry ze = es.nextElement();
    181175                File newFile = new File(dir, ze.getName());
    182176                if (ze.isDirectory()) {
    183177                    newFile.mkdirs();
    184                 } else {
    185                     is = zf.getInputStream(ze);
    186                     os = new BufferedOutputStream(new FileOutputStream(newFile));
     178                } else try (
     179                    InputStream is = zf.getInputStream(ze);
     180                    OutputStream os = new BufferedOutputStream(new FileOutputStream(newFile))
     181                ) {
    187182                    byte[] buffer = new byte[8192];
    188183                    int read;
     
    190185                        os.write(buffer, 0, read);
    191186                    }
    192                     Utils.close(os);
    193                     Utils.close(is);
    194187                }
    195188            }
    196         } finally {
    197             Utils.close(zf);
    198189        }
    199190    }
    200191}
    201 
Note: See TracChangeset for help on using the changeset viewer.