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
15 changes: 13 additions & 2 deletions Source/com/drew/imaging/heif/HeifReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.drew.lang.SequentialReader;
import com.drew.lang.StreamReader;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.heif.HeifBoxTypes;
import com.drew.metadata.heif.HeifContainerTypes;
import com.drew.metadata.heif.HeifDirectory;
Expand All @@ -38,8 +39,15 @@ public class HeifReader
private static final Set<String> ACCEPTABLE_PRE_META_BOX_TYPES =
new HashSet<String>(Arrays.asList(HeifBoxTypes.BOX_FILE_TYPE, HeifContainerTypes.BOX_METADATA));

public void extract(InputStream inputStream, HeifHandler<?> handler)
public void extract(@NotNull InputStream inputStream, @NotNull HeifHandler<?> handler)
Comment on lines -41 to +42
Copy link
Owner

Choose a reason for hiding this comment

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

Annotations are good, thanks!

{
if (inputStream == null) {
throw new IllegalArgumentException("inputStream cannot be null");
}
if (handler == null) {
throw new IllegalArgumentException("handler cannot be null");
}
Comment on lines +44 to +49
Copy link
Owner

Choose a reason for hiding this comment

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

There are probably thousands of such places where we don't validate parameter values as non-null. I'm not sure what value this adds in practice.


// We need to read through the input stream to find the meta box which will tell us what handler to use

// The meta box is not necessarily the first box, so we need to mark the input stream (if we can)
Expand All @@ -49,7 +57,10 @@ public void extract(InputStream inputStream, HeifHandler<?> handler)
boolean markSupported = false;
if (inputStream.markSupported()) {
markSupported = true;
inputStream.mark(inputStream.available() + 1); // +1 since we're going to read past the end of the stream by 1 byte
int available = inputStream.available();
// Some InputStreams return -1 for available(), so use a reasonable default
int markReadAheadLimit = (available > 0) ? available + 1 : 8192;
inputStream.mark(markReadAheadLimit);
Comment on lines +60 to +63
Copy link
Owner

Choose a reason for hiding this comment

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

Is there some additional context on this change? In what case were you seeing this? I'd like to think through the consequences of this a bit more.

}

StreamReader reader = new StreamReader(inputStream);
Expand Down
4 changes: 4 additions & 0 deletions Source/com/drew/imaging/png/PngMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public static Metadata readMetadata(@NotNull File file) throws PngProcessingExce
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream) throws PngProcessingException, IOException
{
if (inputStream == null) {
throw new IllegalArgumentException("inputStream cannot be null");
}

Iterable<PngChunk> chunks = new PngChunkReader().extract(new StreamReader(inputStream), _desiredChunkTypes);

Metadata metadata = new Metadata();
Expand Down
65 changes: 65 additions & 0 deletions Tests/com/drew/imaging/heif/HeifReaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2002-2019 Drew Noakes and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.imaging.heif;

import com.drew.metadata.Metadata;
import com.drew.metadata.heif.HeifBoxHandler;
import org.junit.Test;

import java.io.ByteArrayInputStream;

/**
* @author Drew Noakes https://drewnoakes.com
*/
public class HeifReaderTest
{
@Test(expected = IllegalArgumentException.class)
public void testExtractWithNullInputStream()
{
HeifReader reader = new HeifReader();
Metadata metadata = new Metadata();
HeifBoxHandler handler = new HeifBoxHandler(metadata);

reader.extract(null, handler);
}

@Test(expected = IllegalArgumentException.class)
public void testExtractWithNullHandler()
{
HeifReader reader = new HeifReader();
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);

reader.extract(inputStream, null);
}

@Test
public void testExtractWithValidParameters()
{
// Should not throw any exceptions
HeifReader reader = new HeifReader();
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
Metadata metadata = new Metadata();
HeifBoxHandler handler = new HeifBoxHandler(metadata);

reader.extract(inputStream, handler);
// This should complete without throwing any exceptions
}
}