forked from upstream-mirrors/OpenTracks
KmzImporter: process doc.kml as stream (instead of in memory copy).
Also simplified exception handling.
This commit is contained in:
@@ -22,10 +22,9 @@ import android.util.Log;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FilterInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -52,8 +51,6 @@ public class KmzTrackImporter {
|
|||||||
|
|
||||||
private static final List<String> KMZ_IMAGES_EXT = Arrays.asList("jpeg", "jpg", "png");
|
private static final List<String> KMZ_IMAGES_EXT = Arrays.asList("jpeg", "jpg", "png");
|
||||||
|
|
||||||
private static final int BUFFER_SIZE = 4096;
|
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -207,33 +204,17 @@ public class KmzTrackImporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Track.Id> parseKml(ZipInputStream zipInputStream) {
|
private List<Track.Id> parseKml(ZipInputStream zipInputStream) throws IOException {
|
||||||
XMLImporter kmlFileTrackImporter = new XMLImporter(new KmlFileTrackImporter(context));
|
XMLImporter kmlFileTrackImporter = new XMLImporter(new KmlFileTrackImporter(context));
|
||||||
|
|
||||||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream))) {
|
InputStream nonClosableInputStream = new FilterInputStream(zipInputStream) {
|
||||||
return kmlFileTrackImporter.importFile(byteArrayInputStream);
|
@Override
|
||||||
} catch (ImportParserException | ImportAlreadyExistsException e) {
|
public void close() {
|
||||||
throw e;
|
// SAX2 always tries close InputStreams; but that would also close our ZIP file.
|
||||||
} catch (Exception e) {
|
|
||||||
throw new ImportParserException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the kml as byte array.
|
|
||||||
*
|
|
||||||
* @param zipInputStream the zip input stream
|
|
||||||
*/
|
|
||||||
//TODO We should be able to process the stream; we are wasting memory here.
|
|
||||||
private byte[] getKml(ZipInputStream zipInputStream) throws IOException {
|
|
||||||
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
|
||||||
byte[] buffer = new byte[BUFFER_SIZE];
|
|
||||||
int count;
|
|
||||||
while ((count = zipInputStream.read(buffer)) != -1) {
|
|
||||||
byteArrayOutputStream.write(buffer, 0, count);
|
|
||||||
}
|
}
|
||||||
return byteArrayOutputStream.toByteArray();
|
};
|
||||||
}
|
|
||||||
|
return kmlFileTrackImporter.importFile(nonClosableInputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -243,6 +224,7 @@ public class KmzTrackImporter {
|
|||||||
* @param trackId the track's id which image belongs to.
|
* @param trackId the track's id which image belongs to.
|
||||||
* @param fileName the file name
|
* @param fileName the file name
|
||||||
*/
|
*/
|
||||||
|
@Deprecated //TODO Use JDK9's inputStream.transferTo() instead of manual buffer
|
||||||
private void readAndSaveImageFile(ZipInputStream zipInputStream, Track.Id trackId, String fileName) throws IOException {
|
private void readAndSaveImageFile(ZipInputStream zipInputStream, Track.Id trackId, String fileName) throws IOException {
|
||||||
if (trackId == null || fileName.equals("")) {
|
if (trackId == null || fileName.equals("")) {
|
||||||
return;
|
return;
|
||||||
@@ -252,7 +234,7 @@ public class KmzTrackImporter {
|
|||||||
File file = new File(dir, fileName);
|
File file = new File(dir, fileName);
|
||||||
|
|
||||||
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
|
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
|
||||||
byte[] buffer = new byte[BUFFER_SIZE];
|
byte[] buffer = new byte[4096];
|
||||||
int count;
|
int count;
|
||||||
while ((count = zipInputStream.read(buffer)) != -1) {
|
while ((count = zipInputStream.read(buffer)) != -1) {
|
||||||
fileOutputStream.write(buffer, 0, count);
|
fileOutputStream.write(buffer, 0, count);
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ import javax.xml.parsers.SAXParserFactory;
|
|||||||
|
|
||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses SAX2 to parse XML files.
|
||||||
|
* <p>
|
||||||
|
* NOTE: SAX2 always closes InputStreams after processing.
|
||||||
|
*/
|
||||||
public class XMLImporter {
|
public class XMLImporter {
|
||||||
|
|
||||||
private static final String TAG = XMLImporter.class.getSimpleName();
|
private static final String TAG = XMLImporter.class.getSimpleName();
|
||||||
|
|||||||
Reference in New Issue
Block a user