Skip to content
Open
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
12 changes: 5 additions & 7 deletions Source/com/drew/imaging/png/PngMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
int bytesLeft = bytes.length - (profileNameBytes.length + 1 + 1);
byte[] compressedProfile = reader.getBytes(bytesLeft);

try {
InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(compressedProfile));
try (InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(compressedProfile))) {
new IccReader().extract(new RandomAccessStreamReader(inflateStream), metadata, directory);
inflateStream.close();
} catch(java.util.zip.ZipException zex) {
directory.addError(String.format("Exception decompressing PNG iCCP chunk : %s", zex.getMessage()));
metadata.addDirectory(directory);
Expand Down Expand Up @@ -222,8 +220,8 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
int bytesLeft = bytes.length - (keywordsv.getBytes().length + 1 + 1);
byte[] textBytes = null;
if (compressionMethod == 0) {
try {
textBytes = StreamUtil.readAllBytes(new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft)));
try (InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft))) {
textBytes = StreamUtil.readAllBytes(inflateStream);
} catch(java.util.zip.ZipException zex) {
PngDirectory directory = new PngDirectory(PngChunkType.zTXt);
directory.addError(String.format("Exception decompressing PNG zTXt chunk with keyword \"%s\": %s", keyword, zex.getMessage()));
Expand Down Expand Up @@ -266,8 +264,8 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
textBytes = reader.getNullTerminatedBytes(bytesLeft, false);
} else if (compressionFlag == 1) {
if (compressionMethod == 0) {
try {
textBytes = StreamUtil.readAllBytes(new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft)));
try (InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft))) {
textBytes = StreamUtil.readAllBytes(inflateStream);
} catch(java.util.zip.ZipException zex) {
PngDirectory directory = new PngDirectory(PngChunkType.iTXt);
directory.addError(String.format("Exception decompressing PNG iTXt chunk with keyword \"%s\": %s", keyword, zex.getMessage()));
Expand Down
3 changes: 1 addition & 2 deletions Source/com/drew/metadata/exif/ExifTiffHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ public boolean customProcessTag(final int tagOffset,
byte[] jpegrawbytes = reader.getBytes(tagOffset, byteCount);

// Extract information from embedded image since it is metadata-rich
ByteArrayInputStream jpegmem = new ByteArrayInputStream(jpegrawbytes);
try {
try (ByteArrayInputStream jpegmem = new ByteArrayInputStream(jpegrawbytes)) {
Copy link
Author

Choose a reason for hiding this comment

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

I'm inclined to leave this change in because although as you point out, closing a BAIS has no effect, we already have a try block so it's zero overhead to do so (and actually saves a line of code).

But if you want me to revert this change, please say so.

Metadata jpegDirectory = JpegMetadataReader.readMetadata(jpegmem);
for (Directory directory : jpegDirectory.getDirectories()) {
directory.setParent(_currentDirectory);
Expand Down
95 changes: 46 additions & 49 deletions Source/com/drew/tools/ProcessAllImagesInFolderUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.drew.metadata.xmp.XmpDirectory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
Expand Down Expand Up @@ -75,7 +76,11 @@ public static void main(String[] args) throws IOException
printUsage();
System.exit(1);
}
log = new PrintStream(new FileOutputStream(args[++i], false), true);

try (FileOutputStream fos = new FileOutputStream(args[++i], false)) {
log = new PrintStream(fos , true);
}

} else {
// Treat this argument as a directory
directories.add(arg);
Expand Down Expand Up @@ -493,26 +498,17 @@ private static PrintWriter openWriter(@NotNull File file) throws IOException
javaDir.mkdir();

String outputPath = String.format("%s/metadata/java/%s.txt", file.getParent(), file.getName());
Writer writer = new OutputStreamWriter(
new FileOutputStream(outputPath),
"UTF-8"
);
writer.write("FILE: " + file.getName() + NEW_LINE);

// Detect file type
BufferedInputStream stream = null;
try {
stream = new BufferedInputStream(new FileInputStream(file));
try (Writer writer = new OutputStreamWriter(new FileOutputStream(outputPath), StandardCharsets.UTF_8);
BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
writer.write("FILE: " + file.getName() + NEW_LINE);

// Detect file type
FileType fileType = FileTypeDetector.detectFileType(stream, file.getName());
writer.write(String.format("TYPE: %s" + NEW_LINE, fileType.toString().toUpperCase()));
writer.write(NEW_LINE);
} finally {
if (stream != null) {
stream.close();
}
}

return new PrintWriter(writer);
return new PrintWriter(writer);
}
}

private static void closeWriter(@Nullable Writer writer) throws IOException
Expand Down Expand Up @@ -639,46 +635,47 @@ public void onScanCompleted(@NotNull PrintStream log)

private void writeOutput(@NotNull PrintStream stream) throws IOException
{
Writer writer = new OutputStreamWriter(stream);
writer.write("# Image Database Summary\n\n");
try (Writer writer = new OutputStreamWriter(stream)) {
writer.write("# Image Database Summary\n\n");

for (Map.Entry<String, List<Row>> entry : _rowListByExtension.entrySet()) {
String extension = entry.getKey();
writer.write("## " + extension.toUpperCase() + " Files\n\n");
for (Map.Entry<String, List<Row>> entry : _rowListByExtension.entrySet()) {
String extension = entry.getKey();
writer.write("## " + extension.toUpperCase() + " Files\n\n");

writer.write("File|Manufacturer|Model|Dir Count|Exif?|Makernote|Thumbnail|All Data\n");
writer.write("----|------------|-----|---------|-----|---------|---------|--------\n");
writer.write("File|Manufacturer|Model|Dir Count|Exif?|Makernote|Thumbnail|All Data\n");
writer.write("----|------------|-----|---------|-----|---------|---------|--------\n");

List<Row> rows = entry.getValue();
List<Row> rows = entry.getValue();

// Order by manufacturer, then model
Collections.sort(rows, new Comparator<Row>() {
public int compare(Row o1, Row o2)
{
int c1 = StringUtil.compare(o1.manufacturer, o2.manufacturer);
return c1 != 0 ? c1 : StringUtil.compare(o1.model, o2.model);
// Order by manufacturer, then model
Collections.sort(rows, new Comparator<Row>() {
public int compare(Row o1, Row o2)
{
int c1 = StringUtil.compare(o1.manufacturer, o2.manufacturer);
return c1 != 0 ? c1 : StringUtil.compare(o1.model, o2.model);
}
});

for (Row row : rows) {
writer.write(String.format("[%s](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/%s)|%s|%s|%d|%s|%s|%s|[metadata](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/metadata/%s.txt)\n",
row.file.getName(),
row.relativePath,
StringUtil.urlEncode(row.file.getName()),
row.manufacturer == null ? "" : row.manufacturer,
row.model == null ? "" : row.model,
row.metadata.getDirectoryCount(),
row.exifVersion == null ? "" : row.exifVersion,
row.makernote == null ? "" : row.makernote,
row.thumbnail == null ? "" : row.thumbnail,
row.relativePath,
StringUtil.urlEncode(row.file.getName()).toLowerCase()
));
}
});

for (Row row : rows) {
writer.write(String.format("[%s](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/%s)|%s|%s|%d|%s|%s|%s|[metadata](https://raw.githubusercontent.com/drewnoakes/metadata-extractor-images/master/%s/metadata/%s.txt)\n",
row.file.getName(),
row.relativePath,
StringUtil.urlEncode(row.file.getName()),
row.manufacturer == null ? "" : row.manufacturer,
row.model == null ? "" : row.model,
row.metadata.getDirectoryCount(),
row.exifVersion == null ? "" : row.exifVersion,
row.makernote == null ? "" : row.makernote,
row.thumbnail == null ? "" : row.thumbnail,
row.relativePath,
StringUtil.urlEncode(row.file.getName()).toLowerCase()
));
writer.write('\n');
}

writer.write('\n');
writer.flush();
}
writer.flush();
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/com/drew/lang/CompoundExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public void testNoInnerException() throws Exception
try {
throw new CompoundException("message", null);
} catch (CompoundException e) {
try {
PrintStream nullStream = new PrintStream(new NullOutputStream());
try (PrintStream nullStream = new PrintStream(new NullOutputStream());
PrintWriter printWriter = new PrintWriter(nullStream)) {
e.printStackTrace(nullStream);
e.printStackTrace(new PrintWriter(nullStream));
e.printStackTrace(printWriter);
} catch (Exception e1) {
fail("Exception during printStackTrace for CompoundException with no inner exception");
}
Expand Down