Changeset 6268 in josm for trunk/src/org


Ignore:
Timestamp:
2013-09-27T19:50:29+02:00 (11 years ago)
Author:
Don-vip
Message:

Sonar/FindBugs - Bad practice - Method may fail to close stream

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/ServerSidePreferences.java

    r6248 r6268  
    6464                BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
    6565                StringBuilder b = new StringBuilder();
    66                 for (String line = reader.readLine(); line != null; line = reader.readLine()) {
    67                     b.append(line);
    68                     b.append("\n");
     66                try {
     67                    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
     68                        b.append(line);
     69                        b.append("\n");
     70                    }
     71                } finally {
     72                    reader.close();
    6973                }
    7074                if (con instanceof HttpURLConnection) {
     
    7377                return b.toString();
    7478            } catch (IOException e) {
     79                Main.error(e);
    7580                e.printStackTrace();
    7681            }
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r6248 r6268  
    3333import org.openstreetmap.josm.io.MirroredInputStream;
    3434import org.openstreetmap.josm.tools.Pair;
     35import org.openstreetmap.josm.tools.Utils;
    3536
    3637/**
     
    125126    private static void loadInits() {
    126127        Pattern epsgPattern = Pattern.compile("<(\\d+)>(.*)<>");
     128        BufferedReader r = null;
    127129        try {
    128130            InputStream in = new MirroredInputStream("resource://data/epsg");
    129             BufferedReader r = new BufferedReader(new InputStreamReader(in));
     131            r = new BufferedReader(new InputStreamReader(in));
    130132            String line, lastline = "";
    131133            while ((line = r.readLine()) != null) {
     
    144146            }
    145147        } catch (IOException ex) {
    146             throw new RuntimeException();
     148            throw new RuntimeException(ex);
     149        } finally {
     150            Utils.close(r);
    147151        }
    148152    }
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r6246 r6268  
    153153        ignoredErrors.clear();
    154154        if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
     155            BufferedReader in = null;
    155156            try {
    156                 final BufferedReader in = new BufferedReader(new FileReader(getValidatorDir() + "ignorederrors"));
     157                in = new BufferedReader(new FileReader(getValidatorDir() + "ignorederrors"));
    157158                for (String line = in.readLine(); line != null; line = in.readLine()) {
    158159                    ignoredErrors.add(line);
     
    162163            } catch (final IOException e) {
    163164                e.printStackTrace();
     165            } finally {
     166                Utils.close(in);
    164167            }
    165168        }
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r6265 r6268  
    6262import org.openstreetmap.josm.tools.GBC;
    6363import org.openstreetmap.josm.tools.MultiMap;
     64import org.openstreetmap.josm.tools.Utils;
    6465
    6566/**
     
    205206            return;
    206207        for (String source : sources.split(";")) {
     208            BufferedReader reader = null;
    207209            try {
    208210                MirroredInputStream s = new MirroredInputStream(source);
     
    213215                    r = new InputStreamReader(s);
    214216                }
    215                 BufferedReader reader = new BufferedReader(r);
     217                reader = new BufferedReader(r);
    216218
    217219                String okValue = null;
     
    272274            } catch (IOException e) {
    273275                errorSources += source + "\n";
     276            } finally {
     277                Utils.close(reader);
    274278            }
    275279        }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r6248 r6268  
    674674                    txtSource.append(line + "\n");
    675675                }
     676                reader.close();
    676677            } catch (IOException ex) {
    677678                txtSource.append("<ERROR: failed to read file!>");
  • trunk/src/org/openstreetmap/josm/gui/oauth/OsmOAuthAuthorizationClient.java

    r6248 r6268  
    212212
    213213    protected String extractToken(HttpURLConnection connection) {
    214         try {
    215             BufferedReader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     214        BufferedReader r = null;
     215        try {
     216            r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    216217            String c;
    217218            Pattern p = Pattern.compile(".*authenticity_token.*value=\"([^\"]+)\".*");
    218             while((c = r.readLine()) != null) {
     219            while ((c = r.readLine()) != null) {
    219220                Matcher m = p.matcher(c);
    220                 if(m.find())
     221                if (m.find()) {
    221222                    return m.group(1);
     223                }
    222224            }
    223225        } catch (IOException e) {
     226            Main.error(e);
    224227            return null;
     228        } finally {
     229            Utils.close(r);
    225230        }
    226231        return null;
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r6248 r6268  
    621621                    if (requestBody != null) {
    622622                        BufferedWriter bwr = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
    623                         bwr.write(requestBody);
    624                         bwr.flush();
     623                        try {
     624                            bwr.write(requestBody);
     625                            bwr.flush();
     626                        } finally {
     627                            bwr.close();
     628                        }
    625629                    }
    626630                    Utils.close(out);
     
    657661                    BufferedReader in = new BufferedReader(new InputStreamReader(i));
    658662                    String s;
    659                     while((s = in.readLine()) != null) {
    660                         responseBody.append(s);
    661                         responseBody.append("\n");
     663                    try {
     664                        while((s = in.readLine()) != null) {
     665                            responseBody.append(s);
     666                            responseBody.append("\n");
     667                        }
     668                    } finally {
     669                        in.close();
    662670                    }
    663671                }
  • trunk/src/org/openstreetmap/josm/io/imagery/WMSImagery.java

    r6248 r6268  
    134134        String line;
    135135        StringBuilder ba = new StringBuilder();
    136         while ((line = br.readLine()) != null) {
    137             ba.append(line);
    138             ba.append("\n");
     136        try {
     137            while ((line = br.readLine()) != null) {
     138                ba.append(line);
     139                ba.append("\n");
     140            }
     141        } finally {
     142            br.close();
    139143        }
    140144        String incomingData = ba.toString();
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r6245 r6268  
    328328        try {
    329329            c.close();
    330         } catch(IOException e) {
    331             // ignore
     330        } catch (IOException e) {
     331            Main.warn(e);
    332332        }
    333333    }
     
    342342        try {
    343343            zip.close();
    344         } catch(IOException e) {
    345             // ignore
    346         }
    347     }
    348 
    349     private final static double EPSILION = 1e-11;
    350 
     344        } catch (IOException e) {
     345            Main.warn(e);
     346        }
     347    }
     348
     349    private final static double EPSILON = 1e-11;
     350
     351    /**
     352     * Determines if the two given double values are equal (their delta being smaller than a fixed epsilon)
     353     * @param a The first double value to compare
     354     * @param b The second double value to compare
     355     * @return {@code true} if {@code abs(a - b) <= 1e-11}, {@code false} otherwise
     356     */
    351357    public static boolean equalsEpsilon(double a, double b) {
    352         return Math.abs(a - b) <= EPSILION;
     358        return Math.abs(a - b) <= EPSILON;
    353359    }
    354360
Note: See TracChangeset for help on using the changeset viewer.