Changeset 7037 in josm


Ignore:
Timestamp:
2014-05-01T16:28:25+02:00 (10 years ago)
Author:
Don-vip
Message:

see #8465 - last batch of try-with-resources

Location:
trunk/src/org/openstreetmap/josm
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java

    r7033 r7037  
    301301                            }
    302302                        }
    303                         Utils.close(reader);
    304303                    } catch (Exception e) {
    305304                        Main.error(e);
  • trunk/src/org/openstreetmap/josm/data/AutosaveTask.java

    r7033 r7037  
    304304                if (pidFile.exists()) {
    305305                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pidFile), Utils.UTF_8))) {
    306                         try {
    307                             String jvmId = reader.readLine();
    308                             if (jvmId != null) {
    309                                 String pid = jvmId.split("@")[0];
    310                                 skipFile = jvmPerfDataFileExists(pid);
    311                             }
    312                         } catch (Throwable t) {
    313                             Main.error(t);
    314                         } finally {
    315                             Utils.close(reader);
     306                        String jvmId = reader.readLine();
     307                        if (jvmId != null) {
     308                            String pid = jvmId.split("@")[0];
     309                            skipFile = jvmPerfDataFileExists(pid);
    316310                        }
    317311                    } catch (Throwable t) {
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r7005 r7037  
    7474                MirroredInputStream.cleanup(source);
    7575            }
    76             MirroredInputStream stream = null;
    7776            try {
    7877                ImageryReader reader = new ImageryReader(source);
     
    8079                defaultLayers.addAll(result);
    8180            } catch (IOException ex) {
    82                 Utils.close(stream);
    8381                Main.error(ex, false);
    8482                continue;
    8583            } catch (SAXException ex) {
    86                 Utils.close(stream);
    8784                Main.error(ex);
    8885                continue;
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2GridShiftFile.java

    r7025 r7037  
    155155        topLevelSubGrid = createSubGridTree(subGrid);
    156156        lastSubGrid = topLevelSubGrid[0];
    157 
    158         Utils.close(in);
    159157    }
    160158
  • trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java

    r6987 r7037  
    4545            throw new MissingHelpContentException(helpTopicUrl);
    4646        HttpURLConnection con = null;
    47         BufferedReader in = null;
    4847        try {
    4948            URL u = new URL(helpTopicUrl);
    5049            con = Utils.openHttpConnection(u);
    5150            con.connect();
    52             in = new BufferedReader(new InputStreamReader(con.getInputStream(), Utils.UTF_8));
    53             return prepareHelpContent(in, dotest, u);
     51            try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), Utils.UTF_8))) {
     52                return prepareHelpContent(in, dotest, u);
     53            }
    5454        } catch(MalformedURLException e) {
    5555            throw new HelpContentReaderException(e);
     
    6464            }
    6565            throw ex;
    66         } finally {
    67             Utils.close(in);
    6866        }
    6967    }
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r7005 r7037  
    77import java.io.DataOutputStream;
    88import java.io.IOException;
     9import java.io.InputStream;
    910import java.io.InputStreamReader;
    1011import java.io.UnsupportedEncodingException;
     
    197198
    198199    protected String extractToken(HttpURLConnection connection) {
    199         BufferedReader r = null;
    200         try {
    201             r = new BufferedReader(new InputStreamReader(connection.getInputStream(), Utils.UTF_8));
     200        try (
     201            InputStream is = connection.getInputStream();
     202            BufferedReader r = new BufferedReader(new InputStreamReader(is, Utils.UTF_8))
     203        ) {
    202204            String c;
    203205            Pattern p = Pattern.compile(".*authenticity_token.*value=\"([^\"]+)\".*");
     
    211213            Main.error(e);
    212214            return null;
    213         } finally {
    214             Utils.close(r);
    215215        }
    216216        return null;
     
    364364
    365365    protected void authenticateOsmSession(SessionId sessionId, String userName, String password) throws OsmLoginFailedException {
    366         DataOutputStream dout = null;
    367366        try {
    368367            URL url = new URL(buildOsmLoginUrl());
     
    392391            connection.connect();
    393392
    394             dout = new DataOutputStream(connection.getOutputStream());
    395             dout.writeBytes(request);
    396             dout.flush();
    397             Utils.close(dout);
     393            try (DataOutputStream dout = new DataOutputStream(connection.getOutputStream())) {
     394                dout.writeBytes(request);
     395                dout.flush();
     396            }
    398397
    399398            // after a successful login the OSM website sends a redirect to a follow up page. Everything
     
    409408            throw new OsmLoginFailedException(e);
    410409        } finally {
    411             Utils.close(dout);
    412410            synchronized(this) {
    413411                connection = null;
     
    456454            parameters.put("allow_read_prefs", "yes");
    457455        }
    458         if(privileges.isAllowModifyNotes()) {
     456        if (privileges.isAllowModifyNotes()) {
    459457            parameters.put("allow_write_notes", "yes");
    460458        }
     
    463461
    464462        String request = buildPostRequest(parameters);
    465         DataOutputStream dout = null;
    466463        try {
    467464            URL url = new URL(oauthProviderParameters.getAuthoriseUrl());
     
    480477            connection.connect();
    481478
    482             dout = new DataOutputStream(connection.getOutputStream());
    483             dout.writeBytes(request);
    484             dout.flush();
     479            try (DataOutputStream dout = new DataOutputStream(connection.getOutputStream())) {
     480                dout.writeBytes(request);
     481                dout.flush();
     482            }
    485483
    486484            int retCode = connection.getResponseCode();
    487485            if (retCode != HttpURLConnection.HTTP_OK)
    488486                throw new OsmOAuthAuthorizationException(tr("Failed to authorize OAuth request  ''{0}''", requestToken.getKey()));
    489         } catch(IOException e) {
    490             throw new OsmOAuthAuthorizationException(e);
    491         } finally {
    492             Utils.close(dout);
     487        } catch (IOException e) {
     488            throw new OsmOAuthAuthorizationException(e);
     489        } finally {
    493490            synchronized(this) {
    494491                connection = null;
  • trunk/src/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTask.java

    r6995 r7037  
    165165    @Override
    166166    protected void realRun() throws SAXException, IOException, OsmTransferException {
    167         BufferedReader bin = null;
    168167        try {
    169168            try {
     
    195194            }
    196195            StringBuilder changesets = new StringBuilder();
    197             bin = new BufferedReader(new InputStreamReader(connection.getInputStream(), Utils.UTF_8));
    198             String line;
    199             while ((line = bin.readLine()) != null) {
    200                 changesets.append(line).append("\n");
     196            try (BufferedReader bin = new BufferedReader(new InputStreamReader(connection.getInputStream(), Utils.UTF_8))) {
     197                String line;
     198                while ((line = bin.readLine()) != null) {
     199                    changesets.append(line).append("\n");
     200                }
    201201            }
    202202            if (! (changesets.toString().contains("<osm") && changesets.toString().contains("</osm>"))) {
     
    215215            alertConnectionFailed();
    216216            return;
    217         } finally {
    218             Utils.close(bin);
    219217        }
    220218    }
  • trunk/src/org/openstreetmap/josm/io/GpxExporter.java

    r7005 r7037  
    3636import org.openstreetmap.josm.tools.CheckParameterUtil;
    3737import org.openstreetmap.josm.tools.GBC;
    38 import org.openstreetmap.josm.tools.Utils;
    3938
    4039public class GpxExporter extends FileExporter implements GpxConstants {
     
    176175        }
    177176
    178         OutputStream fo = null;
    179         try {
    180             fo = Compression.getCompressedFileOutputStream(file);
     177       
     178        try (OutputStream fo = Compression.getCompressedFileOutputStream(file)) {
    181179            new GpxWriter(fo).write(gpxData);
    182180            fo.flush();
     
    185183            JOptionPane.showMessageDialog(Main.parent, tr("Error while exporting {0}:\n{1}", fn, x.getMessage()),
    186184                    tr("Error"), JOptionPane.ERROR_MESSAGE);
    187         } finally {
    188             Utils.close(fo);
    189185        }
    190186    }
  • trunk/src/org/openstreetmap/josm/io/NmeaReader.java

    r7012 r7037  
    174174        Collection<Collection<WayPoint>> currentTrack = new ArrayList<>();
    175175
    176         BufferedReader rd = null;
    177         try {
    178             rd = new BufferedReader(new InputStreamReader(source, Utils.UTF_8));
    179 
     176        try (BufferedReader rd = new BufferedReader(new InputStreamReader(source, Utils.UTF_8))) {
    180177            StringBuilder sb = new StringBuilder(1024);
    181178            int loopstart_char = rd.read();
     
    209206        } catch (Exception e) {
    210207            Main.warn(e);
    211         } finally {
    212             Utils.close(rd);
    213         }
    214     }
     208        }
     209    }
     210
    215211    private static class NMEAParserState {
    216212        protected Collection<WayPoint> waypoints = new ArrayList<>();
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r7033 r7037  
    629629                    activeConnection.setDoOutput(true);
    630630                    activeConnection.setRequestProperty("Content-type", "text/xml");
    631                     OutputStream out = activeConnection.getOutputStream();
    632 
    633                     // It seems that certain bits of the Ruby API are very unhappy upon
    634                     // receipt of a PUT/POST message without a Content-length header,
    635                     // even if the request has no payload.
    636                     // Since Java will not generate a Content-length header unless
    637                     // we use the output stream, we create an output stream for PUT/POST
    638                     // even if there is no payload.
    639                     if (requestBody != null) {
    640                         try (BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, Utils.UTF_8))) {
    641                             bwr.write(requestBody);
    642                             bwr.flush();
     631                    try (OutputStream out = activeConnection.getOutputStream()) {
     632                        // It seems that certain bits of the Ruby API are very unhappy upon
     633                        // receipt of a PUT/POST message without a Content-length header,
     634                        // even if the request has no payload.
     635                        // Since Java will not generate a Content-length header unless
     636                        // we use the output stream, we create an output stream for PUT/POST
     637                        // even if there is no payload.
     638                        if (requestBody != null) {
     639                            try (BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, Utils.UTF_8))) {
     640                                bwr.write(requestBody);
     641                                bwr.flush();
     642                            }
    643643                        }
    644644                    }
    645                     Utils.close(out);
    646645                }
    647646
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r6798 r7037  
    6262    @Override
    6363    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    64         InputStream in = null;
    65         try {
    66             in = Compression.getUncompressedFileInputStream(file);
     64        try (InputStream in = Compression.getUncompressedFileInputStream(file)) {
    6765            importData(in, file, progressMonitor);
    6866        } catch (FileNotFoundException e) {
    6967            Main.error(e);
    7068            throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e);
    71         } finally {
    72             Utils.close(in);
    7369        }
    7470    }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/RemoteControlHttpsServer.java

    r7034 r7037  
    2929
    3030import org.openstreetmap.josm.Main;
    31 import org.openstreetmap.josm.tools.Utils;
    3231
    3332/**
     
    6059                        Main.error(tr("Unable to find JOSM keystore at {0}. Remote control will not be available on HTTPS.", KEYSTORE_PATH));
    6160                    } else {
    62                         try {
    63                             ks.load(in, password);
    64                         } finally {
    65                             Utils.close(in);
    66                         }
     61                        ks.load(in, password);
    6762                       
    6863                        if (Main.isDebugEnabled()) {
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r7033 r7037  
    153153     */
    154154    protected String downloadPluginList(String site, final ProgressMonitor monitor) {
    155         BufferedReader in = null;
    156 
    157155        /* replace %<x> with empty string or x=plugins (separated with comma) */
    158156        String pl = Utils.join(",", Main.pref.getCollection("plugins"));
     
    174172                connection.setRequestProperty("Accept-Charset", "utf-8");
    175173            }
    176             in = new BufferedReader(new InputStreamReader(connection.getInputStream(), Utils.UTF_8));
    177             StringBuilder sb = new StringBuilder();
    178             String line;
    179             while ((line = in.readLine()) != null) {
    180                 sb.append(line).append("\n");
    181             }
    182             return sb.toString();
     174            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), Utils.UTF_8))) {
     175                StringBuilder sb = new StringBuilder();
     176                String line;
     177                while ((line = in.readLine()) != null) {
     178                    sb.append(line).append("\n");
     179                }
     180                return sb.toString();
     181            }
    183182        } catch (MalformedURLException e) {
    184183            if (canceled) return null;
     
    197196                connection = null;
    198197            }
    199             Utils.close(in);
    200198            monitor.finishTask();
    201199        }
     
    206204        try (InputStream errStream = connection.getErrorStream()) {
    207205            if (errStream != null) {
    208                 BufferedReader err = null;
    209                 try {
     206                try (BufferedReader err = new BufferedReader(new InputStreamReader(errStream, Utils.UTF_8))) {
    210207                    String line;
    211                     err = new BufferedReader(new InputStreamReader(errStream, Utils.UTF_8));
    212208                    while ((line = err.readLine()) != null) {
    213209                        sb.append(line).append("\n");
     
    216212                    Main.error(e);
    217213                    Main.error(ex);
    218                 } finally {
    219                     Utils.close(err);
    220214                }
    221215            }
  • trunk/src/org/openstreetmap/josm/tools/AudioUtil.java

    r7033 r7037  
    3838                * audioFormat.getFrameSize() /* bytes per frame */;
    3939            double naturalLength = filesize / bytesPerSecond;
    40             Utils.close(audioInputStream);
    4140            double calibration = Main.pref.getDouble("audio.calibration", 1.0 /* default, ratio */);
    4241            return naturalLength / calibration;
  • trunk/src/org/openstreetmap/josm/tools/BugReportExceptionHandler.java

    r6925 r7037  
    211211     */
    212212    public static URL getBugReportUrl(String debugText) {
    213         try {
     213        try (
    214214            ByteArrayOutputStream out = new ByteArrayOutputStream();
    215             GZIPOutputStream gzip = new GZIPOutputStream(out);
     215            GZIPOutputStream gzip = new GZIPOutputStream(out)
     216        ) {
    216217            gzip.write(debugText.getBytes(Utils.UTF_8));
    217             Utils.close(gzip);
    218218
    219219            return new URL(Main.getJOSMWebsite()+"/josmticket?" +
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r7033 r7037  
    200200                // Try lsb_release (only available on LSB-compliant Linux systems, see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod )
    201201                Process p = Runtime.getRuntime().exec("lsb_release -ds");
    202                 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), Utils.UTF_8));
    203                 String line = Utils.strip(input.readLine());
    204                 Utils.close(input);
    205                 if (line != null && !line.isEmpty()) {
    206                     line = line.replaceAll("\"+","");
    207                     line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
    208                     if(line.startsWith("Linux ")) // e.g. Linux Mint
    209                         return line;
    210                     else if(!line.isEmpty())
    211                         return "Linux " + line;
     202                try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), Utils.UTF_8))) {
     203                    String line = Utils.strip(input.readLine());
     204                    if (line != null && !line.isEmpty()) {
     205                        line = line.replaceAll("\"+","");
     206                        line = line.replaceAll("NAME=",""); // strange code for some Gentoo's
     207                        if(line.startsWith("Linux ")) // e.g. Linux Mint
     208                            return line;
     209                        else if(!line.isEmpty())
     210                            return "Linux " + line;
     211                    }
    212212                }
    213213            } catch (IOException e) {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r7019 r7037  
    801801        }
    802802        Process p = new ProcessBuilder(command).start();
    803         BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), UTF_8));
    804         StringBuilder all = null;
    805         String line;
    806         while ((line = input.readLine()) != null) {
    807             if (all == null) {
    808                 all = new StringBuilder(line);
    809             } else {
    810                 all.append("\n");
    811                 all.append(line);
    812             }
    813         }
    814         Utils.close(input);
    815         return all != null ? all.toString() : null;
     803        try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream(), UTF_8))) {
     804            StringBuilder all = null;
     805            String line;
     806            while ((line = input.readLine()) != null) {
     807                if (all == null) {
     808                    all = new StringBuilder(line);
     809                } else {
     810                    all.append("\n");
     811                    all.append(line);
     812                }
     813            }
     814            return all != null ? all.toString() : null;
     815        }
    816816    }
    817817
Note: See TracChangeset for help on using the changeset viewer.