Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 1b2b880

Browse files
authored
Merge pull request #877 from iankchristie/gzipFatBeacon
Gzip fat beacon
2 parents fa38299 + 6bf6a1c commit 1b2b880

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

android/PhysicalWeb/app/src/main/java/org/physical_web/physicalweb/BluetoothSite.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,16 @@ private File getHtmlFile() {
228228
return new File(websiteDir, "website.html");
229229
}
230230

231+
private File getTempFile() {
232+
File websiteDir = new File(activity.getFilesDir(), "Websites");
233+
websiteDir.mkdir();
234+
return new File(websiteDir, "temp.html");
235+
}
236+
231237
private void openInChrome(File file) {
238+
if(Utils.isGzippedFile(file)) {
239+
file = Utils.gunzip(file, getTempFile());
240+
}
232241
Intent intent = new Intent(Intent.ACTION_VIEW);
233242
Uri contentUri = new FileProvider()
234243
.getUriForFile(activity, "org.physical_web.fileprovider", file);

android/PhysicalWeb/app/src/main/java/org/physical_web/physicalweb/Utils.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
import org.json.JSONException;
4747

4848
import java.io.ByteArrayOutputStream;
49+
import java.io.File;
50+
import java.io.FileInputStream;
51+
import java.io.FileOutputStream;
52+
import java.io.FileNotFoundException;
4953
import java.io.IOException;
5054
import java.io.InputStream;
5155
import java.net.URI;
@@ -56,6 +60,7 @@
5660
import java.util.List;
5761
import java.util.Map;
5862
import java.util.Set;
63+
import java.util.zip.GZIPInputStream;
5964

6065

6166

@@ -95,6 +100,7 @@ class Utils {
95100
private static final String SEPARATOR = "\0";
96101
private static Set<String> mFavoriteUrls = new HashSet<>();
97102
private static Set<String> mBlockedUrls = new HashSet<>();
103+
private static final int GZIP_SIGNATURE_LENGTH = 2;
98104

99105
// Compares PwPairs by first considering if it has been favorited
100106
// and then considering distance
@@ -1119,4 +1125,62 @@ public PwsResultBuilder setPwsTripTimeMillis(PwsResult pwsResult, long timeMilli
11191125
return this;
11201126
}
11211127
}
1128+
1129+
/**
1130+
* Determines if a file is gzipped by examining the signature of
1131+
* the file, which is the first two bytes.
1132+
* @param file to be determined if is gzipped.
1133+
* @return true If the contents of the file are gzipped otherwise false.
1134+
*/
1135+
public static boolean isGzippedFile(File file) {
1136+
InputStream input;
1137+
1138+
try {
1139+
input = new FileInputStream(file);
1140+
} catch(FileNotFoundException e) {
1141+
return false;
1142+
}
1143+
1144+
byte[] signature = new byte[GZIP_SIGNATURE_LENGTH];
1145+
1146+
try {
1147+
input.read(signature);
1148+
} catch(IOException e) {
1149+
return false;
1150+
}
1151+
1152+
return ((signature[0] == (byte) (GZIPInputStream.GZIP_MAGIC))
1153+
&& (signature[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8)));
1154+
}
1155+
1156+
1157+
/**
1158+
* Out-of-place Gunzips from src to dest.
1159+
* @param src file containing gzipped information.
1160+
* @param dest file to place decompressed information.
1161+
* @return File that has decompressed information.
1162+
*/
1163+
public static File gunzip(File src, File dest) {
1164+
1165+
byte[] buffer = new byte[1024];
1166+
1167+
try{
1168+
1169+
GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(src));
1170+
1171+
FileOutputStream out = new FileOutputStream(dest);
1172+
1173+
int len;
1174+
while ((len = gzis.read(buffer)) > 0) {
1175+
out.write(buffer, 0, len);
1176+
}
1177+
1178+
gzis.close();
1179+
out.close();
1180+
1181+
} catch(IOException ex){
1182+
ex.printStackTrace();
1183+
}
1184+
return dest;
1185+
}
11221186
}

0 commit comments

Comments
 (0)