Changeset 6747 in josm


Ignore:
Timestamp:
2014-01-20T19:32:25+01:00 (10 years ago)
Author:
bastiK
Message:

#8581 - Embedded SVG leads to very high memory consumption

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/session/SessionReader.java

    r6643 r6747  
    373373                    deps.putVoid(idx);
    374374                    String depStr = e.getAttribute("depends");
    375                     if (depStr != null) {
     375                    if (depStr != null && !depStr.isEmpty()) {
    376376                        for (String sd : depStr.split(",")) {
    377377                            Integer d = null;
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r6615 r6747  
    396396                return null;
    397397
    398             try {
    399                 if (name.startsWith("data:")) {
    400                     Matcher m = dataUrlPattern.matcher(name);
    401                     if (m.matches()) {
    402                         String mediatype = m.group(1);
    403                         String base64 = m.group(2);
    404                         String data = m.group(3);
    405                         byte[] bytes = ";base64".equals(base64)
    406                                 ? Base64.decodeBase64(data)
    407                                         : URLDecoder.decode(data, "utf-8").getBytes();
    408                                 if (mediatype != null && mediatype.contains("image/svg+xml")) {
    409                                     URI uri = getSvgUniverse().loadSVG(new StringReader(new String(bytes)), name);
    410                                     return new ImageResource(getSvgUniverse().getDiagram(uri));
    411                                 } else {
    412                                     try {
    413                                         return new ImageResource(ImageIO.read(new ByteArrayInputStream(bytes)));
    414                                     } catch (IOException e) {
    415                                         Main.warn("IOException while reading image: "+e.getMessage());
    416                                     }
    417                                 }
    418                     }
    419                 }
    420             } catch (UnsupportedEncodingException ex) {
    421                 throw new RuntimeException(ex.getMessage(), ex);
     398            if (name.startsWith("data:")) {
     399                String url = name;
     400                ImageResource ir = cache.get(url);
     401                if (ir != null) return ir;
     402                ir = getIfAvailableDataUrl(url);
     403                if (ir != null) {
     404                    cache.put(url, ir);
     405                }
     406                return ir;
    422407            }
    423408
     
    535520        } finally {
    536521            Utils.close(is);
     522        }
     523    }
     524
     525    private static ImageResource getIfAvailableDataUrl(String url) {
     526        try {
     527            Matcher m = dataUrlPattern.matcher(url);
     528            if (m.matches()) {
     529                String mediatype = m.group(1);
     530                String base64 = m.group(2);
     531                String data = m.group(3);
     532                byte[] bytes = ";base64".equals(base64)
     533                        ? Base64.decodeBase64(data)
     534                                : URLDecoder.decode(data, "utf-8").getBytes();
     535                if (mediatype != null && mediatype.contains("image/svg+xml")) {
     536                    URI uri = getSvgUniverse().loadSVG(new StringReader(new String(bytes)), url);
     537                    return new ImageResource(getSvgUniverse().getDiagram(uri));
     538                } else {
     539                    try {
     540                        return new ImageResource(ImageIO.read(new ByteArrayInputStream(bytes)));
     541                    } catch (IOException e) {
     542                        Main.warn("IOException while reading image: "+e.getMessage());
     543                    }
     544                }
     545            }
     546            return null;
     547        } catch (UnsupportedEncodingException ex) {
     548            throw new RuntimeException(ex.getMessage(), ex);
    537549        }
    538550    }
Note: See TracChangeset for help on using the changeset viewer.