Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed Jun 5, 2024
1 parent e346a43 commit dabef16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 42 deletions.
39 changes: 1 addition & 38 deletions src/main/java/gov/nasa/pds/tools/util/ImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

Expand All @@ -16,25 +14,16 @@

public class ImageUtil {
private static final Logger LOG = LoggerFactory.getLogger(ImageUtil.class);
private URL target = null;

private static int JPEG_FIRST_SHORT = 0xffd8;
private static int JPEG_LAST_SHORT = 0xffd9;

private static int PNG_SIGNATURE_FIRST_INT = 0x89504e47;
private static int PNG_SIGNATURE_SECOND_INT = 0x0d0a1a0a;

public ImageUtil(URL target) {
this.target = target;
public ImageUtil() {
}

/**
* Returns the URL of the target.
*
*/
public URL getTarget() {
return (this.target);
}

/**
* Check if a JPEG file is valid by inspecting the first 2 bytes for 0xffd8 and last 2 bytes for
Expand All @@ -49,19 +38,6 @@ public boolean isJPEG(String jpegBase, URL parentURL) throws Exception {

LOG.debug("isJPEG:jpegBase {}", jpegBase);

// Get the location of the input file.
URI uri = null;
try {
uri = getTarget().toURI();
} catch (URISyntaxException e) {
// Should never happen
// but if it does, print an error message.
// Observed in DEV that if the file name contains spaces, the getURI() results
// in the URISyntaxException exception.
LOG.error("isJPEG:Cannot build URI for target {}", getTarget());
throw new Exception("isJPEG:Cannot build URI for target [" + getTarget() + "]");
}

// Note: The function FilenameUtils.getPath() doesn't seem to work correctly.
// It returns the path without the leading slash '/':
//
Expand Down Expand Up @@ -140,19 +116,6 @@ public boolean isPNG(String pngBase, URL parentURL) throws Exception {

LOG.debug("isPNG:pngBase {}", pngBase);

// Get the location of the input file.
URI uri = null;
try {
uri = getTarget().toURI();
} catch (URISyntaxException e) {
// Should never happen
// but if it does, print an error message.
// Observed in DEV that if the file name contains spaces, the getURI() results
// in the URISyntaxException exception.
LOG.error("isPNG:Cannot build URI for target {}", getTarget());
throw new Exception("isPNG:Cannot build URI for target [" + getTarget() + "]");
}

// Get the parent from parentURL instead of using "new
// File(uri.getPath()).getParent()" which will only get the top level directory.
// It would be a bug if the file is in a sub directory of the top level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,11 @@ private void handleJPEG(ValidationTarget target, URL fileRef, TinyNodeImpl fileO

if (this.imageUtil == null) {
// Save imageUtil so it can be reused.
this.imageUtil = new ImageUtil(fileRef);
this.imageUtil = new ImageUtil();
}

jpegValidateFlag = this.imageUtil.isJPEG(jpegName, new URL(parent, directory));
jpegValidateFlag = this.imageUtil.isJPEG(jpegName, new URL(parent,
directory.endsWith("/") ? directory : (directory + "/")));

// Report a warning if the JPEG file is not compliant.
if (!jpegValidateFlag) {
Expand Down Expand Up @@ -863,10 +864,11 @@ private void handlePNG(ValidationTarget target, URL fileRef, TinyNodeImpl fileOb

if (this.imageUtil == null) {
// Save imageUtil so it can be reused.
this.imageUtil = new ImageUtil(fileRef);
this.imageUtil = new ImageUtil();
}

validateFlag = this.imageUtil.isPNG(pngName, new URL(parent, directory));
validateFlag = this.imageUtil.isPNG(pngName, new URL(parent,
directory.endsWith("/") ? directory : (directory + "/")));

// Report a warning if the PNG file is not compliant.
if (!validateFlag) {
Expand Down

0 comments on commit dabef16

Please sign in to comment.