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

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

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/AudioUtil.java

    r7029 r7033  
    3131     */
    3232    public static double getCalibratedDuration(File wavFile) {
    33         try {
    34             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
    35                 new URL("file:".concat(wavFile.getAbsolutePath())));
     33        try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(
     34                new URL("file:".concat(wavFile.getAbsolutePath())))) {
    3635            AudioFormat audioFormat = audioInputStream.getFormat();
    3736            long filesize = wavFile.length();
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r7012 r7033  
    397397        if ("en".equals(loadedCode))
    398398            return;
    399         FileInputStream fis = null;
    400         JarInputStream jar = null;
    401         FileInputStream fisTrans = null;
    402         JarInputStream jarTrans = null;
    403399        String enfile = "data/en.lang";
    404400        String langfile = "data/"+loadedCode+".lang";
    405         try
    406         {
     401        try (
     402            FileInputStream fis = new FileInputStream(source);
     403            JarInputStream jar = new JarInputStream(fis)
     404        ) {
    407405            ZipEntry e;
    408             fis = new FileInputStream(source);
    409             jar = new JarInputStream(fis);
    410406            boolean found = false;
    411             while(!found && (e = jar.getNextEntry()) != null)
    412             {
     407            while (!found && (e = jar.getNextEntry()) != null) {
    413408                String name = e.getName();
    414409                if(name.equals(enfile))
    415410                    found = true;
    416411            }
    417             if(found)
    418             {
    419                 fisTrans = new FileInputStream(source);
    420                 jarTrans = new JarInputStream(fisTrans);
    421                 found = false;
    422                 while(!found && (e = jarTrans.getNextEntry()) != null)
    423                 {
    424                     String name = e.getName();
    425                     if(name.equals(langfile))
    426                         found = true;
     412            if (found) {
     413                try (
     414                    FileInputStream fisTrans = new FileInputStream(source);
     415                    JarInputStream jarTrans = new JarInputStream(fisTrans)
     416                ) {
     417                    found = false;
     418                    while(!found && (e = jarTrans.getNextEntry()) != null) {
     419                        String name = e.getName();
     420                        if (name.equals(langfile))
     421                            found = true;
     422                    }
     423                    if (found)
     424                        load(jar, jarTrans, true);
    427425                }
    428                 if(found)
    429                     load(jar, jarTrans, true);
    430             }
    431         } catch(IOException e) {
     426            }
     427        } catch (IOException e) {
    432428            // Ignore
    433         } finally {
    434             Utils.close(jar);
    435             Utils.close(fis);
    436             Utils.close(jarTrans);
    437             Utils.close(fisTrans);
    438429        }
    439430    }
     
    460451                return false;
    461452        }
    462         InputStream enStream = null;
    463         InputStream trStream = null;
    464         try {
    465             enStream = en.openStream();
    466             trStream = tr.openStream();
     453        try (
     454            InputStream enStream = en.openStream();
     455            InputStream trStream = tr.openStream()
     456        ) {
    467457            if (load(enStream, trStream, false)) {
    468458                pluralMode = languages.get(l);
     
    470460                return true;
    471461            }
    472         } catch(IOException e) {
     462        } catch (IOException e) {
    473463            // Ignore exception
    474         } finally {
    475             Utils.close(trStream);
    476             Utils.close(enStream);
    477464        }
    478465        return false;
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r7025 r7033  
    504504
    505505    private static ImageResource getIfAvailableHttp(String url, ImageType type) {
    506         MirroredInputStream is = null;
    507         try {
    508             is = new MirroredInputStream(url,
    509                     new File(Main.pref.getCacheDirectory(), "images").getPath());
     506        try (MirroredInputStream is = new MirroredInputStream(url,
     507                    new File(Main.pref.getCacheDirectory(), "images").getPath())) {
    510508            switch (type) {
    511509            case SVG:
     
    526524        } catch (IOException e) {
    527525            return null;
    528         } finally {
    529             Utils.close(is);
    530526        }
    531527    }
     
    603599
    604600    private static ImageResource getIfAvailableZip(String fullName, File archive, String inArchiveDir, ImageType type) {
    605         ZipFile zipFile = null;
    606         try {
    607             zipFile = new ZipFile(archive);
     601        try (ZipFile zipFile = new ZipFile(archive)) {
    608602            if (inArchiveDir == null || ".".equals(inArchiveDir)) {
    609603                inArchiveDir = "";
     
    613607            String entryName = inArchiveDir + fullName;
    614608            ZipEntry entry = zipFile.getEntry(entryName);
    615             if(entry != null)
    616             {
     609            if (entry != null) {
    617610                int size = (int)entry.getSize();
    618611                int offs = 0;
    619612                byte[] buf = new byte[size];
    620                 InputStream is = null;
    621                 try {
    622                     is = zipFile.getInputStream(entry);
     613                try (InputStream is = zipFile.getInputStream(entry)) {
    623614                    switch (type) {
    624615                    case SVG:
     
    641632                        return img == null ? null : new ImageResource(img);
    642633                    default:
    643                         throw new AssertionError();
     634                        throw new AssertionError("Unknown ImageType: "+type);
    644635                    }
    645                 } finally {
    646                     Utils.close(is);
    647636                }
    648637            }
    649638        } catch (Exception e) {
    650639            Main.warn(tr("Failed to handle zip file ''{0}''. Exception was: {1}", archive.getName(), e.toString()));
    651         } finally {
    652             Utils.close(zipFile);
    653640        }
    654641        return null;
     
    785772            });
    786773
    787             parser.parse(new InputSource(new MirroredInputStream(
     774            try (InputStream is = new MirroredInputStream(
    788775                    base + fn,
    789                     new File(Main.pref.getPreferencesDir(), "images").toString()
    790                     )));
     776                    new File(Main.pref.getPreferencesDir(), "images").toString())
     777            ) {
     778                parser.parse(new InputSource(is));
     779            }
    791780        } catch (SAXReturnException r) {
    792781            return r.getResult();
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r7012 r7033  
    283283                File file = new File(path);
    284284                if (file.exists()) {
    285                     BufferedReader reader = null;
    286                     try {
    287                         reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8));
     285                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Utils.UTF_8))) {
    288286                        String id = null;
    289287                        String release = null;
     
    313311                    } catch (IOException e) {
    314312                        // Ignore
    315                     } finally {
    316                         Utils.close(reader);
    317313                    }
    318314                }
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r6898 r7033  
    4343    public String read(String url) throws IOException {
    4444        URL u = new URL(url);
    45         BufferedReader in = Utils.openURLReader(u);
    46         try {
     45        try (BufferedReader in = Utils.openURLReader(u)) {
    4746            if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
    4847                return readFromTrac(in, u);
    4948            return readNormal(in);
    50         } finally {
    51             Utils.close(in);
    5249        }
    5350    }
     
    8481
    8582    private String readLang(URL url) throws IOException {
    86         BufferedReader in;
    87         try {
    88             in = Utils.openURLReader(url);
     83        try (BufferedReader in = Utils.openURLReader(url)) {
     84            return readFromTrac(in, url);
    8985        } catch (IOException e) {
    9086            Main.addNetworkError(url, Utils.getRootCause(e));
    9187            throw e;
    92         }
    93         try {
    94             return readFromTrac(in, url);
    95         } finally {
    96             Utils.close(in);
    9788        }
    9889    }
  • trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java

    r7012 r7033  
    55
    66import java.io.IOException;
     7import java.io.InputStream;
    78import java.io.Reader;
    89import java.lang.reflect.Field;
     
    1718import java.util.Stack;
    1819
     20import javax.xml.XMLConstants;
    1921import javax.xml.parsers.ParserConfigurationException;
    2022import javax.xml.parsers.SAXParser;
     
    278280
    279281    public Iterable<Object> startWithValidation(final Reader in, String namespace, String schemaSource) throws SAXException {
    280         try {
    281             SchemaFactory factory =  SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    282             Schema schema = factory.newSchema(new StreamSource(new MirroredInputStream(schemaSource)));
     282        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
     283        try (InputStream mis = new MirroredInputStream(schemaSource)) {
     284            Schema schema = factory.newSchema(new StreamSource(mis));
    283285            ValidatorHandler validator = schema.newValidatorHandler();
    284286            validator.setContentHandler(parser);
Note: See TracChangeset for help on using the changeset viewer.