Fixed a possible buffer memory leak in GeolocationCache on first enable

http://www.evanjones.ca/java-native-leak-bug.html
"TL;DR: Always close GZIPInputStream and GZIPOutputStream since they use
native memory via zlib."
This commit is contained in:
Rsl1122 2018-09-15 14:38:42 +03:00
parent 3ae0855ef5
commit 764a1c661c
2 changed files with 11 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import com.maxmind.geoip2.record.Country;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
@ -147,8 +148,12 @@ public class GeolocationCache implements SubSystem {
return;
}
URL downloadSite = new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz");
try (ReadableByteChannel rbc = Channels.newChannel(new GZIPInputStream(downloadSite.openStream()));
FileOutputStream fos = new FileOutputStream(getInstance().geolocationDB.getAbsoluteFile())) {
try (
InputStream in = downloadSite.openStream();
GZIPInputStream gzipIn = new GZIPInputStream(in);
ReadableByteChannel rbc = Channels.newChannel(gzipIn);
FileOutputStream fos = new FileOutputStream(getInstance().geolocationDB.getAbsoluteFile())
) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}

View File

@ -92,8 +92,10 @@ public abstract class Response {
? getContent()
: locale.replaceMatchingLanguage(getContent());
try (GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody());
ByteArrayInputStream bis = new ByteArrayInputStream(sentContent.getBytes(StandardCharsets.UTF_8))) {
try (
GZIPOutputStream out = new GZIPOutputStream(exchange.getResponseBody());
ByteArrayInputStream bis = new ByteArrayInputStream(sentContent.getBytes(StandardCharsets.UTF_8))
) {
byte[] buffer = new byte[2048];
int count;
while ((count = bis.read(buffer)) != -1) {