Search in sources :

Example 6 with CBZip2InputStream

use of org.apache.tools.bzip2.CBZip2InputStream in project ant by apache.

the class BUnzip2 method extract.

/**
 * Do the unbzipping.
 */
protected void extract() {
    if (srcResource.getLastModified() > dest.lastModified()) {
        log("Expanding " + srcResource.getName() + " to " + dest.getAbsolutePath());
        OutputStream out = null;
        CBZip2InputStream zIn = null;
        InputStream fis = null;
        BufferedInputStream bis = null;
        try {
            out = Files.newOutputStream(dest.toPath());
            fis = srcResource.getInputStream();
            bis = new BufferedInputStream(fis);
            int b = bis.read();
            if (b != 'B') {
                throw new BuildException("Invalid bz2 file.", getLocation());
            }
            b = bis.read();
            if (b != 'Z') {
                throw new BuildException("Invalid bz2 file.", getLocation());
            }
            zIn = new CBZip2InputStream(bis, true);
            byte[] buffer = new byte[BUFFER_SIZE];
            int count = 0;
            do {
                out.write(buffer, 0, count);
                count = zIn.read(buffer, 0, buffer.length);
            } while (count != -1);
        } catch (IOException ioe) {
            String msg = "Problem expanding bzip2 " + ioe.getMessage();
            throw new BuildException(msg, ioe, getLocation());
        } finally {
            FileUtils.close(bis);
            FileUtils.close(fis);
            FileUtils.close(out);
            FileUtils.close(zIn);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException)

Example 7 with CBZip2InputStream

use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.

the class IndexCreator method extractOsmToNodesDB.

private OsmDbCreator extractOsmToNodesDB(OsmDbAccessor accessor, File readFile, IProgress progress, IOsmStorageFilter addFilter, int additionId, int shiftId, boolean ovewriteIds, boolean generateNewIds, boolean createTables, OsmDbCreator previous) throws IOException, SQLException, XmlPullParserException {
    boolean pbfFile = false;
    InputStream stream = new BufferedInputStream(new FileInputStream(readFile), 8192 * 4);
    InputStream streamFile = stream;
    long st = System.currentTimeMillis();
    if (readFile.getName().endsWith(".bz2")) {
        // $NON-NLS-1$
        if (stream.read() != 'B' || stream.read() != 'Z') {
        // throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
        } else {
            stream = new CBZip2InputStream(stream);
        }
    } else if (readFile.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    } else if (readFile.getName().endsWith(".pbf")) {
        // $NON-NLS-1$
        pbfFile = true;
    }
    OsmBaseStorage storage = pbfFile ? new OsmBaseStoragePbf() : new OsmBaseStorage();
    storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
    if (addFilter != null) {
        storage.getFilters().add(addFilter);
    }
    storage.getFilters().add(new IOsmStorageFilter() {

        @Override
        public boolean acceptEntityToLoad(OsmBaseStorage storage, EntityId entityId, Entity entity) {
            if (indexAddressCreator != null) {
                indexAddressCreator.registerCityIfNeeded(entity);
            }
            // accept to allow db creator parse it
            return true;
        }
    });
    // 1. Loading osm file
    OsmDbCreator dbCreator = new OsmDbCreator(additionId, shiftId, ovewriteIds, generateNewIds);
    if (previous != null) {
        dbCreator.setNodeIds(previous.getNodeIds());
        dbCreator.setWayIds(previous.getWayIds());
        dbCreator.setRelationIds(previous.getRelationIds());
    }
    dbCreator.setBackwardCompatibleIds(backwardCompatibleIds);
    try {
        // $NON-NLS-1$
        setGeneralProgress(progress, "[15 / 100]");
        // $NON-NLS-1$
        progress.startTask(Messages.getString("IndexCreator.LOADING_FILE") + readFile.getAbsolutePath(), -1);
        // 1 init database to store temporary data
        dbCreator.initDatabase(osmDBdialect, accessor.getDbConn(), createTables);
        storage.getFilters().add(dbCreator);
        if (pbfFile) {
            ((OsmBaseStoragePbf) storage).parseOSMPbf(stream, progress, false);
        } else {
            storage.parseOSM(stream, progress, streamFile, false);
        }
        dbCreator.finishLoading();
        osmDBdialect.commitDatabase(accessor.getDbConn());
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File parsed : " + (System.currentTimeMillis() - st));
        }
        progress.finishTask();
        return dbCreator;
    } finally {
        if (log.isInfoEnabled()) {
            // $NON-NLS-1$
            log.info("File indexed : " + (System.currentTimeMillis() - st));
        }
    }
}
Also used : Entity(net.osmand.osm.edit.Entity) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OsmBaseStoragePbf(net.osmand.osm.io.OsmBaseStoragePbf) IOsmStorageFilter(net.osmand.osm.io.IOsmStorageFilter) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) EntityId(net.osmand.osm.edit.Entity.EntityId) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage)

