Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIMOB-16185-Use GZipInputStream if the cached content is gzip #5985

Merged
merged 2 commits into from
Oct 10, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;

import org.apache.commons.codec.digest.DigestUtils;
import org.appcelerator.kroll.common.Log;
Expand Down Expand Up @@ -237,9 +238,25 @@ public static InputStream openCachedStream(URI uri)
return null;
}
try {
boolean isGZip = false;
// Read in the headers
try {
Map<String, List<String>> headers = readHeaders(hFile);
String contentEncoding = getHeader(headers, "content-encoding");
if ("gzip".equalsIgnoreCase(contentEncoding)) {
isGZip = true;
}
} catch (IOException e) {
// continue with file read?
}
if (isGZip) {
return new GZIPInputStream(new FileInputStream(bFile));
}
return new FileInputStream(bFile);
} catch (FileNotFoundException e) {
// Fallback to URL download.
// Fallback to URL download?
return null;
} catch (IOException e) {
return null;
}

Expand Down Expand Up @@ -301,6 +318,18 @@ public CacheResponse get(URI uri, String rqstMethod,
return null;
}

// Read in the headers
Map<String, List<String>> headers = readHeaders(hFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it throws IOException?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NV.


// Update the access log
hFile.setLastModified(System.currentTimeMillis());

// Respond with the cache
return new TiCacheResponse(headers, new FileInputStream(bFile));
}

private static Map<String, List<String>> readHeaders(File hFile) throws IOException
{
// Read in the headers
Map<String, List<String>> headers = new HashMap<String, List<String>>();
BufferedReader rdr = new BufferedReader(new FileReader(hFile), 1024);
Expand All @@ -312,15 +341,10 @@ public CacheResponse get(URI uri, String rqstMethod,
headers.get(keyval[0]).add(keyval[1]);
}
rdr.close();

// Update the access log
hFile.setLastModified(System.currentTimeMillis());

// Respond with the cache
return new TiCacheResponse(headers, new FileInputStream(bFile));
return headers;
}

protected String getHeader(Map<String, List<String>> headers, String header)
protected static String getHeader(Map<String, List<String>> headers, String header)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you change it to static method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am calling getHeader from the static function openCachedStream also.

{
List<String> values = headers.get(header);
if (values == null || values.size() == 0) {
Expand Down