From 2922d2f9d841065e3816df24b25a5931b7d32dd7 Mon Sep 17 00:00:00 2001 From: Brenschede Date: Wed, 10 Jul 2019 18:50:35 +0200 Subject: [PATCH] add compression option for service calls to BRouter --- .../osmand/plus/routing/RouteProvider.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java index 8bb9e00146..d1e553dc62 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java +++ b/OsmAnd/src/net/osmand/plus/routing/RouteProvider.java @@ -4,6 +4,7 @@ package net.osmand.plus.routing; import android.content.Context; import android.os.Build; import android.os.Bundle; +import android.util.Base64; import net.osmand.Location; import net.osmand.PlatformUtil; @@ -51,6 +52,7 @@ import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URLConnection; @@ -61,6 +63,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.zip.GZIPInputStream; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; @@ -1179,6 +1182,7 @@ public class RouteProvider { bpars.putString("v", mode); bpars.putString("trackFormat", "gpx"); bpars.putString("turnInstructionFormat", "osmand"); + bpars.putString("acceptCompressedResult", "true"); OsmandApplication ctx = (OsmandApplication) params.ctx; List res = new ArrayList(); @@ -1192,11 +1196,23 @@ public class RouteProvider { String gpxMessage = brouterService.getTrackFromParams(bpars); if (gpxMessage == null) gpxMessage = "no result from brouter"; - if (!gpxMessage.startsWith("<")) { + + boolean isZ64Encoded = gpxMessage.startsWith("ejY0"); // base-64 version of "z64" + + if (!( isZ64Encoded || gpxMessage.startsWith("<") ) ) { return new RouteCalculationResult(gpxMessage); } - GPXFile gpxFile = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxMessage.getBytes("UTF-8"))); + InputStream gpxStream; + if ( isZ64Encoded ) { + ByteArrayInputStream bais = new ByteArrayInputStream( Base64.decode(gpxMessage, Base64.DEFAULT) ); + bais.read( new byte[3] ); // skip prefix + gpxStream = new GZIPInputStream( bais ); + } else { + gpxStream = new ByteArrayInputStream(gpxMessage.getBytes("UTF-8")); + } + + GPXFile gpxFile = GPXUtilities.loadGPXFile(gpxStream); dir = parseOsmAndGPXRoute(res, gpxFile, true, params.leftSide, params.mode.getDefaultSpeed());