Ignore:
Timestamp:
2014-06-14T12:40:50+02:00 (11 years ago)
Author:
bastiK
Message:

reworked MirroredInputStream (renamed to CachedFile):

  • no more awkwardly open and close InputStream if you just want the underlying file (e.g. to get file inside zip file)
  • make it easier to add configuration parameters, without having endless list of parameters for the constructor (Factory style, similar to ImageProvider)

breaks plugins; see #10139

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r7198 r7248  
    3030import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference.MapPaintPrefHelper;
    3131import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    32 import org.openstreetmap.josm.io.MirroredInputStream;
     32import org.openstreetmap.josm.io.CachedFile;
    3333import org.openstreetmap.josm.tools.ImageProvider;
    3434import org.openstreetmap.josm.tools.Utils;
     
    228228
    229229    private static StyleSource fromSourceEntry(SourceEntry entry) {
    230         MirroredInputStream in = null;
     230        CachedFile cf = null;
    231231        try {
    232232            Set<String> mimes = new HashSet<>();
    233233            mimes.addAll(Arrays.asList(XmlStyleSource.XML_STYLE_MIME_TYPES.split(", ")));
    234234            mimes.addAll(Arrays.asList(MapCSSStyleSource.MAPCSS_STYLE_MIME_TYPES.split(", ")));
    235             in = new MirroredInputStream(entry.url, null, Utils.join(", ", mimes));
    236             String zipEntryPath = in.findZipEntryPath("mapcss", "style");
     235            cf = new CachedFile(entry.url).setHttpAccept(Utils.join(", ", mimes));
     236            String zipEntryPath = cf.findZipEntryPath("mapcss", "style");
    237237            if (zipEntryPath != null) {
    238238                entry.isZip = true;
     
    240240                return new MapCSSStyleSource(entry);
    241241            }
    242             zipEntryPath = in.findZipEntryPath("xml", "style");
     242            zipEntryPath = cf.findZipEntryPath("xml", "style");
    243243            if (zipEntryPath != null)
    244244                return new XmlStyleSource(entry);
     
    248248                return new XmlStyleSource(entry);
    249249            else {
    250                 try (InputStreamReader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) {
     250                try (InputStreamReader reader = new InputStreamReader(cf.getInputStream(), StandardCharsets.UTF_8)) {
    251251                    WHILE: while (true) {
    252252                        int c = reader.read();
     
    272272            Main.warn(tr("Failed to load Mappaint styles from ''{0}''. Exception was: {1}", entry.url, e.toString()));
    273273            Main.error(e);
    274         } finally {
    275             Utils.close(in);
    276274        }
    277275        return null;
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r7185 r7248  
    1818import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    1919import org.openstreetmap.josm.gui.preferences.SourceEntry;
    20 import org.openstreetmap.josm.io.MirroredInputStream;
     20import org.openstreetmap.josm.io.CachedFile;
    2121import org.openstreetmap.josm.tools.ImageProvider;
    2222import org.openstreetmap.josm.tools.Utils;
     
    8282
    8383    /**
    84      * Returns a new {@code MirroredInputStream} to the local file containing style source (can be a text file or an archive).
    85      * @return A new {@code MirroredInputStream} to the local file containing style source
     84     * Returns a new {@code CachedFile} to the local file containing style source (can be a text file or an archive).
     85     * @return A new {@code CachedFile} to the local file containing style source
    8686     * @throws IOException if any I/O error occurs.
    8787     * @since 7081
    8888     */
    89     public abstract MirroredInputStream getMirroredInputStream() throws IOException;
     89    public abstract CachedFile getCachedFile() throws IOException;
    9090
    9191    /**
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSStyleSource.java

    r7200 r7248  
    4242import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.TokenMgrError;
    4343import org.openstreetmap.josm.gui.preferences.SourceEntry;
    44 import org.openstreetmap.josm.io.MirroredInputStream;
     44import org.openstreetmap.josm.io.CachedFile;
    4545import org.openstreetmap.josm.tools.CheckParameterUtil;
    4646import org.openstreetmap.josm.tools.LanguageInfo;
     
    275275            return new ByteArrayInputStream(css.getBytes(StandardCharsets.UTF_8));
    276276        }
    277         MirroredInputStream in = getMirroredInputStream();
     277        CachedFile cf = getCachedFile();
    278278        if (isZip) {
    279             File file = in.getFile();
    280             Utils.close(in);
     279            File file = cf.getFile();
    281280            zipFile = new ZipFile(file, StandardCharsets.UTF_8);
    282281            zipIcons = file;
     
    286285            zipFile = null;
    287286            zipIcons = null;
    288             return in;
    289         }
    290     }
    291 
    292     @Override
    293     public MirroredInputStream getMirroredInputStream() throws IOException {
    294         return new MirroredInputStream(url, null, MAPCSS_STYLE_MIME_TYPES);
     287            return cf.getInputStream();
     288        }
     289    }
     290
     291    @Override
     292    public CachedFile getCachedFile() throws IOException {
     293        return new CachedFile(url).setHttpAccept(MAPCSS_STYLE_MIME_TYPES);
    295294    }
    296295
  • trunk/src/org/openstreetmap/josm/gui/mappaint/xml/XmlStyleSource.java

    r7083 r7248  
    2929import org.openstreetmap.josm.gui.mappaint.StyleSource;
    3030import org.openstreetmap.josm.gui.preferences.SourceEntry;
    31 import org.openstreetmap.josm.io.MirroredInputStream;
     31import org.openstreetmap.josm.io.CachedFile;
    3232import org.openstreetmap.josm.tools.Utils;
    3333import org.openstreetmap.josm.tools.XmlObjectParser;
     
    104104    @Override
    105105    public InputStream getSourceInputStream() throws IOException {
    106         MirroredInputStream in = getMirroredInputStream();
    107         InputStream zip = in.findZipEntryInputStream("xml", "style");
     106        CachedFile cf = getCachedFile();
     107        InputStream zip = cf.findZipEntryInputStream("xml", "style");
    108108        if (zip != null) {
    109             zipIcons = in.getFile();
     109            zipIcons = cf.getFile();
    110110            return zip;
    111111        } else {
    112112            zipIcons = null;
    113             return in;
    114         }
    115     }
    116 
    117     @Override
    118     public MirroredInputStream getMirroredInputStream() throws IOException {
    119         return new MirroredInputStream(url, null, XML_STYLE_MIME_TYPES);
     113            return cf.getInputStream();
     114        }
     115    }
     116
     117    @Override
     118    public CachedFile getCachedFile() throws IOException {
     119        return new CachedFile(url).setHttpAccept(XML_STYLE_MIME_TYPES);
    120120    }
    121121
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r7092 r7248  
    2222import java.io.File;
    2323import java.io.IOException;
     24import java.io.InputStream;
    2425import java.io.InputStreamReader;
    2526import java.net.MalformedURLException;
     
    8990import org.openstreetmap.josm.gui.widgets.JFileChooserManager;
    9091import org.openstreetmap.josm.gui.widgets.JosmTextField;
    91 import org.openstreetmap.josm.io.MirroredInputStream;
     92import org.openstreetmap.josm.io.CachedFile;
    9293import org.openstreetmap.josm.io.OsmTransferException;
    9394import org.openstreetmap.josm.tools.GBC;
     
    10441045        @Override
    10451046        public void actionPerformed(ActionEvent e) {
    1046             MirroredInputStream.cleanup(url);
     1047            CachedFile.cleanup(url);
    10471048            reloadAvailableSources(url, sourceProviders);
    10481049        }
     
    12801281                }
    12811282
    1282                 MirroredInputStream stream = new MirroredInputStream(url);
     1283                InputStream stream = new CachedFile(url).getInputStream();
    12831284                reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
    12841285
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetReader.java

    r7082 r7248  
    2525import org.openstreetmap.josm.Main;
    2626import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
    27 import org.openstreetmap.josm.io.MirroredInputStream;
     27import org.openstreetmap.josm.io.CachedFile;
    2828import org.openstreetmap.josm.tools.XmlObjectParser;
    2929import org.xml.sax.SAXException;
     
    225225    public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
    226226        Collection<TaggingPreset> tp;
     227        CachedFile cf = new CachedFile(source).setHttpAccept(PRESET_MIME_TYPES);
    227228        try (
    228             MirroredInputStream s = new MirroredInputStream(source, null, PRESET_MIME_TYPES);
    229229            // zip may be null, but Java 7 allows it: https://blogs.oracle.com/darcy/entry/project_coin_null_try_with
    230             InputStream zip = s.findZipEntryInputStream("xml", "preset")
     230            InputStream zip = cf.findZipEntryInputStream("xml", "preset")
    231231        ) {
    232232            if (zip != null) {
    233                 zipIcons = s.getFile();
    234             }
    235             try (InputStreamReader r = new InputStreamReader(zip == null ? s : zip, StandardCharsets.UTF_8)) {
     233                zipIcons = cf.getFile();
     234            }
     235            try (InputStreamReader r = new InputStreamReader(zip == null ? cf.getInputStream() : zip, StandardCharsets.UTF_8)) {
    236236                tp = readAll(new BufferedReader(r), validate);
    237237            }
Note: See TracChangeset for help on using the changeset viewer.