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

Pull requst for adding input streams closing. Please don't merge if not success #201

Merged
merged 3 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

Expand Down Expand Up @@ -46,17 +47,16 @@ public ScreenshotNegative process(ScreenshotNegative negative) {
return amendedNegative;
}

try {
BufferedImage srcImage = ImageIO.read(newInputStream(amendedNegative.getTemporaryPath()));
try (
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
InputStream imageStream = newInputStream(amendedNegative.getTemporaryPath())
) {
BufferedImage srcImage = ImageIO.read(imageStream);
BufferedImage destImage = deepCopy(srcImage);
destImage = withFilterFor(negative.getBlurLevel()).filter(srcImage, destImage);

try(ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
ImageIO.write(destImage,"png",outStream);
Files.write(negative.getTemporaryPath(),outStream.toByteArray());
}


ImageIO.write(destImage, "png", outStream);
Files.write(negative.getTemporaryPath(), outStream.toByteArray());
} catch (Throwable e) {
LOGGER.warn("Failed to blur screenshot", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -46,17 +47,19 @@ public ScreenshotNegative process(ScreenshotNegative negative) {
}

private void saveResizedScreenshotTo(Path temporaryPath) throws IOException {
BufferedImage image = ImageIO.read(Files.newInputStream(temporaryPath));
BufferedImage resizedImage;
try (InputStream images = Files.newInputStream(temporaryPath)) {
BufferedImage image = ImageIO.read(images);

Dimension imageSize = sizeOf(image);
Dimension targetSize = targetSizeInProportionTo(imageSize);
Dimension imageSize = sizeOf(image);
Dimension targetSize = targetSizeInProportionTo(imageSize);

if (imageSize.equals(targetSize)) {
return;
if (imageSize.equals(targetSize)) {
return;
}
resizedImage = resize(image, targetSize.width, targetSize.height);
}

BufferedImage resizedImage = resize(image, targetSize.width, targetSize.height);
try(OutputStream resizedImageStream = Files.newOutputStream(temporaryPath)) {
try (OutputStream resizedImageStream = Files.newOutputStream(temporaryPath)) {
ImageIO.write(resizedImage, "png", resizedImageStream);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ private Dimension getImageDimension() {
String suffix = this.getFileSuffix(screenshotFile.getPath());
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersBySuffix(suffix);
if (imageReaders.hasNext()) {

ImageReader reader = imageReaders.next();
try {
ImageInputStream stream = new FileImageInputStream(screenshotFile);
ImageInputStream stream = null;
try{
stream = new FileImageInputStream(screenshotFile);
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
Expand All @@ -66,7 +68,18 @@ private Dimension getImageDimension() {
logger.warn("Could not find the dimensions of the screenshot for " + screenshotFile);
return new Dimension(0,0);
} finally {
reader.dispose();
try {
reader.dispose();
} catch (Throwable e) {
logger.error("During reader disposing",e);
}
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
logger.error("During image stream closing",e);
}
}
} else {
throw new ScreenshotException("Could not find the dimensions of the screenshot for " + screenshotFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public FileToDownload(URL url) {
}

public byte[] asByteArray() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try(InputStream in = new BufferedInputStream(url.openStream())) {
try(
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(url.openStream())
) {
IOUtils.copy(in, out);
return out.toByteArray();
}
return out.toByteArray();
}

public String asString() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,14 @@ private static TreeSet<String> findClassesInJar(URI jarDirectory) throws Excepti

String[] split = schemeSpecificPart.split("!");
URL jar = new URL(split[0]);
ZipInputStream zip = new ZipInputStream(jar.openStream());
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
if (entry.getName().endsWith(".class")) {
String className = classNameFor(entry);
if (isNotAnInnerClass(className)) {
classes.add(className);
try(ZipInputStream zip = new ZipInputStream(jar.openStream())) {
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
if (entry.getName().endsWith(".class")) {
String className = classNameFor(entry);
if (isNotAnInnerClass(className)) {
classes.add(className);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ private Optional<Narrative> narrativeLoadedFrom(File narrativeFile, int requirem
}

private Optional<Narrative> narrativeLoadedFrom(File narrativeFile, String defaultType) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(narrativeFile), "UTF-8"));
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(narrativeFile), "UTF-8"))) {
List<String> lines = readPreambleFrom(reader);

String title = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ScreenshotDigest {

Expand All @@ -20,10 +21,12 @@ public ScreenshotDigest(EnvironmentVariables environmentVariables, BlurLevel blu
private final EnvironmentVariables environmentVariables;

public String forScreenshot(File screenshotFile) throws IOException {
return DigestUtils.md5Hex(new FileInputStream(screenshotFile))
+ "_" + blurLevel.or(BlurLevel.NONE).toString()
+ optionalWidth()
+ ".png";
try(InputStream screenshot = new FileInputStream(screenshotFile)){
return DigestUtils.md5Hex(screenshot)
+ "_" + blurLevel.or(BlurLevel.NONE).toString()
+ optionalWidth()
+ ".png";
}
}

private String optionalWidth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ private void updatePreferencesFrom(Properties... propertySets) throws IOExceptio
private Properties preferencesIn(File preferencesFile) throws IOException {
Properties preferenceProperties = new Properties();
if (preferencesFile.exists()) {
LOGGER.info("LOADING LOCAL PROPERTIES FROM {} ", preferencesFile.getAbsolutePath());
preferenceProperties.load(new FileInputStream(preferencesFile));
try(InputStream preferences = new FileInputStream(preferencesFile)) {
LOGGER.info("LOADING LOCAL PROPERTIES FROM {} ", preferencesFile.getAbsolutePath());
preferenceProperties.load(preferences);
}
}
return preferenceProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ public VersionProvider(EnvironmentVariables environmentVariables) {
public String getVersion()
{
String path = "/serenity-version.properties";
InputStream stream = getClass().getResourceAsStream(path);
if (stream == null)
return "UNKNOWN";
Properties props = new Properties();
try {
try (InputStream stream = getClass().getResourceAsStream(path)){
if (stream == null)
return "UNKNOWN";
props.load(stream);
stream.close();
return (String) props.get("application.version");
Expand Down