Changeset 9280 in josm for trunk


Ignore:
Timestamp:
2016-01-03T16:30:55+01:00 (9 years ago)
Author:
Don-vip
Message:

improve performance and simplify file copy operations (major performance gain when downloading geotools plugin)

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

Legend:

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

    r9187 r9280  
    77import java.awt.event.ActionEvent;
    88import java.io.File;
    9 import java.io.FileOutputStream;
    109import java.io.IOException;
    1110import java.io.InputStream;
    1211import java.net.URI;
     12import java.nio.file.Files;
     13import java.nio.file.StandardCopyOption;
    1314import java.util.Arrays;
    1415import java.util.List;
     
    153154                        file = File.createTempFile("session_", ".joz", Utils.getJosmTempDir());
    154155                        tempFile = true;
    155                         try (FileOutputStream out = new FileOutputStream(file)) {
    156                             Utils.copyStream(is, out);
    157                         }
     156                        Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    158157                    }
    159158                    reader.loadSession(file, zip, monitor);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r9078 r9280  
    1616import java.awt.event.MouseEvent;
    1717import java.io.BufferedInputStream;
    18 import java.io.BufferedOutputStream;
    1918import java.io.BufferedReader;
    2019import java.io.File;
    21 import java.io.FileOutputStream;
    2220import java.io.IOException;
    2321import java.io.InputStream;
    2422import java.io.InputStreamReader;
    25 import java.io.OutputStream;
    2623import java.nio.charset.StandardCharsets;
     24import java.nio.file.Files;
     25import java.nio.file.StandardCopyOption;
    2726import java.util.ArrayList;
    2827import java.util.Arrays;
     
    510509                try {
    511510                    InputStream in = s.getSourceInputStream();
    512                     try (
    513                         InputStream bis = new BufferedInputStream(in);
    514                         OutputStream bos = new BufferedOutputStream(new FileOutputStream(file))
    515                     ) {
    516                         byte[] buffer = new byte[4096];
    517                         int length;
    518                         while ((length = bis.read(buffer)) > -1 && !canceled) {
    519                             bos.write(buffer, 0, length);
    520                         }
     511                    try (InputStream bis = new BufferedInputStream(in)) {
     512                        Files.copy(bis, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    521513                    } finally {
    522514                        s.closeSourceInputStream(in);
    523515                    }
    524516                } catch (IOException e) {
     517                    Main.warn(e);
    525518                    error = true;
    526519                }
  • trunk/src/org/openstreetmap/josm/gui/io/DownloadFileTask.java

    r9171 r9280  
    55
    66import java.awt.Component;
    7 import java.io.BufferedOutputStream;
    87import java.io.File;
    98import java.io.FileOutputStream;
     
    1413import java.net.URL;
    1514import java.nio.charset.StandardCharsets;
     15import java.nio.file.Files;
     16import java.nio.file.StandardCopyOption;
    1617import java.util.Enumeration;
    1718import java.util.zip.ZipEntry;
     
    182183                if (ze.isDirectory()) {
    183184                    newFile.mkdirs();
    184                 } else try (
    185                     InputStream is = zf.getInputStream(ze);
    186                     OutputStream os = new BufferedOutputStream(new FileOutputStream(newFile))
    187                 ) {
    188                     byte[] buffer = new byte[8192];
    189                     int read;
    190                     while ((read = is.read(buffer)) != -1) {
    191                         os.write(buffer, 0, read);
    192                     }
     185                } else try (InputStream is = zf.getInputStream(ze)) {
     186                    Files.copy(is, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    193187                }
    194188            }
  • trunk/src/org/openstreetmap/josm/io/CachedFile.java

    r9168 r9280  
    55
    66import java.io.BufferedInputStream;
    7 import java.io.BufferedOutputStream;
    87import java.io.File;
    98import java.io.FileInputStream;
    10 import java.io.FileOutputStream;
    119import java.io.IOException;
    1210import java.io.InputStream;
    13 import java.io.OutputStream;
    1411import java.net.HttpURLConnection;
    1512import java.net.MalformedURLException;
    1613import java.net.URL;
    1714import java.nio.charset.StandardCharsets;
     15import java.nio.file.Files;
     16import java.nio.file.StandardCopyOption;
    1817import java.util.ArrayList;
    1918import java.util.Arrays;
     
    429428                return localFile;
    430429            }
    431             try (
    432                 InputStream bis = new BufferedInputStream(con.getContent());
    433                 OutputStream fos = new FileOutputStream(destDirFile);
    434                 OutputStream bos = new BufferedOutputStream(fos)
    435             ) {
    436                 byte[] buffer = new byte[4096];
    437                 int length;
    438                 while ((length = bis.read(buffer)) > -1) {
    439                     bos.write(buffer, 0, length);
    440                 }
     430            try (InputStream bis = new BufferedInputStream(con.getContent())) {
     431                Files.copy(bis, destDirFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    441432            }
    442433            localFile = new File(destDir, localPath);
  • trunk/src/org/openstreetmap/josm/plugins/Plugin.java

    r9231 r9280  
    44import java.io.File;
    55import java.io.FileNotFoundException;
    6 import java.io.FileOutputStream;
    76import java.io.IOException;
    87import java.io.InputStream;
    98import java.net.URL;
    109import java.net.URLClassLoader;
     10import java.nio.file.Files;
     11import java.nio.file.StandardCopyOption;
    1112import java.security.AccessController;
    1213import java.security.PrivilegedAction;
     
    119120            pluginDir.mkdirs();
    120121        }
    121         try (
    122             FileOutputStream out = new FileOutputStream(new File(pluginDirName, to));
    123             InputStream in = getClass().getResourceAsStream(from)
    124         ) {
     122        try (InputStream in = getClass().getResourceAsStream(from)) {
    125123            if (in == null) {
    126124                throw new IOException("Resource not found: "+from);
    127125            }
    128             byte[] buffer = new byte[8192];
    129             for (int len = in.read(buffer); len > 0; len = in.read(buffer)) {
    130                 out.write(buffer, 0, len);
    131             }
     126            Files.copy(in, new File(pluginDirName, to).toPath(), StandardCopyOption.REPLACE_EXISTING);
    132127        }
    133128    }
  • trunk/src/org/openstreetmap/josm/plugins/PluginDownloadTask.java

    r9168 r9280  
    66import java.awt.Component;
    77import java.io.File;
    8 import java.io.FileOutputStream;
    98import java.io.IOException;
    109import java.io.InputStream;
    11 import java.io.OutputStream;
    1210import java.net.MalformedURLException;
    1311import java.net.URL;
     12import java.nio.file.Files;
     13import java.nio.file.StandardCopyOption;
    1414import java.util.Collection;
    1515import java.util.LinkedList;
     
    127127                        .connect();
    128128            }
    129             try (
    130                 InputStream in = downloadConnection.getContent();
    131                 OutputStream out = new FileOutputStream(file)
    132             ) {
    133                 byte[] buffer = new byte[8192];
    134                 for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
    135                     out.write(buffer, 0, read);
    136                 }
     129            try (InputStream in = downloadConnection.getContent()) {
     130                Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
    137131            }
    138132        } catch (MalformedURLException e) {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r9274 r9280  
    2323import java.io.InputStream;
    2424import java.io.InputStreamReader;
    25 import java.io.OutputStream;
    2625import java.io.UnsupportedEncodingException;
    2726import java.net.HttpURLConnection;
     
    435434
    436435    /**
    437      * Copy data from source stream to output stream.
    438      * @param source source stream
    439      * @param destination target stream
    440      * @return number of bytes copied
    441      * @throws IOException if any I/O error occurs
    442      */
    443     public static int copyStream(InputStream source, OutputStream destination) throws IOException {
    444         int count = 0;
    445         byte[] b = new byte[512];
    446         int read;
    447         while ((read = source.read(b)) != -1) {
    448             count += read;
    449             destination.write(b, 0, read);
    450         }
    451         return count;
    452     }
    453 
    454     /**
    455436     * Deletes a directory recursively.
    456437     * @param path The directory to delete
Note: See TracChangeset for help on using the changeset viewer.