From a3ee3c15fc7878057d0e93a7e1fbde19242dc3d8 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Tue, 8 Mar 2016 11:41:20 +0100 Subject: [PATCH] Pebble: copy pebble-app-js.js out of the pbw upon installation not upon reading the .pbw This eliminates the need to copy the whole file into a byte[], and all file size limts are gone. --- .../devices/pebble/PBWInstallHandler.java | 12 +++--------- .../devices/pebble/PBWReader.java | 19 ------------------- .../gadgetbridge/util/FileUtils.java | 18 +++++++++++------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java index 9794ca8f..30becd11 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWInstallHandler.java @@ -13,6 +13,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.io.Writer; import nodomain.freeyourgadget.gadgetbridge.R; @@ -167,21 +168,14 @@ public class PBWInstallHandler implements InstallHandler { LOG.error(e.getMessage(), e); } - String jsConfigFile = mPBWReader.getJsConfigurationFile(); + InputStream jsConfigFile = mPBWReader.getInputStreamFile("pebble-js-app.js"); if (jsConfigFile != null) { outputFile = new File(destDir, app.getUUID().toString() + "_config.js"); try { - writer = new BufferedWriter(new FileWriter(outputFile)); + FileUtils.copyStreamToFile(jsConfigFile, outputFile); } catch (IOException e) { LOG.error("Failed to open output file: " + e.getMessage(), e); - return; - } - try { - writer.write(jsConfigFile); - writer.close(); - } catch (IOException e) { - LOG.error("Failed to write to output file: " + e.getMessage(), e); } } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWReader.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWReader.java index e287b328..42f05b74 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWReader.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PBWReader.java @@ -57,7 +57,6 @@ public class PBWReader { private short mAppVersion; private int mIconId; private int mFlags; - private String jsConfigurationFile = null; private JSONObject mAppKeys = null; @@ -213,20 +212,6 @@ public class PBWReader { e.printStackTrace(); break; } - } else if (fileName.equals("pebble-js-app.js")) { - LOG.info("Found JS file: app supports configuration."); - long bytes = ze.getSize(); - if (bytes > 65536) { - LOG.info("size exceeding 64k, skipping"); - continue; - } - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - while ((count = zis.read(buffer)) != -1) { - baos.write(buffer, 0, count); - } - - jsConfigurationFile = baos.toString(); } else if (fileName.equals(platformDir + "pebble-app.bin")) { zis.read(buffer, 0, 108); byte[] tmp_buf = new byte[32]; @@ -342,8 +327,4 @@ public class PBWReader { public JSONObject getAppKeysJSON() { return mAppKeys; } - - public String getJsConfigurationFile() { - return jsConfigurationFile; - } } \ No newline at end of file diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java index de9bc9dc..b354edb7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/FileUtils.java @@ -47,6 +47,16 @@ public class FileUtils { } } + public static void copyStreamToFile(InputStream inputStream, File destFile) throws IOException { + FileOutputStream fout = new FileOutputStream(destFile); + byte[] buf = new byte[4096]; + while (inputStream.available() > 0) { + int bytes = inputStream.read(buf); + fout.write(buf, 0, bytes); + } + fout.close(); + } + public static void copyURItoFile(Context ctx, Uri uri, File destFile) throws IOException { if (uri.getPath().equals(destFile.getPath())) { return; @@ -60,14 +70,8 @@ public class FileUtils { e.printStackTrace(); return; } - FileOutputStream fout = new FileOutputStream(destFile); - byte[] buf = new byte[4096]; - while (fin.available() > 0) { - int bytes = fin.read(buf); - fout.write(buf, 0, bytes); - } + copyStreamToFile(fin, destFile); fin.close(); - fout.close(); } public static String getStringFromFile(File file) throws IOException {