Ignore:
Timestamp:
2014-10-19T01:27:04+02:00 (10 years ago)
Author:
donvip
Message:

[josm_plugins] fix java 7 warnings / global usage of try-with-resource

Location:
applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CacheControl.java

    r30737 r30738  
    11// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
    22package cadastre_fr;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
     5
     6import java.io.BufferedOutputStream;
     7import java.io.File;
     8import java.io.FileInputStream;
     9import java.io.FileOutputStream;
     10import java.io.IOException;
     11import java.io.ObjectInputStream;
     12import java.io.ObjectOutputStream;
     13import java.io.OutputStream;
     14import java.util.ArrayList;
     15import java.util.concurrent.locks.Lock;
     16import java.util.concurrent.locks.ReentrantLock;
     17
     18import javax.swing.JDialog;
     19import javax.swing.JOptionPane;
     20
     21import org.openstreetmap.josm.Main;
     22
    323/**
    424 * This class handles the WMS layer cache mechanism. The design is oriented for a good performance (no
     
    1030 * is inserted before each append and an exception is raised at objects read).
    1131 */
    12 
    13 import static org.openstreetmap.josm.tools.I18n.tr;
    14 
    15 import java.io.*;
    16 import java.util.ArrayList;
    17 import java.util.concurrent.locks.Lock;
    18 import java.util.concurrent.locks.ReentrantLock;
    19 
    20 import javax.swing.JDialog;
    21 import javax.swing.JOptionPane;
    22 import org.openstreetmap.josm.Main;
    23 
    2432public class CacheControl implements Runnable {
    2533
     
    3240            super(out);
    3341        }
     42        @Override
    3443        protected void writeStreamHeader() throws IOException {
    3544            reset();
     
    4049
    4150    public static int cacheSize = 500;
    42 
    4351
    4452    public WMSLayer wmsLayer = null;
     
    141149    public boolean loadCache(File file, int currentLambertZone) {
    142150        boolean successfulRead = false;
    143         FileInputStream fis = null;
    144         ObjectInputStream ois = null;
    145         try {
    146             fis = new FileInputStream(file);
    147             ois = new ObjectInputStream(fis);
     151        try (
     152            FileInputStream fis = new FileInputStream(file);
     153            ObjectInputStream ois = new ObjectInputStream(fis);
     154        ) {
    148155            successfulRead = wmsLayer.read(file, ois, currentLambertZone);
    149156        } catch (Exception ex) {
     
    151158            JOptionPane.showMessageDialog(Main.parent, tr("Error loading file.\nProbably an old version of the cache file."), tr("Error"), JOptionPane.ERROR_MESSAGE);
    152159            return false;
    153         } finally {
    154             try {
    155                 ois.close();
    156                 fis.close();
    157             } catch (Exception e) {
    158                 e.printStackTrace();
    159             }
    160160        }
    161161        if (successfulRead && wmsLayer.isRaster()) {
     
    165165        return successfulRead;
    166166    }
    167 
    168167
    169168    public synchronized void saveCache(GeorefImage image) {
     
    177176     * Thread saving the grabbed images in background.
    178177     */
     178    @Override
    179179    public synchronized void run() {
    180180        for (;;) {
     
    186186                try {
    187187                    if (file.exists()) {
    188                         ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
    189                                 new BufferedOutputStream(new FileOutputStream(file, true)));
    190                         for (int i=0; i < size; i++) {
    191                             oos.writeObject(imagesToSave.get(i));
     188                        try (ObjectOutputStreamAppend oos = new ObjectOutputStreamAppend(
     189                                new BufferedOutputStream(new FileOutputStream(file, true)))) {
     190                            for (int i=0; i < size; i++) {
     191                                oos.writeObject(imagesToSave.get(i));
     192                            }
    192193                        }
    193                         oos.close();
    194194                    } else {
    195                         ObjectOutputStream oos = new ObjectOutputStream(
    196                                 new BufferedOutputStream(new FileOutputStream(file)));
    197                         wmsLayer.write(file, oos);
    198                         for (int i=0; i < size; i++) {
    199                             oos.writeObject(imagesToSave.get(i));
     195                        try (ObjectOutputStream oos = new ObjectOutputStream(
     196                                new BufferedOutputStream(new FileOutputStream(file)))) {
     197                            wmsLayer.write(file, oos);
     198                            for (int i=0; i < size; i++) {
     199                                oos.writeObject(imagesToSave.get(i));
     200                            }
    200201                        }
    201                         oos.close();
    202202                    }
    203203                } catch (IOException e) {
    204                     e.printStackTrace(System.out);
     204                    Main.error(e);
    205205                }
    206206                imagesLock.lock();
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreGrabber.java

    r28961 r30738  
    2222    private CadastreInterface wmsInterface = new CadastreInterface();
    2323
    24     public GeorefImage grab(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax) throws IOException, OsmTransferException {
    25 
     24    public GeorefImage grab(WMSLayer wmsLayer, EastNorth lambertMin, EastNorth lambertMax)
     25            throws IOException, OsmTransferException {
    2626        try {
    2727            URL url = null;
     
    8888        wmsInterface.urlConn.setRequestMethod("GET");
    8989        wmsInterface.setCookie();
    90         InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
    91         BufferedImage img = ImageIO.read(is);
    92         is.close();
    93         return img;
     90        try (InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE)) {
     91            return ImageIO.read(is);
     92        }
    9493    }
    9594
     
    9796        return wmsInterface;
    9897    }
    99 
    10098}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreInterface.java

    r30737 r30738  
    229229            }
    230230            if (Main.isDebugEnabled()) {
    231                 Main.debug(lines);
     231                Main.debug(lines);
    232232            }
    233233        } catch (MalformedURLException e) {
     
    284284            urlConn.setDoInput(true);
    285285            setCookie();
    286             OutputStream wr = urlConn.getOutputStream();
    287             wr.write(content.getBytes());
    288             System.out.println("POST "+content);
    289             wr.flush();
    290             wr.close();
    291             BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    292             while ((ln = rd.readLine()) != null) {
    293                 lines += ln;
    294             }
    295             rd.close();
     286            try (OutputStream wr = urlConn.getOutputStream()) {
     287                wr.write(content.getBytes());
     288                Main.info("POST "+content);
     289                wr.flush();
     290            }
     291            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream()))) {
     292                while ((ln = rd.readLine()) != null) {
     293                    lines += ln;
     294                }
     295            }
    296296            urlConn.disconnect();
    297297            if (lines != null) {
     
    371371            setCookie(urlConn2);
    372372            urlConn2.connect();
    373             System.out.println("GET "+getAllImagesURL);
    374             BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()));
    375             while ((ln = rd.readLine()) != null) {
    376                 lines += ln;
    377             }
    378             rd.close();
     373            Main.info("GET "+getAllImagesURL);
     374            try (BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn2.getInputStream()))) {
     375                while ((ln = rd.readLine()) != null) {
     376                    lines += ln;
     377                }
     378            }
    379379            urlConn2.disconnect();
    380             //System.out.println("GET="+lines);
    381380        } catch (IOException e) {
    382381            listOfFeuilles.clear();
    383             e.printStackTrace();
     382            Main.error(e);
    384383        }
    385384        return lines;
     
    483482        }
    484483        System.out.println("GET "+searchFormURL);
    485         BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
    486         while ((ln = in.readLine()) != null) {
    487             line += ln;
    488         }
    489         in.close();
     484        try (BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()))) {
     485            while ((ln = in.readLine()) != null) {
     486                line += ln;
     487            }
     488        }
    490489        urlConn.disconnect();
    491490        parseBBoxCommune(wmsLayer, line);
     
    511510
    512511    private void parseGeoreferences(WMSLayer wmsLayer, String input) {
    513         /* commented since cadastre WMS changes mid july 2013 
     512        /* commented since cadastre WMS changes mid july 2013
    514513         * until new GeoBox coordinates parsing is solved */
    515514//        if (input.lastIndexOf(cBBoxCommunStart) != -1) {
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreSessionExporter.java

    r29922 r30738  
    3030    }
    3131
    32         @Override
    33         public Collection<Layer> getDependencies() {
     32    @Override
     33    public Collection<Layer> getDependencies() {
    3434        return Collections.emptySet();
    35         }
     35    }
    3636
    37         @Override
    38         public Component getExportPanel() {
     37    @Override
     38    public Component getExportPanel() {
    3939        final JPanel p = new JPanel(new GridBagLayout());
    4040        export = new JCheckBox();
     
    4646        p.add(GBC.glue(1,0), GBC.std().fill(GBC.HORIZONTAL));
    4747        return p;
    48         }
     48    }
    4949
    50         @Override
    51         public boolean shallExport() {
     50    @Override
     51    public boolean shallExport() {
    5252        return export.isSelected();
    53         }
     53    }
    5454
    55         @Override
    56         public boolean requiresZip() {
    57                 return false;
    58         }
     55    @Override
     56    public boolean requiresZip() {
     57        return false;
     58    }
    5959
    60         @Override
    61         public Element export(ExportSupport support) throws IOException {
     60    @Override
     61    public Element export(ExportSupport support) throws IOException {
    6262        Element layerEl = support.createElement("layer");
    6363        layerEl.setAttribute("type", "cadastre-fr");
     
    7676        file.appendChild(support.createTextNode(url.toString()));
    7777        return layerEl;
    78         }
     78    }
    7979
    8080}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/CadastreSessionImporter.java

    r30701 r30738  
    2222public class CadastreSessionImporter implements SessionLayerImporter{
    2323
    24         @Override
    25         public Layer load(Element elem, ImportSupport support,
    26                         ProgressMonitor progressMonitor) throws IOException,
    27                         IllegalDataException {
     24    @Override
     25    public Layer load(Element elem, ImportSupport support,
     26            ProgressMonitor progressMonitor) throws IOException,
     27            IllegalDataException {
    2828        String version = elem.getAttribute("version");
    2929        if (!"0.1".equals(version)) {
     
    4141            fileStr = URLDecoder.decode(fileStr, "UTF-8");
    4242            fileStr = fileStr.substring(fileStr.indexOf(":/")+2);
    43                 String filename = fileStr.substring(fileStr.lastIndexOf("/")+1,fileStr.length());
     43            String filename = fileStr.substring(fileStr.lastIndexOf("/")+1,fileStr.length());
    4444            String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());
    4545            // create layer and load cache
     
    6060            throw new RuntimeException(e);
    6161        }
    62         }
     62    }
    6363
    6464}
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/DownloadSVGBuilding.java

    r30737 r30738  
    4343    private static String errorMessage;
    4444
    45 
     45    /**
     46     * Constructs a new {@code DownloadSVGBuilding}.
     47     */
    4648    public DownloadSVGBuilding(WMSLayer wmsLayer) {
    4749        super(tr("Downloading {0}", wmsLayer.getName()));
     
    240242        wmsInterface.urlConn.setRequestMethod("GET");
    241243        wmsInterface.setCookie();
    242         InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
    243244        File file = new File(CadastrePlugin.cacheDir + "building.svg");
    244245        String svg = new String();
    245         try {
     246        try (InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE)) {
    246247            if (file.exists())
    247248                file.delete();
    248             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
    249             InputStreamReader isr =new InputStreamReader(is);
    250             BufferedReader br = new BufferedReader(isr);
    251             String line="";
    252             while ( null!=(line=br.readLine())){
    253                 line += "\n";
    254                 bos.write(line.getBytes());
    255                 svg += line;
    256             }
    257             br.close();
    258             bos.close();
     249            try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
     250                 InputStreamReader isr = new InputStreamReader(is);
     251                 BufferedReader br = new BufferedReader(isr)) {
     252                String line="";
     253                while ( null!=(line=br.readLine())){
     254                    line += "\n";
     255                    bos.write(line.getBytes());
     256                    svg += line;
     257                }
     258            }
    259259        } catch (IOException e) {
    260             e.printStackTrace(System.out);
    261         }
    262         is.close();
     260            Main.error(e);
     261        }
    263262        return svg;
    264263    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/DownloadSVGTask.java

    r30737 r30738  
    4747    private static String errorMessage;
    4848
     49    /**
     50     * Constructs a new {@code DownloadSVGTask}.
     51     */
    4952    public DownloadSVGTask(WMSLayer wmsLayer) {
    5053        super(tr("Downloading {0}", wmsLayer.getName()));
     
    194197        wmsInterface.urlConn.setRequestMethod("GET");
    195198        wmsInterface.setCookie();
    196         InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE);
    197199        File file = new File(CadastrePlugin.cacheDir + "boundary.svg");
    198200        String svg = new String();
    199         try {
     201        try (InputStream is = new ProgressInputStream(wmsInterface.urlConn, NullProgressMonitor.INSTANCE)) {
    200202            if (file.exists())
    201203                file.delete();
    202             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
    203             InputStreamReader isr =new InputStreamReader(is);
    204             BufferedReader br = new BufferedReader(isr);
    205             String line="";
    206             while ( null!=(line=br.readLine())){
    207                 line += "\n";
    208                 bos.write(line.getBytes());
    209                 svg += line;
     204            try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, true));
     205                 InputStreamReader isr =new InputStreamReader(is);
     206                 BufferedReader br = new BufferedReader(isr)) {
     207                String line="";
     208                while ( null!=(line=br.readLine())){
     209                    line += "\n";
     210                    bos.write(line.getBytes());
     211                    svg += line;
     212                }
    210213            }
    211             br.close();
    212             bos.close();
    213214        } catch (IOException e) {
    214             e.printStackTrace(System.out);
    215         }
    216         is.close();
     215            Main.error(e);
     216        }
    217217        return svg;
    218218    }
  • applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSLayer.java

    r30737 r30738  
    133133        // if the layer is currently saving the images in the cache, wait until it's finished
    134134        if(grabThread != null)
    135                         grabThread.cancel();
     135                grabThread.cancel();
    136136        grabThread = null;
    137137        super.destroy();
Note: See TracChangeset for help on using the changeset viewer.