Changeset 19104 in josm


Ignore:
Timestamp:
2024-06-13T14:05:00+02:00 (5 months ago)
Author:
taylor.smock
Message:

Fix #23731: Don't close output stream (patch by GerdP, modified)

The major modification is the addition of @SuppressWarnings.

File:
1 edited

Legend:

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

    r19103 r19104  
    22package org.openstreetmap.josm.io.session;
    33
    4 import java.io.IOException;
    54import java.io.OutputStream;
    65import java.io.OutputStreamWriter;
    76import java.io.PrintWriter;
    8 import java.io.UncheckedIOException;
    97import java.io.Writer;
    108import java.nio.charset.StandardCharsets;
     
    1412import org.openstreetmap.josm.io.OsmWriter;
    1513import org.openstreetmap.josm.io.OsmWriterFactory;
    16 import org.openstreetmap.josm.tools.JosmRuntimeException;
    1714
    1815/**
     
    3835     * Exports OSM data to the given output stream.
    3936     * @param data data set
    40      * @param out output stream
    41      * @since 15386
     37     * @param out output stream (must be closed by caller; note: if caller has access, caller should use
     38     *            {@link org.apache.commons.io.output.CloseShieldOutputStream} when calling this method to
     39     *            avoid potential future issues)
    4240     */
     41    @SuppressWarnings({"squid:S2095", "PMD.CloseResource"}) // All the closeables in this method will close the input OutputStream.
    4342    public static void exportData(DataSet data, OutputStream out) {
     43        // This writer will close out when it is closed
    4444        Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
     45        // The PrintWriter will close the writer when it is closed, and the OsmWriter will close the PrintWriter when it is closed.
     46        OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, data.getVersion());
    4547        data.getReadLock().lock();
    46         try (OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, data.getVersion())) {
     48        try {
    4749            w.write(data);
    4850            w.flush();
    49         } catch (IOException e) {
    50             // Catch needed since XmlWriter (parent of OsmWriter) has IOException in the method signature.
    51             // It doesn't actually throw though. In other words, we should never hit this.
    52             throw new UncheckedIOException(e);
    5351        } finally {
    5452            data.getReadLock().unlock();
Note: See TracChangeset for help on using the changeset viewer.