Changeset 13204 in josm for trunk


Ignore:
Timestamp:
2017-12-17T00:17:20+01:00 (7 years ago)
Author:
Don-vip
Message:

enable new PMD rule AvoidFileStream - see https://pmd.github.io/pmd-6.0.0/pmd_rules_java_performance.html#avoidfilestream / https://bugs.openjdk.java.net/browse/JDK-8080225 for details

Location:
trunk
Files:
22 edited

Legend:

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

    r12856 r13204  
    33
    44import java.io.File;
    5 import java.io.FileOutputStream;
    65import java.io.IOException;
     6import java.nio.channels.FileChannel;
    77import java.nio.channels.FileLock;
     8import java.nio.file.StandardOpenOption;
    89import java.util.Arrays;
    910import java.util.Properties;
     
    110111            Logging.warn("Cannot create cache dir lock file");
    111112        }
    112         cacheDirLock = new FileOutputStream(cacheDirLockPath).getChannel().tryLock();
     113        cacheDirLock = FileChannel.open(cacheDirLockPath.toPath(), StandardOpenOption.WRITE).tryLock();
    113114
    114115        if (cacheDirLock == null)
  • trunk/src/org/openstreetmap/josm/data/projection/datum/NTV2Proj4DirGridShiftFileSource.java

    r12868 r13204  
    33
    44import java.io.File;
    5 import java.io.FileInputStream;
    6 import java.io.FileNotFoundException;
     5import java.io.IOException;
    76import java.io.InputStream;
     7import java.nio.file.Files;
     8import java.nio.file.InvalidPathException;
    89import java.util.Arrays;
    910import java.util.Collections;
     
    6364        if (grid != null) {
    6465            try {
    65                 return new FileInputStream(grid.getAbsoluteFile());
    66             } catch (FileNotFoundException ex) {
    67                 Logging.warn("NTV2 grid shift file not found: " + grid);
    68                 Logging.trace(ex);
     66                return Files.newInputStream(grid.getAbsoluteFile().toPath());
     67            } catch (IOException | InvalidPathException ex) {
     68                Logging.warn("Unable to open NTV2 grid shift file: " + grid);
     69                Logging.debug(ex);
    6970            }
    7071        }
  • trunk/src/org/openstreetmap/josm/gui/io/CustomConfigurator.java

    r13173 r13204  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedInputStream;
    76import java.io.ByteArrayInputStream;
    87import java.io.CharArrayReader;
    98import java.io.CharArrayWriter;
    109import java.io.File;
    11 import java.io.FileInputStream;
    1210import java.io.IOException;
    1311import java.io.InputStream;
    1412import java.nio.charset.StandardCharsets;
     13import java.nio.file.Files;
     14import java.nio.file.InvalidPathException;
    1515import java.util.ArrayList;
    1616import java.util.Collection;
     
    397397                String fileDir = file.getParentFile().getAbsolutePath();
    398398                if (fileDir != null) engine.eval("scriptDir='"+normalizeDirName(fileDir) +"';");
    399                 try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
     399                try (InputStream is = Files.newInputStream(file.toPath())) {
    400400                    openAndReadXML(is);
    401401                }
    402             } catch (ScriptException | IOException | SecurityException ex) {
     402            } catch (ScriptException | IOException | SecurityException | InvalidPathException ex) {
    403403                PreferencesUtils.log(ex, "Error reading custom preferences:");
    404404            }
  • trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java

    r12620 r13204  
    66import java.awt.Component;
    77import java.io.File;
    8 import java.io.FileOutputStream;
    98import java.io.IOException;
    109import java.io.InputStream;
     
    1413import java.nio.charset.StandardCharsets;
    1514import java.nio.file.Files;
     15import java.nio.file.InvalidPathException;
    1616import java.nio.file.StandardCopyOption;
    1717import java.util.Enumeration;
     
    115115            try (
    116116                InputStream in = downloadConnection.getResponse().getContent();
    117                 OutputStream out = new FileOutputStream(file)
     117                OutputStream out = Files.newOutputStream(file.toPath())
    118118            ) {
    119119                byte[] buffer = new byte[32_768];
     
    145145            Logging.warn(msg);
    146146            throw new DownloadException(msg, e);
    147         } catch (IOException e) {
     147        } catch (IOException | InvalidPathException e) {
    148148            if (canceled)
    149149                return;
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/NoteExporter.java

    r12671 r13204  
    55
    66import java.io.File;
    7 import java.io.FileOutputStream;
    87import java.io.IOException;
    98import java.io.OutputStream;
     9import java.nio.file.Files;
    1010
    1111import org.openstreetmap.josm.actions.ExtensionFileFilter;
     
    4040        Logging.info("exporting notes to file: " + file);
    4141        if (layer instanceof NoteLayer) {
    42             try (OutputStream os = new FileOutputStream(file);
     42            try (OutputStream os = Files.newOutputStream(file.toPath());
    4343                 NoteWriter writer = new NoteWriter(os)) {
    4444                writer.write(((NoteLayer) layer).getNoteData());
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/ValidatorErrorExporter.java

    r12671 r13204  
    55
    66import java.io.File;
    7 import java.io.FileOutputStream;
    87import java.io.IOException;
    98import java.io.OutputStream;
     9import java.nio.file.Files;
    1010
    1111import org.openstreetmap.josm.actions.ExtensionFileFilter;
     
    4444        if (layer instanceof ValidatorLayer && editLayer != null) {
    4545            Logging.info("exporting validation errors to file: " + file);
    46             try (OutputStream os = new FileOutputStream(file);
     46            try (OutputStream os = Files.newOutputStream(file.toPath());
    4747                 ValidatorErrorWriter writer = new ValidatorErrorWriter(os)) {
    4848                writer.write(editLayer.validationErrors);
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/WMSLayerExporter.java

    r12851 r13204  
    33
    44import java.io.File;
    5 import java.io.FileOutputStream;
    65import java.io.IOException;
    76import java.io.ObjectOutputStream;
     7import java.nio.file.Files;
    88
    99import org.openstreetmap.josm.data.StructUtils;
     
    3838
    3939        if (layer instanceof AbstractTileSourceLayer) {
    40             try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
     40            try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) {
    4141                oos.writeInt(CURRENT_FILE_VERSION); // file version
    4242                oos.writeObject(MainApplication.getMap().mapView.getCenter());
    43                 ImageryPreferenceEntry entry = new ImageryPreferenceEntry(((AbstractTileSourceLayer) layer).getInfo());
     43                ImageryPreferenceEntry entry = new ImageryPreferenceEntry(((AbstractTileSourceLayer<?>) layer).getInfo());
    4444                oos.writeObject(StructUtils.serializeStruct(entry, ImageryPreferenceEntry.class));
    4545            }
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/WMSLayerImporter.java

    r12851 r13204  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.IOException;
    98import java.io.InvalidClassException;
    109import java.io.ObjectInputStream;
     10import java.nio.file.Files;
    1111import java.util.Map;
    1212
     
    4949        final ImageryLayer layer;
    5050
    51         try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
     51        try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(file.toPath()))) {
    5252            int sfv = ois.readInt();
    5353            if (sfv < 5) {
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

    r13130 r13204  
    2121import java.awt.event.WindowEvent;
    2222import java.io.File;
    23 import java.io.FileInputStream;
    2423import java.io.IOException;
    2524import java.io.InputStream;
     25import java.nio.file.Files;
    2626import java.text.DateFormat;
    2727import java.text.ParseException;
     
    331331        private InputStream createInputStream(File sel) throws IOException {
    332332            if (Utils.hasExtension(sel, "gpx.gz")) {
    333                 return new GZIPInputStream(new FileInputStream(sel));
     333                return new GZIPInputStream(Files.newInputStream(sel.toPath()));
    334334            } else {
    335                 return new FileInputStream(sel);
     335                return Files.newInputStream(sel.toPath());
    336336            }
    337337        }
  • trunk/src/org/openstreetmap/josm/gui/mappaint/RenderingCLI.java

    r13050 r13204  
    77import java.awt.image.BufferedImage;
    88import java.io.File;
    9 import java.io.FileInputStream;
    109import java.io.FileNotFoundException;
    1110import java.io.IOException;
     11import java.nio.file.Files;
     12import java.nio.file.Paths;
    1213import java.util.ArrayList;
    1314import java.util.List;
     
    543544    }
    544545
    545     private DataSet loadDataset() throws FileNotFoundException, IllegalDataException {
     546    private DataSet loadDataset() throws IOException, IllegalDataException {
    546547        if (argInput == null) {
    547548            throw new IllegalArgumentException(tr("Missing argument - input data file ({0})", "--input|-i"));
    548549        }
    549550        try {
    550             return OsmReader.parseDataSet(new FileInputStream(argInput), null);
     551            return OsmReader.parseDataSet(Files.newInputStream(Paths.get(argInput)), null);
    551552        } catch (IllegalDataException e) {
    552553            throw new IllegalDataException(tr("In .osm data file ''{0}'' - ", argInput) + e.getMessage());
  • trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java

    r12856 r13204  
    55import java.io.BufferedOutputStream;
    66import java.io.File;
    7 import java.io.FileInputStream;
    8 import java.io.FileOutputStream;
    97import java.io.IOException;
    108import java.nio.charset.StandardCharsets;
     9import java.nio.file.Files;
     10import java.nio.file.InvalidPathException;
    1111import java.util.concurrent.TimeUnit;
    1212
     
    185185     */
    186186    private void loadFromDisk() throws T {
    187         try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(path))) {
     187        try (BufferedInputStream input = new BufferedInputStream(Files.newInputStream(path.toPath()))) {
    188188            this.data = new byte[input.available()];
    189189            if (input.read(this.data) < this.data.length) {
    190190                Logging.error("Failed to read expected contents from "+path);
    191191            }
    192         } catch (IOException e) {
     192        } catch (IOException | InvalidPathException e) {
    193193            Logging.trace(e);
    194194            if (!isOffline()) {
     
    202202     */
    203203    private void saveToDisk() {
    204         try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(path))) {
     204        try (BufferedOutputStream output = new BufferedOutputStream(Files.newOutputStream(path.toPath()))) {
    205205            output.write(this.data);
    206206            output.flush();
    207         } catch (IOException e) {
     207        } catch (IOException | InvalidPathException e) {
    208208            Logging.error(e);
    209209        }
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r12856 r13204  
    88import java.io.Closeable;
    99import java.io.File;
    10 import java.io.FileInputStream;
    1110import java.io.IOException;
    1211import java.io.InputStream;
     
    218217            }
    219218        }
    220         return new FileInputStream(file);
     219        return Files.newInputStream(file.toPath());
    221220    }
    222221
  • trunk/src/org/openstreetmap/josm/io/Compression.java

    r12772 r13204  
    33
    44import java.io.File;
    5 import java.io.FileInputStream;
    6 import java.io.FileOutputStream;
    75import java.io.IOException;
    86import java.io.InputStream;
    97import java.io.OutputStream;
    108import java.nio.charset.StandardCharsets;
     9import java.nio.file.Files;
    1110import java.util.zip.GZIPInputStream;
    1211import java.util.zip.GZIPOutputStream;
     
    150149     */
    151150    public static InputStream getUncompressedFileInputStream(File file) throws IOException {
    152         FileInputStream in = new FileInputStream(file);
     151        InputStream in = Files.newInputStream(file.toPath());
    153152        try {
    154153            return byExtension(file.getName()).getUncompressedInputStream(in);
     
    188187     */
    189188    public static OutputStream getCompressedFileOutputStream(File file) throws IOException {
    190         FileOutputStream out = new FileOutputStream(file);
     189        OutputStream out = Files.newOutputStream(file.toPath());
    191190        try {
    192191            return byExtension(file.getName()).getCompressedOutputStream(out);
  • trunk/src/org/openstreetmap/josm/io/session/SessionReader.java

    r12620 r13204  
    77import java.io.BufferedInputStream;
    88import java.io.File;
    9 import java.io.FileInputStream;
    109import java.io.FileNotFoundException;
    1110import java.io.IOException;
     
    1514import java.net.URISyntaxException;
    1615import java.nio.charset.StandardCharsets;
     16import java.nio.file.Files;
    1717import java.util.ArrayList;
    1818import java.util.Collection;
     
    715715            }
    716716        } else {
    717             try {
    718                 return new FileInputStream(sessionFile);
    719             } catch (FileNotFoundException ex) {
    720                 throw new IOException(ex);
    721             }
     717            return Files.newInputStream(sessionFile.toPath());
    722718        }
    723719    }
  • trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java

    r12778 r13204  
    44import java.io.BufferedOutputStream;
    55import java.io.File;
    6 import java.io.FileNotFoundException;
    7 import java.io.FileOutputStream;
    86import java.io.IOException;
    97import java.io.OutputStream;
    108import java.io.OutputStreamWriter;
    119import java.nio.charset.StandardCharsets;
     10import java.nio.file.Files;
    1211import java.util.ArrayList;
    1312import java.util.Collection;
     
    331330     */
    332331    public void write(File f) throws IOException {
    333         try (OutputStream out = new FileOutputStream(f)) {
     332        try (OutputStream out = Files.newOutputStream(f.toPath())) {
    334333            write(out);
    335         } catch (FileNotFoundException e) {
    336             throw new IOException(e);
    337334        }
    338335    }
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r12620 r13204  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.IOException;
    98import java.io.InputStream;
    109import java.lang.reflect.Constructor;
    1110import java.net.URL;
     11import java.nio.file.Files;
     12import java.nio.file.InvalidPathException;
    1213import java.text.MessageFormat;
    1314import java.util.ArrayList;
     
    114115        this.file = file;
    115116        try (
    116             FileInputStream fis = new FileInputStream(file);
     117            InputStream fis = Files.newInputStream(file.toPath());
    117118            JarInputStream jar = new JarInputStream(fis)
    118119        ) {
     
    122123            scanManifest(manifest, false);
    123124            libraries.add(0, Utils.fileToURL(file));
    124         } catch (IOException e) {
     125        } catch (IOException | InvalidPathException e) {
    125126            throw new PluginException(name, e);
    126127        }
  • trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

    r12620 r13204  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.FilenameFilter;
    98import java.io.IOException;
     9import java.io.InputStream;
     10import java.nio.file.Files;
     11import java.nio.file.InvalidPathException;
    1012import java.util.ArrayList;
    1113import java.util.Collection;
     
    145147
    146148    protected void processLocalPluginInformationFile(File file) throws PluginListParseException {
    147         try (FileInputStream fin = new FileInputStream(file)) {
     149        try (InputStream fin = Files.newInputStream(file.toPath())) {
    148150            List<PluginInformation> pis = new PluginListParser().parse(fin);
    149151            for (PluginInformation pi : pis) {
     
    154156                availablePlugins.put(pi.name, pi);
    155157            }
    156         } catch (IOException e) {
     158        } catch (IOException | InvalidPathException e) {
    157159            throw new PluginListParseException(e);
    158160        }
  • trunk/src/org/openstreetmap/josm/tools/I18n.java

    r12931 r13204  
    44import java.io.BufferedInputStream;
    55import java.io.File;
    6 import java.io.FileInputStream;
    76import java.io.IOException;
    87import java.io.InputStream;
     
    1110import java.net.URL;
    1211import java.nio.charset.StandardCharsets;
     12import java.nio.file.Files;
     13import java.nio.file.InvalidPathException;
    1314import java.text.MessageFormat;
    1415import java.util.ArrayList;
     
    372373        final String langfile = "data/"+loadedCode+".lang";
    373374        try (
    374             FileInputStream fis = new FileInputStream(source);
     375            InputStream fis = Files.newInputStream(source.toPath());
    375376            JarInputStream jar = new JarInputStream(fis)
    376377        ) {
     
    384385            if (found) {
    385386                try (
    386                     FileInputStream fisTrans = new FileInputStream(source);
     387                    InputStream fisTrans = Files.newInputStream(source.toPath());
    387388                    JarInputStream jarTrans = new JarInputStream(fisTrans)
    388389                ) {
     
    397398                }
    398399            }
    399         } catch (IOException e) {
    400             // Ignore
     400        } catch (IOException | InvalidPathException e) {
    401401            Logging.trace(e);
    402402        }
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookUnixoid.java

    r12846 r13204  
    88import java.io.BufferedReader;
    99import java.io.File;
    10 import java.io.FileInputStream;
    1110import java.io.IOException;
     11import java.io.InputStream;
    1212import java.net.URI;
    1313import java.net.URISyntaxException;
     
    403403        if (f.exists()) {
    404404            CertificateFactory fact = CertificateFactory.getInstance("X.509");
    405             try (FileInputStream is = new FileInputStream(f)) {
     405            try (InputStream is = Files.newInputStream(f.toPath())) {
    406406                return (X509Certificate) fact.generateCertificate(is);
    407407            }
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r12887 r13204  
    3232import java.io.BufferedWriter;
    3333import java.io.File;
    34 import java.io.FileInputStream;
    3534import java.io.IOException;
     35import java.io.InputStream;
    3636import java.io.OutputStream;
    3737import java.io.OutputStreamWriter;
     
    4242import java.nio.file.FileSystems;
    4343import java.nio.file.Files;
     44import java.nio.file.InvalidPathException;
    4445import java.nio.file.Path;
    4546import java.security.InvalidKeyException;
     
    518519            return;
    519520        }
    520         try (FileInputStream fis = new FileInputStream(templateFile.toFile())) {
     521        try (InputStream fis = Files.newInputStream(templateFile)) {
    521522            Properties props = new Properties();
    522523            props.load(fis);
     
    581582            }
    582583            Utils.updateSystemProperty("sun.awt.fontconfig", fontconfigFile.toString());
    583         } catch (IOException ex) {
     584        } catch (IOException | InvalidPathException ex) {
    584585            Logging.error(ex);
    585586        }
  • trunk/src/org/openstreetmap/josm/tools/RightAndLefthandTraffic.java

    r12856 r13204  
    33
    44import java.io.File;
    5 import java.io.FileInputStream;
    6 import java.io.FileOutputStream;
    75import java.io.IOException;
    86import java.io.InputStream;
     
    119import java.io.Writer;
    1210import java.nio.charset.StandardCharsets;
     11import java.nio.file.Files;
     12import java.nio.file.InvalidPathException;
     13import java.nio.file.Paths;
    1314import java.util.ArrayList;
    1415import java.util.Collection;
     
    154155        DataSet ds = optimizedWays.iterator().next().getDataSet();
    155156        File file = new File(Config.getDirs().getCacheDirectory(true), "left-right-hand-traffic.osm");
    156         try (Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
     157        try (Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8);
    157158             OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, ds.getVersion())
    158159            ) {
     
    166167
    167168    private static Collection<Way> loadOptimizedBoundaries() {
    168         try (InputStream is = new FileInputStream(new File(
    169                 Config.getDirs().getCacheDirectory(false), "left-right-hand-traffic.osm"))) {
     169        try (InputStream is = Files.newInputStream(Paths.get(
     170                Config.getDirs().getCacheDirectory(false).getPath(), "left-right-hand-traffic.osm"))) {
    170171           return OsmReader.parseDataSet(is, null).getWays();
    171         } catch (IllegalDataException | IOException ex) {
     172        } catch (IllegalDataException | IOException | InvalidPathException ex) {
    172173            Logging.trace(ex);
    173174            return Collections.emptyList();
  • trunk/tools/pmd/josm-ruleset.xml

    r13202 r13204  
    170170  </rule>
    171171  <rule ref="category/java/performance.xml">
    172     <exclude name="AvoidFileStream"/>
    173172    <exclude name="AvoidInstantiatingObjectsInLoops"/>
    174173    <exclude name="AvoidUsingShortType"/>
Note: See TracChangeset for help on using the changeset viewer.