Changeset 30436 in osm


Ignore:
Timestamp:
2014-05-09T05:21:14+02:00 (10 years ago)
Author:
donvip
Message:

[josm_plugins] see #josm8465 - Java 7 code update

Location:
applications/editors/josm/plugins
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/CommandLine/src/CommandLine/CommandLine.java

    r30203 r30436  
    549549
    550550                if (tracks) {
    551                     final GpxWriter gpxWriter = new GpxWriter(printWriter);
    552                     GpxFilter gpxFilter = new GpxFilter();
    553                     gpxFilter.initBboxFilter(bbox);
    554                     List<GpxLayer> gpxLayers = Main.map.mapView.getLayersOfType(GpxLayer.class);
    555                     for (GpxLayer gpxLayer : gpxLayers) {
    556                         gpxFilter.addGpxData(gpxLayer.data);
    557                     }
    558                     gpxWriter.write(gpxFilter.getGpxData());
    559                     Utils.close(gpxWriter);
     551                    try (GpxWriter gpxWriter = new GpxWriter(printWriter)) {
     552                        GpxFilter gpxFilter = new GpxFilter();
     553                        gpxFilter.initBboxFilter(bbox);
     554                        List<GpxLayer> gpxLayers = Main.map.mapView.getLayersOfType(GpxLayer.class);
     555                        for (GpxLayer gpxLayer : gpxLayers) {
     556                            gpxFilter.addGpxData(gpxLayer.data);
     557                        }
     558                        gpxWriter.write(gpxFilter.getGpxData());
     559                    } catch (IOException e) {
     560                        Main.warn(e);
     561                    }
    560562                }
    561563                Utils.close(osmWriter);
  • applications/editors/josm/plugins/mirrored_download/src/mirrored_download/UrlSelectionDialog.java

    r30354 r30436  
    2626import org.openstreetmap.josm.Main;
    2727import org.openstreetmap.josm.io.MirroredInputStream;
    28 import org.openstreetmap.josm.tools.Utils;
    2928
    3029public class UrlSelectionDialog
     
    106105    String src = Main.pref.get("plugin.mirrored_download.url-src", "https://josm.openstreetmap.de/mirrored_download_info");
    107106    Collection<String> urls = new ArrayList<String>();
    108     InputStream in = null;
    109     try {
    110       in = new MirroredInputStream(src, 24*60*60);
    111       BufferedReader reader = new BufferedReader(new InputStreamReader(in));
     107    try (
     108      InputStream in = new MirroredInputStream(src, 24*60*60);
     109      BufferedReader reader = new BufferedReader(new InputStreamReader(in))
     110    ) {
    112111      String line = null;
    113112      while ((line = reader.readLine()) != null) {
     
    117116        }
    118117      }
    119       Utils.close(reader);
    120118    } catch (IOException e) {
    121       e.printStackTrace();
    122     } finally {
    123       Utils.close(in);
     119      Main.error(e);
    124120    }
    125121    for (String url : Main.pref.getCollection("plugin.mirrored_download.custom-urls")) {
     
    136132          cbSelectUrl.getSelectedItem().toString());
    137133    }
    138 
    139134  }
    140135
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/SevenZipReader.java

    r30340 r30436  
    3535    public SevenZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) throws IOException {
    3636        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)) {
    4240            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();
    5449            }
    55         } finally {
    56             Utils.close(out);
     50            throw new IOException(message);
    5751        }
    5852    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ModuleDownloadTask.java

    r30340 r30436  
    2222import org.openstreetmap.josm.plugins.opendata.OdPlugin;
    2323import org.openstreetmap.josm.tools.CheckParameterUtil;
    24 import org.openstreetmap.josm.tools.Utils;
    2524import org.xml.sax.SAXException;
    2625
     
    9291
    9392    protected void download(ModuleInformation pi, File file) throws ModuleDownloadException{
    94         OutputStream out = null;
    95         InputStream in = null;
    9693        try {
    9794            if (pi.downloadlink == null) {
     
    108105                downloadConnection.connect();
    109106            }
    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                }
    115115            }
    116             out.close();
    117             in.close();
    118116        } catch(MalformedURLException e) {
    119117            String msg = tr("Warning: Cannot download module ''{0}''. Its download link ''{1}'' is not a valid URL. Skipping download.", pi.name, pi.downloadlink);
     
    125123            throw new ModuleDownloadException(e);
    126124        } finally {
    127             Utils.close(in);
    128125            synchronized(this) {
    129126                downloadConnection = null;
    130127            }
    131             Utils.close(out);
    132128        }
    133129    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadLocalModuleInformationTask.java

    r30340 r30436  
    174174
    175175    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)) {
    179177            List<ModuleInformation> pis = new ModuleListParser().parse(fin);
    180178            for (ModuleInformation pi : pis) {
     
    187185        } catch(IOException e) {
    188186            throw new ModuleListParseException(e);
    189         } finally {
    190             Utils.close(fin);
    191187        }
    192188    }
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/modules/ReadRemoteModuleInformationTask.java

    r30340 r30436  
    3333import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
    3434import org.openstreetmap.josm.tools.ImageProvider;
    35 import org.openstreetmap.josm.tools.Utils;
    3635import org.xml.sax.SAXException;
    3736
     
    142141     */
    143142    protected String downloadModuleList(String site, ProgressMonitor monitor) {
    144         BufferedReader in = null;
    145143        StringBuilder sb = new StringBuilder();
    146144        try {
     
    159157                connection.setRequestProperty("Accept-Charset", "utf-8");
    160158            }
    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            }
    167166        } catch(MalformedURLException e) {
    168167            if (canceled) return null;
     
    180179                connection = null;
    181180            }
    182             Utils.close(in);
    183181            monitor.finishTask();
    184182        }
     
    192190     */
    193191    protected void downloadModuleIcons(String site, File destFile, ProgressMonitor monitor) {
    194         InputStream in = null;
    195         OutputStream out = null;
    196192        try {
    197193            site = site.replaceAll("%<(.*)>", "");
     
    207203                connection.setRequestProperty("Host", url.getHost());
    208204            }
    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            }
    217214        } catch(MalformedURLException e) {
    218215            if (canceled) return;
     
    230227                connection = null;
    231228            }
    232             Utils.close(in);
    233229            monitor.finishTask();
    234230        }
  • applications/editors/josm/plugins/openstreetbugs/src/org/openstreetmap/josm/plugins/osb/api/util/HttpUtils.java

    r29736 r30436  
    4242        int length = -1;
    4343        byte[] b = new byte[1024];
    44         InputStream in = Utils.openURL(new URL(url));
    45         while( (length = in.read(b)) > 0 ) {
    46             bos.write(b, 0, length);
     44        try (InputStream in = Utils.openURL(new URL(url))) {
     45            while( (length = in.read(b)) > 0 ) {
     46                bos.write(b, 0, length);
     47            }
    4748        }
    48         Utils.close(in);
    4949
    5050        return new String(bos.toByteArray(), charset);
     
    6666
    6767        //send the post
    68         OutputStream os = con.getOutputStream();
    69         os.write(content.getBytes("UTF-8"));
    70         os.flush();
     68        try (OutputStream os = con.getOutputStream()) {
     69            os.write(content.getBytes("UTF-8"));
     70            os.flush();
     71        }
    7172
    7273        // read the response
     
    7475        int length = -1;
    7576        byte[] b = new byte[1024];
    76         InputStream in = con.getInputStream();
    77         while( (length = in.read(b)) > 0 ) {
    78             bos.write(b, 0, length);
     77        try (InputStream in = con.getInputStream()) {
     78            while( (length = in.read(b)) > 0 ) {
     79                bos.write(b, 0, length);
     80            }
    7981        }
    80         Utils.close(in);
    81         Utils.close(os);
    8282
    8383        return new String(bos.toByteArray(), responseCharset);
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java

    r29531 r30436  
    5656import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationAction;
    5757import org.openstreetmap.josm.plugins.piclayer.transform.PictureTransform;
    58 import org.openstreetmap.josm.tools.Utils;
    5958
    6059/**
     
    457456
    458457    public void loadWorldfile(InputStream is) throws IOException {
    459         BufferedReader br = null;
    460         try {
     458       
     459        try (
    461460            Reader reader = new InputStreamReader(is);
    462             br = new BufferedReader(reader);
     461            BufferedReader br = new BufferedReader(reader)
     462        ) {
    463463            double e[] = new double[6];
    464464            for (int i=0; i<6; ++i) {
     
    488488            initialImageScale = 1;
    489489            Main.map.mapView.repaint();
    490         } finally {
    491             Utils.close(br);
    492490        }
    493491    }
  • applications/editors/josm/plugins/piclayer/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerFromFile.java

    r27120 r30436  
    2323import static org.openstreetmap.josm.tools.I18n.tr;
    2424
    25 import org.openstreetmap.josm.Main;
    26 
    2725import java.awt.Image;
    2826import java.io.File;
     
    3836import javax.swing.JOptionPane;
    3937
    40 import org.openstreetmap.josm.tools.Utils;
     38import org.openstreetmap.josm.Main;
    4139/**
    4240 * Layer displaying a picture loaded from a file.
     
    9694                if (imgEntry != null) {
    9795                    imgNameInZip = imgEntry.getName();
    98                     InputStream is = null;
    99                     try {
    100                         is = zipFile.getInputStream(imgEntry);
    101                         image = ImageIO.read(is);
    102                         return image;
    103                     } finally {
    104                         Utils.close(is);
     96                    try (InputStream is = zipFile.getInputStream(imgEntry)) {
     97                        return ImageIO.read(is);
    10598                    }
    10699                }
  • applications/editors/josm/plugins/reverter/src/reverter/OsmServerMultiObjectReader.java

    r29548 r30436  
    1212import org.openstreetmap.josm.io.OsmServerReader;
    1313import org.openstreetmap.josm.io.OsmTransferException;
    14 import org.openstreetmap.josm.tools.Utils;
    1514import org.xml.sax.SAXException;
    1615
     
    3029        sb.append(version);
    3130        progressMonitor.beginTask("", 1);
    32         InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
    33         try {
     31        try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
    3432            rdr.addData(in);
    3533        } catch (Exception e) {
     
    3735        } finally {
    3836            progressMonitor.finishTask();
    39             Utils.close(in);
    4037        }
    4138    }
  • applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmServerChangesetReader.java

    r30319 r30436  
    7070                return null;
    7171            monitor.indeterminateSubTask(tr("Downloading changesets ..."));
    72             List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
    73             return changesets;
     72            return OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
    7473        } catch(OsmTransferException e) {
    7574            throw e;
Note: See TracChangeset for help on using the changeset viewer.