Skip to content

Commit

Permalink
ClientBundle gen should close files when done reading them
Browse files Browse the repository at this point in the history
ImageBundleBuilder and ImageResourceGenerator don't explicitly close
files when finished with them. On some machines, this results in too
many files being open for the process to open additional files,
preventing compilation from continuing.

Bug: issue 7880
Change-Id: I01aaa65173eb7967a54bc340e7f4885d91c082f7
  • Loading branch information
niloc132 authored and Gerrit Code Review committed Mar 20, 2015
1 parent 761fe26 commit 349210a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 67 deletions.
63 changes: 4 additions & 59 deletions dev/core/src/com/google/gwt/util/tools/Utility.java
Expand Up @@ -24,10 +24,7 @@
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.Writer;
import java.net.Socket;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.util.Iterator; import java.util.Iterator;
Expand Down Expand Up @@ -58,64 +55,12 @@ public final class Utility {
* Helper that ignores exceptions during close, because what are you going to * Helper that ignores exceptions during close, because what are you going to
* do? * do?
*/ */
public static void close(InputStream is) { public static void close(AutoCloseable closeable) {
try { try {
if (is != null) { if (closeable != null) {
is.close(); closeable.close();
} }
} catch (IOException e) { } catch (Exception e) {
}
}

/**
* Helper that ignores exceptions during close, because what are you going to
* do?
*/
public static void close(OutputStream os) {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
}
}

/**
* Helper that ignores exceptions during close, because what are you going to
* do?
*/
public static void close(Reader reader) {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}

/**
* Helper that ignores exceptions during close, because what are you going to
* do?
*/
public static void close(Socket socket) {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
}
}

/**
* Helper that ignores exceptions during close, because what are you going to
* do?
*/
public static void close(Writer writer) {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
} }
} }


Expand Down
10 changes: 5 additions & 5 deletions user/src/com/google/gwt/resources/rg/ImageBundleBuilder.java
Expand Up @@ -31,6 +31,7 @@
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
Expand Down Expand Up @@ -693,17 +694,16 @@ private ImageRect addImage(TreeLogger logger, String imageName, URL imageUrl)
// Be safe by default and assume that the incoming image is lossy // Be safe by default and assume that the incoming image is lossy
boolean lossy = true; boolean lossy = true;
// Load the image // Load the image
try { try (InputStream is = imageUrl.openStream();
MemoryCacheImageInputStream imageInputStream = new MemoryCacheImageInputStream(is)) {
/* /*
* ImageIO uses an SPI pattern API. We don't care about the particulars of * ImageIO uses an SPI pattern API. We don't care about the particulars of
* the implementation, so just choose the first ImageReader. * the implementation, so just choose the first ImageReader.
*/ */
MemoryCacheImageInputStream input = new MemoryCacheImageInputStream( Iterator<ImageReader> it = ImageIO.getImageReaders(imageInputStream);
imageUrl.openStream());
Iterator<ImageReader> it = ImageIO.getImageReaders(input);
readers : while (it.hasNext()) { readers : while (it.hasNext()) {
ImageReader reader = it.next(); ImageReader reader = it.next();
reader.setInput(input); reader.setInput(imageInputStream);


int numImages = reader.getNumImages(true); int numImages = reader.getNumImages(true);
if (numImages == 0) { if (numImages == 0) {
Expand Down
20 changes: 17 additions & 3 deletions user/src/com/google/gwt/resources/rg/ImageResourceGenerator.java
Expand Up @@ -38,11 +38,13 @@
import com.google.gwt.safehtml.shared.UriUtils; import com.google.gwt.safehtml.shared.UriUtils;
import com.google.gwt.user.rebind.SourceWriter; import com.google.gwt.user.rebind.SourceWriter;
import com.google.gwt.user.rebind.StringSourceWriter; import com.google.gwt.user.rebind.StringSourceWriter;
import com.google.gwt.util.tools.Utility;


import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -577,12 +579,11 @@ public void prepare(TreeLogger logger, ResourceContext context,
* they actually offer a space-savings. * they actually offer a space-savings.
*/ */
try { try {
URL contentLocation = localizedImage.getUrl(); int originalSize = getContentLength(localizedImage.getUrl());
int originalSize = contentLocation.openConnection().getContentLength();


// Re-encode the data // Re-encode the data
URL reencodedContents = reencodeToTempFile(logger, rect); URL reencodedContents = reencodeToTempFile(logger, rect);
int newSize = reencodedContents.openConnection().getContentLength(); int newSize = getContentLength(reencodedContents);


// But only use it if we did a better job on compression // But only use it if we did a better job on compression
if (newSize < originalSize) { if (newSize < originalSize) {
Expand All @@ -608,6 +609,19 @@ public void prepare(TreeLogger logger, ResourceContext context,
} }
} }


/**
* Helper method to read the contentLength of a given URL, automatically
* closing the InputStream that is opened as a side effect.
*/
private int getContentLength(URL url) throws IOException {
URLConnection conn = url.openConnection();
try {
return conn.getContentLength();
} finally {
Utility.close(conn.getInputStream());
}
}

/** /**
* Creates a cache key to be used with {@link ResourceContext#putCachedData}. * Creates a cache key to be used with {@link ResourceContext#putCachedData}.
* The key is based on the ClientBundle type, support for data URLs, and the * The key is based on the ClientBundle type, support for data URLs, and the
Expand Down

0 comments on commit 349210a

Please sign in to comment.