Changeset 6716 in josm


Ignore:
Timestamp:
2014-01-17T21:23:07+01:00 (6 years ago)
Author:
simon04
Message:

Allow to read/write .gpx.bz2 files, refactor compression handling

Location:
trunk/src/org/openstreetmap/josm/io
Files:
1 added
11 edited

Legend:

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

    r6643 r6716  
    1010import java.awt.event.KeyEvent;
    1111import java.io.File;
    12 import java.io.FileOutputStream;
    1312import java.io.IOException;
     13import java.io.OutputStream;
    1414import java.text.MessageFormat;
    1515import java.util.Calendar;
     
    173173        }
    174174
    175         FileOutputStream fo = null;
     175        OutputStream fo = null;
    176176        try {
    177             fo = new FileOutputStream(file);
     177            fo = Compression.getCompressedFileOutputStream(file);
    178178            new GpxWriter(fo).write(gpxData);
    179179            fo.flush();
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r6643 r6716  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.IOException;
    98import java.io.InputStream;
    10 import java.util.zip.GZIPInputStream;
    119
    1210import javax.swing.JOptionPane;
     
    3129     */
    3230    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
    33             "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
     31            "gpx,gpx.gz,gpx.bz2", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz, *.gpx.bz2)");
    3432
    3533    /**
     
    7876    @Override
    7977    public void importData(File file, ProgressMonitor progressMonitor) throws IOException {
    80         InputStream is;
    81         if (file.getName().endsWith(".gpx.gz")) {
    82             is = new GZIPInputStream(new FileInputStream(file));
    83         } else {
    84             is = new FileInputStream(file);
    85         }
    86         String fileName = file.getName();
     78        final InputStream is = Compression.getUncompressedFileInputStream(file);
     79        final String fileName = file.getName();
    8780
    8881        try {
  • trunk/src/org/openstreetmap/josm/io/OsmBzip2Exporter.java

    r6289 r6716  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io;
    3 
    4 import java.io.File;
    5 import java.io.FileNotFoundException;
    6 import java.io.FileOutputStream;
    7 import java.io.IOException;
    8 import java.io.OutputStream;
    9 
    10 import org.apache.tools.bzip2.CBZip2OutputStream;
    11 import org.openstreetmap.josm.tools.Utils;
    123
    134public class OsmBzip2Exporter extends OsmExporter {
     
    2011    }
    2112
    22     @Override
    23     protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
    24         OutputStream out = new FileOutputStream(file);
    25         try {
    26             out.write('B');
    27             out.write('Z');
    28             return new CBZip2OutputStream(out);
    29         } catch (IOException e) {
    30             Utils.close(out);
    31             throw e;
    32         }
    33     }
     13    // compression handling is preformed in super-class
     14
    3415}
  • trunk/src/org/openstreetmap/josm/io/OsmBzip2Importer.java

    r6070 r6716  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.File;
    7 import java.io.FileInputStream;
    8 import java.io.IOException;
    9 
    106import org.openstreetmap.josm.actions.ExtensionFileFilter;
    11 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    127
    138public class OsmBzip2Importer extends OsmImporter {
     
    2015    }
    2116
    22     @Override
    23     public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    24         importData(getBZip2InputStream(new FileInputStream(file)), file, progressMonitor);
    25     }
     17    // compression handling is preformed in super-class
     18
    2619}
  • trunk/src/org/openstreetmap/josm/io/OsmChangeImporter.java

    r6643 r6716  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.FileNotFoundException;
    98import java.io.IOException;
     
    3837    @Override public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    3938        try {
    40             FileInputStream in = new FileInputStream(file);
    41 
    42             if (file.getName().endsWith(".osc")) {
    43                 importData(in, file, progressMonitor);
    44             } else if (file.getName().endsWith(".gz")) {
    45                 importData(getGZipInputStream(in), file, progressMonitor);
    46             } else {
    47                 importData(getBZip2InputStream(in), file, progressMonitor);
    48             }
    49 
     39            importData(Compression.getUncompressedFileInputStream(file), file, progressMonitor);
    5040        } catch (FileNotFoundException e) {
    5141            Main.error(e);
  • trunk/src/org/openstreetmap/josm/io/OsmExporter.java

    r6643 r6716  
    5353
    5454    protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
    55         return new FileOutputStream(file);
     55        return Compression.getCompressedFileOutputStream(file);
    5656    }
    5757
  • trunk/src/org/openstreetmap/josm/io/OsmGzipExporter.java

    r5361 r6716  
    22package org.openstreetmap.josm.io;
    33
    4 import java.io.File;
    5 import java.io.FileNotFoundException;
    6 import java.io.FileOutputStream;
    7 import java.io.IOException;
    8 import java.io.OutputStream;
    9 import java.util.zip.GZIPOutputStream;
    104public class OsmGzipExporter extends OsmExporter {
    115
     
    148    }
    159
    16     @Override
    17     protected OutputStream getOutputStream(File file) throws FileNotFoundException, IOException {
    18         OutputStream out = new FileOutputStream(file);
    19         return new GZIPOutputStream(out);
    20     }
     10    // compression handling is preformed in super-class
     11
    2112}
  • trunk/src/org/openstreetmap/josm/io/OsmGzipImporter.java

    r6070 r6716  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.File;
    7 import java.io.FileInputStream;
    8 import java.io.IOException;
    9 
    106import org.openstreetmap.josm.actions.ExtensionFileFilter;
    11 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    127
    138public class OsmGzipImporter extends OsmImporter {
     
    2015    }
    2116
    22     @Override
    23     public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    24         importData(getGZipInputStream(new FileInputStream(file)), file, progressMonitor);
    25     }
     17    // compression handling is preformed in super-class
     18
    2619}
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r6643 r6716  
    55
    66import java.io.File;
    7 import java.io.FileInputStream;
    87import java.io.FileNotFoundException;
    98import java.io.IOException;
     
    6059    @Override
    6160    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    62         FileInputStream in = null;
     61        InputStream in = null;
    6362        try {
    64             in = new FileInputStream(file);
     63            in = Compression.getUncompressedFileInputStream(file);
    6564            importData(in, file, progressMonitor);
    6665        } catch (FileNotFoundException e) {
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r6244 r6716  
    3838            this.progressMonitor = progressMonitor;
    3939            this.compression = compression;
    40         }
    41        
    42         protected final InputStream getUncompressedInputStream() throws IOException {
    43             switch (compression) {
    44                 case BZIP2: return FileImporter.getBZip2InputStream(in);
    45                 case GZIP: return FileImporter.getGZipInputStream(in);
    46                 case NONE:
    47                 default: return in;
    48             }
    4940        }
    5041       
     
    121112                return null;
    122113            progressMonitor.subTask(tr("Downloading OSM data..."));
    123             return OsmReader.parseDataSet(getUncompressedInputStream(), progressMonitor.createSubTaskMonitor(1, false));
     114            return OsmReader.parseDataSet(compression.getUncompressedInputStream(in), progressMonitor.createSubTaskMonitor(1, false));
    124115        }
    125116    }
     
    136127                return null;
    137128            progressMonitor.subTask(tr("Downloading OSM data..."));
    138             return OsmChangeReader.parseDataSet(getUncompressedInputStream(), progressMonitor.createSubTaskMonitor(1, false));
     129            return OsmChangeReader.parseDataSet(compression.getUncompressedInputStream(in), progressMonitor.createSubTaskMonitor(1, false));
    139130        }
    140131    }
     
    151142                return null;
    152143            progressMonitor.subTask(tr("Downloading OSM data..."));
    153             GpxReader reader = new GpxReader(getUncompressedInputStream());
     144            GpxReader reader = new GpxReader(compression.getUncompressedInputStream(in));
    154145            gpxParsedProperly = reader.parse(false);
    155146            GpxData result = reader.getGpxData();
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r6695 r6716  
    3434    private boolean doAuthenticate = false;
    3535    protected boolean gpxParsedProperly;
    36    
    37     protected enum Compression {
    38         NONE,
    39         BZIP2,
    40         GZIP
    41     }
    4236
    4337    /**
Note: See TracChangeset for help on using the changeset viewer.