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

Now ensures all opened streams are closed #662

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions .ci.settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<settings>

<servers>
<server>
<id>ossrh</id>
<username>${env.SONATYPE_USERNAME}</username>
<password>${env.SONATYPE_PASSWORD}</password>
</server>
</servers>

</settings>
36 changes: 36 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: 'CI Build'

on:
workflow_dispatch:
push:
branches:
- master
pull_request:
types: [ opened, reopened, edited ]
branches:
- master

concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: false

jobs:
compile-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Java and Maven
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '8'
cache: 'maven'
cache-dependency-path: 'pom.xml'

- name: Compile
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V

- name: Run tests
run: mvn test -B
44 changes: 44 additions & 0 deletions .github/workflows/ci-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: 'CI Publish to Maven Central'

on:
workflow_dispatch:
push:
tags:
- 'release/*'

concurrency:
group: '${{ github.workflow }} @ ${{ github.head_ref || github.ref }}'
cancel-in-progress: false

jobs:
compile-test-and-publish-to-maven-central:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Java and Maven
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '8'
cache: 'maven'
cache-dependency-path: 'pom.xml'

- name: Compile
run: mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V

- name: Run tests
run: mvn test -B

- name: Copy publishing-config settings.xml into place
run: cp .ci.settings.xml $HOME/.m2/settings.xml

- name: Publish to Maven Central
run: ./publish.sh
env:
SONATYPE_GPGKEY_FILE_ENCRYPTION_KEY: ${{ secrets.SONATYPE_GPGKEY_FILE_ENCRYPTION_KEY }}
SONATYPE_GPGKEY_FILE_ENCRYPTION_IV: ${{ secrets.SONATYPE_GPGKEY_FILE_ENCRYPTION_IV }}
SONATYPE_GPGKEY_PASSPHRASE: ${{ secrets.SONATYPE_GPGKEY_PASSPHRASE }}
SONATYPE_USERNAME: ${{ vars.SONATYPE_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
36 changes: 0 additions & 36 deletions .github/workflows/maven.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
build
.DS_Store
.gradle
/metadata-extractor.iml
16 changes: 9 additions & 7 deletions Source/com/drew/imaging/ImageMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ public static Metadata readMetadata(@NotNull final InputStream inputStream) thro
@NotNull
public static Metadata readMetadata(@NotNull final InputStream inputStream, final long streamLength) throws ImageProcessingException, IOException
{
BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream
? (BufferedInputStream)inputStream
: new BufferedInputStream(inputStream);
try (BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream
? (BufferedInputStream)inputStream : new BufferedInputStream(inputStream)) {

FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream);
FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream);

Metadata metadata = readMetadata(bufferedInputStream, streamLength, fileType);
Metadata metadata = readMetadata(bufferedInputStream, streamLength, fileType);

metadata.addDirectory(new FileTypeDirectory(fileType));
metadata.addDirectory(new FileTypeDirectory(fileType));

return metadata;

}

return metadata;
}

/**
Expand Down
15 changes: 8 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,9 @@ 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 (ByteArrayInputStream bais = new ByteArrayInputStream(compressedProfile);
InflaterInputStream inflateStream = new InflaterInputStream(bais)) {
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 +221,9 @@ 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 (ByteArrayInputStream bais = new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft);
InflaterInputStream inflateStream = new InflaterInputStream(bais)) {
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 +266,9 @@ 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 (ByteArrayInputStream bais = new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft);
InflaterInputStream inflateStream = new InflaterInputStream(bais)) {
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
17 changes: 9 additions & 8 deletions Source/com/drew/lang/StreamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ public final class StreamUtil
{
public static byte[] readAllBytes(InputStream stream) throws IOException
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = stream.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
}

byte[] buffer = new byte[1024];
while (true) {
int bytesRead = stream.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
return outputStream.toByteArray();
}

return outputStream.toByteArray();
}
}
15 changes: 9 additions & 6 deletions Source/com/drew/lang/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ public static <T extends CharSequence> String join(@NotNull T[] strings, @NotNul
@NotNull
public static String fromStream(@NotNull InputStream stream) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
try (InputStreamReader inputStreamReader = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
return sb.toString();
}


public static int compare(@Nullable String s1, @Nullable String s2)
{
boolean null1 = s1 == null;
Expand Down
98 changes: 51 additions & 47 deletions Source/com/drew/metadata/eps/EpsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,22 @@ private static void extractXmpData(@NotNull final Metadata metadata, @NotNull Se
*/
private static byte[] readUntil(@NotNull SequentialReader reader, @NotNull byte[] sentinel) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();

final int length = sentinel.length;
int depth = 0;

while (depth != length) {
byte b = reader.getByte();
if (b == sentinel[depth])
depth++;
else
depth = 0;
bytes.write(b);
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
final int length = sentinel.length;
int depth = 0;

while (depth != length) {
byte b = reader.getByte();
if (b == sentinel[depth])
depth++;
else
depth = 0;
bytes.write(b);
}

return bytes.toByteArray();
}

return bytes.toByteArray();
}

/**
Expand All @@ -303,48 +304,48 @@ private static byte[] readUntil(@NotNull SequentialReader reader, @NotNull byte[
@Nullable
private static byte[] decodeHexCommentBlock(@NotNull SequentialReader reader) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {

// Use a state machine to efficiently parse data in a single traversal
// Use a state machine to efficiently parse data in a single traversal

final int AwaitingPercent = 0;
final int AwaitingSpace = 1;
final int AwaitingHex1 = 2;
final int AwaitingHex2 = 3;
final int AwaitingPercent = 0;
final int AwaitingSpace = 1;
final int AwaitingHex1 = 2;
final int AwaitingHex2 = 3;

int state = AwaitingPercent;
int state = AwaitingPercent;

int carry = 0;
boolean done = false;
int carry = 0;
boolean done = false;

byte b = 0;
while (!done) {
b = reader.getByte();
byte b = 0;
while (!done) {
b = reader.getByte();

switch (state) {
switch (state) {
case AwaitingPercent: {
switch (b) {
case '\r':
case '\n':
case ' ':
// skip newline chars and spaces
break;
case '%':
state = AwaitingSpace;
break;
default:
return null;
case '\r':
case '\n':
case ' ':
// skip newline chars and spaces
break;
case '%':
state = AwaitingSpace;
break;
default:
return null;
}
break;
}
case AwaitingSpace: {
switch (b) {
case ' ':
state = AwaitingHex1;
break;
default:
done = true;
break;
case ' ':
state = AwaitingHex1;
break;
default:
done = true;
break;
}
break;
}
Expand All @@ -368,14 +369,17 @@ private static byte[] decodeHexCommentBlock(@NotNull SequentialReader reader) th
state = AwaitingHex1;
break;
}
}
}
}

// skip through the remainder of the last line
while (b != '\n')
b = reader.getByte();
// skip through the remainder of the last line
while (b != '\n')
b = reader.getByte();

return bytes.toByteArray();

}

return bytes.toByteArray();
}

/**
Expand Down
Loading