Example 8 with CBZip2InputStream

use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.

the class FixBasemapRoads method parseOsmFile.

private OsmBaseStorage parseOsmFile(File read) throws FileNotFoundException, IOException, XmlPullParserException {
    OsmBaseStorage storage = new OsmBaseStorage();
    InputStream stream = new BufferedInputStream(new FileInputStream(read), 8192 * 4);
    InputStream streamFile = stream;
    if (read.getName().endsWith(".bz2")) {
        // $NON-NLS-1$
        if (stream.read() != 'B' || stream.read() != 'Z') {
        // throw new RuntimeException("The source stream must start with the characters BZ if it is to be read as a BZip2 stream."); //$NON-NLS-1$
        } else {
            stream = new CBZip2InputStream(stream);
        }
    } else if (read.getName().endsWith(".gz")) {
        // $NON-NLS-1$
        stream = new GZIPInputStream(stream);
    }
    storage.parseOSM(stream, new ConsoleProgressImplementation(), streamFile, true);
    return storage;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) OsmBaseStorage(net.osmand.osm.io.OsmBaseStorage) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) ConsoleProgressImplementation(net.osmand.impl.ConsoleProgressImplementation) FileInputStream(java.io.FileInputStream)

Example 9 with CBZip2InputStream

use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.

the class WikiDatabasePreparation method processWikipedia.

protected static void processWikipedia(final String wikiPg, String lang, Map<Long, LatLon> links, String sqliteFileName) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException, SQLException, ComponentLookupException {
    SAXParser sx = SAXParserFactory.newInstance().newSAXParser();
    InputStream streamFile = new BufferedInputStream(new FileInputStream(wikiPg), 8192 * 4);
    InputStream stream = streamFile;
    if (stream.read() != 'B' || stream.read() != 'Z') {
        throw new RuntimeException(// $NON-NLS-1$
        "The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
    }
    CBZip2InputStream zis = new CBZip2InputStream(stream);
    Reader reader = new InputStreamReader(zis, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    final WikiOsmHandler handler = new WikiOsmHandler(sx, streamFile, lang, links, new File(sqliteFileName));
    sx.parse(is, handler);
    handler.finish();
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) SAXParser(javax.xml.parsers.SAXParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 10 with CBZip2InputStream

use of org.apache.tools.bzip2.CBZip2InputStream in project OsmAnd-tools by osmandapp.

the class WikiVoyagePreparation method processWikivoyage.

protected static void processWikivoyage(final String wikiPg, String lang, String sqliteFileName, File langlinkConn) throws ParserConfigurationException, SAXException, FileNotFoundException, IOException, SQLException, ComponentLookupException {
    SAXParser sx = SAXParserFactory.newInstance().newSAXParser();
    InputStream streamFile = new BufferedInputStream(new FileInputStream(wikiPg), 8192 * 4);
    InputStream stream = streamFile;
    if (stream.read() != 'B' || stream.read() != 'Z') {
        stream.close();
        throw new RuntimeException(// $NON-NLS-1$
        "The source stream must start with the characters BZ if it is to be read as a BZip2 stream.");
    }
    CBZip2InputStream zis = new CBZip2InputStream(stream);
    Reader reader = new InputStreamReader(zis, "UTF-8");
    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");
    final WikiOsmHandler handler = new WikiOsmHandler(sx, streamFile, lang, new File(sqliteFileName), langlinkConn);
    sx.parse(is, handler);
    handler.finish();
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CBZip2InputStream(org.apache.tools.bzip2.CBZip2InputStream) SAXParser(javax.xml.parsers.SAXParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GPXFile(net.osmand.osm.util.GPXUtils.GPXFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InputStream (java.io.InputStream)12 CBZip2InputStream (org.apache.tools.bzip2.CBZip2InputStream)12 FileInputStream (java.io.FileInputStream)10 BufferedInputStream (java.io.BufferedInputStream)9 GZIPInputStream (java.util.zip.GZIPInputStream)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)4 OsmBaseStorage (net.osmand.osm.io.OsmBaseStorage)4 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OutputStream (java.io.OutputStream)2 SAXParser (javax.xml.parsers.SAXParser)2 ConsoleProgressImplementation (net.osmand.impl.ConsoleProgressImplementation)2 Entity (net.osmand.osm.edit.Entity)2 InputSource (org.xml.sax.InputSource)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 JsonStatusCorpusReader (io.anserini.document.twitter.JsonStatusCorpusReader)1 Status (io.anserini.document.twitter.Status)1