Skip to content

Commit

Permalink
#4 Spotbugs
Browse files Browse the repository at this point in the history
  • Loading branch information
amosshi committed Nov 8, 2019
1 parent 857370d commit 6ca83c0
Show file tree
Hide file tree
Showing 61 changed files with 299 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.freeinternals.commonlib.core;

import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -24,6 +25,37 @@
*/
public final class BytesTool {

/**
* Get a string for the {@code hex} view of byte array {@code data}.
*
* @param data Byte array
* @return A string representing the {@code hex} version of {@code data}
*/
public static String getByteDataHexView(final byte[] data) {
if (data == null) {
return "";
}
if (data.length < 1) {
return "";
}

final StringBuilder sb = new StringBuilder(data.length * 5);
final int length = data.length;
int i;
int lineBreakCounter = 0;
for (i = 0; i < length; i++) {
sb.append(String.format(" %02X", data[i]));
lineBreakCounter++;
if (lineBreakCounter == 16) {
sb.append('\n');
lineBreakCounter = 0;
}
}
sb.append('\n');

return sb.toString();
}

public static boolean isByteArrayEmpty(final byte[] buff, final int startPos, final int length) {
boolean result = false;

Expand Down Expand Up @@ -108,37 +140,6 @@ public static boolean isByteArraySame(final byte[] bin1, final byte[] bin2, fina
return same;
}

/**
* Get a string for the {@code hex} view of byte array {@code data}.
*
* @param data Byte array
* @return A string representing the {@code hex} version of {@code data}
*/
public static String getByteDataHexView(final byte[] data) {
if (data == null) {
return "";
}
if (data.length < 1) {
return "";
}

final StringBuilder sb = new StringBuilder(data.length * 5);
final int length = data.length;
int i;
int lineBreakCounter = 0;
for (i = 0; i < length; i++) {
sb.append(String.format(" %02X", data[i]));
lineBreakCounter++;
if (lineBreakCounter == 16) {
sb.append('\n');
lineBreakCounter = 0;
}
}
sb.append('\n');

return sb.toString();
}

/**
* Returns byte array from the {@code file}
*
Expand Down Expand Up @@ -208,4 +209,18 @@ public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry z
}
}

public static void skip(final InputStream is, final long skip) throws IOException {
long skippedBytes = is.skip(skip);
if (skippedBytes != skip) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", skip, skippedBytes));
}
}

public static void skipBytes(final DataInput di, final int skip) throws IOException {
long skippedBytes = di.skipBytes(skip);
if (skippedBytes != skip) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", skip, skippedBytes));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ public FileComponenPlaceHolder(final PosDataInputStream posDataInputStream, int

if (length > 0) {
this.length = length;
int skippedBytes = posDataInputStream.skipBytes(length);
if (skippedBytes != length) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", length, skippedBytes));
}
BytesTool.skipBytes(posDataInputStream, length);
} else {
this.length = 0;
}
Expand Down
6 changes: 0 additions & 6 deletions FormatCLASS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>3.1.12</version>
<type>jar</type>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.freeinternals.commonlib.core.BytesTool;
import org.freeinternals.commonlib.core.PosByteArrayInputStream;
import org.freeinternals.commonlib.core.PosDataInputStream;

Expand Down Expand Up @@ -1624,10 +1625,7 @@ protected InstructionParsed parse(final int curPos, final PosDataInputStream pdi
InstructionParsed parsed = new InstructionParsed(curPos, this.code);
parsed.cpIndex = pdis.readUnsignedShort();
int nArgs = pdis.readUnsignedByte();
int skippedBytes = pdis.skipBytes(1);
if (skippedBytes != 1) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", 1, skippedBytes));
}
BytesTool.skipBytes(pdis, 1);

parsed.opCodeText = String.format("%s interface=%d, nargs=%d", this.name(), parsed.cpIndex, nArgs);
return parsed;
Expand All @@ -1641,10 +1639,9 @@ protected InstructionParsed parse(final int curPos, final PosDataInputStream pdi
protected InstructionParsed parse(final int curPos, final PosDataInputStream pdis) throws IOException {
InstructionParsed parsed = new InstructionParsed(curPos, this.code);
parsed.cpIndex = pdis.readUnsignedShort();
int skippedBytes = pdis.skipBytes(2); // Skip 2 zero bytes
if (skippedBytes != 2) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", 2, skippedBytes));
}

// Skip 2 zero bytes
BytesTool.skipBytes(pdis, 2);
parsed.opCodeText = this.name();
return parsed;
}
Expand Down Expand Up @@ -2003,10 +2000,7 @@ private void skipPad(final PosDataInputStream pdis) throws IOException {
int skip = pdis.getPos() % 4;
skip = (skip > 0) ? 4 - skip : skip;
if (skip > 0) {
int skippedBytes = pdis.skipBytes(skip);
if (skippedBytes != skip) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", skip, skippedBytes));
}
BytesTool.skipBytes(pdis, skip);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class AttributeCode extends AttributeInfo {
this.code = new byte[this.code_length.value];
int readBytes = posDataInputStream.read(this.code);
if (readBytes != this.code_length.value) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", this.code_length.value, readBytes));
throw new IOException(String.format("Failed to read %d bytes, actual bytes read %d", this.code_length.value, readBytes));
}

this.exception_table_length = new u2(posDataInputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class AttributeUnrecognized extends AttributeInfo {
this.rawData = new byte[this.attribute_length.value];
int readBytes = posDataInputStream.read(this.rawData);
if (readBytes != this.attribute_length.value) {
throw new IOException(String.format("Failed to skip %d bytes, actual bytes skipped %d", this.attribute_length.value, readBytes));
throw new IOException(String.format("Failed to read %d bytes, actual bytes red %d", this.attribute_length.value, readBytes));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.BytesTool;
import org.freeinternals.commonlib.core.FileComponent;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.GenerateTreeNode;
Expand All @@ -27,7 +28,7 @@ public class FileData extends FileComponent implements GenerateTreeNode {
this.isCompressedData = isCompressedData;

if (length > 0) {
pDis.skip(length);
BytesTool.skip(pDis, length);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.IOException;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.BytesTool;
import org.freeinternals.commonlib.core.FileComponent;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.GenerateTreeNode;
Expand Down Expand Up @@ -46,7 +47,8 @@ public class Marker extends FileComponent implements GenerateTreeNode {
if (MarkerCode.isLengthAvailable(markerCode)) {
this.marker_length = pDIS.readUnsignedShort();
super.length = this.marker_length + 2;
pDIS.skip(this.marker_length - 2);
long skip = this.marker_length - 2;
BytesTool.skip(pDIS, skip);
} else {
this.marker_length = 0;
super.length = MarkerCode.MARKER_CODE_BYTES_COUNT;
Expand All @@ -56,13 +58,13 @@ public class Marker extends FileComponent implements GenerateTreeNode {
protected void parse(final PosDataInputStream pDisMarker) throws IOException, FileFormatException {
}

protected void parseInitSkip(final PosDataInputStream pDisMarker) throws IOException{
protected void parseInitSkip(final PosDataInputStream pDisMarker) throws IOException {
// Skip the marker code and length field
pDisMarker.skip(MarkerCode.MARKER_CODE_BYTES_COUNT);
pDisMarker.skip(MarkerCode.MARKER_LENGTH_BYTES_COUNT);
long skip = MarkerCode.MARKER_CODE_BYTES_COUNT + MarkerCode.MARKER_LENGTH_BYTES_COUNT;
BytesTool.skip(pDisMarker, skip);
}

protected String parseIdentifier(final PosDataInputStream pDisMarker) throws IOException{
protected String parseIdentifier(final PosDataInputStream pDisMarker) throws IOException {

// Parse the Identifier, an '\000' ended ASCII string
final StringBuffer sb = new StringBuffer(IDENTIFIER_LENGTH_MAX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class MarkerCode {

public static final int MARKER_CODE_BYTES_COUNT = 2;
public static final int MARKER_LENGTH_BYTES_COUNT = 2;
public static final int min = 0xFF00;
public static final int MIN = 0xFF00;
public static final int TEMP = 0xFF01; // Standalone, For temporary private use in arithmetic coding
public static final int RES_min = 0xFF02; // Reserved
public static final int RES_max = 0xFFBF; // Reserved
public static final int RES_MIN = 0xFF02; // Reserved
public static final int RES_MAX = 0xFFBF; // Reserved
public static final int SOF00 = 0xFFC0; // Specified
public static final int SOF01 = 0xFFC1; // Specified
public static final int SOF02 = 0xFFC2; // Specified
Expand Down Expand Up @@ -81,7 +81,7 @@ public class MarkerCode {
public static final int JPG12 = 0xFFFC; // Reserved for JPEG extensions
public static final int JPG13 = 0xFFFD; // Reserved for JPEG extensions
public static final int COM = 0xFFFE; // Specified
public static final int max = 0xFFFF;
public static final int MAX = 0xFFFF;

/**
* The marker code value is in valid value space or not.
Expand All @@ -90,7 +90,7 @@ public class MarkerCode {
* @return <code>true</code> when in valid value space, else <code>false</code>
*/
public static boolean isValid(int code) {
return (code > min && code != max);
return (code > MIN && code != MAX);
}

/**
Expand Down Expand Up @@ -175,7 +175,7 @@ public static String getMarkerName(int markerCode) {

if (markerCode == MarkerCode.TEMP) {
name = "TEMP";
} else if (markerCode >= MarkerCode.RES_min && markerCode <= MarkerCode.RES_max) {
} else if (markerCode >= MarkerCode.RES_MIN && markerCode <= MarkerCode.RES_MAX) {
name = "RES";
} else if (markerCode >= MarkerCode.SOF00 && markerCode <= MarkerCode.COM) {
switch (markerCode) {
Expand Down Expand Up @@ -368,6 +368,9 @@ public static String getMarkerName(int markerCode) {
case MarkerCode.COM:
name = "COM";
break;
default:
name = "Un-recognized";
break;
}
}

Expand All @@ -377,11 +380,11 @@ public static String getMarkerName(int markerCode) {
public static String getMarkerDescription(int markerCode) {
String desc = "Error";

if (markerCode == MarkerCode.min) {
if (markerCode == MarkerCode.MIN) {
desc = "MIN";
} else if (markerCode == MarkerCode.TEMP) {
desc = "[<strong>Reserved markers</strong>] For temporary private use in arithmetic coding";
} else if (markerCode >= MarkerCode.RES_min && markerCode <= MarkerCode.RES_max) {
} else if (markerCode >= MarkerCode.RES_MIN && markerCode <= MarkerCode.RES_MAX) {
desc = "[<strong>Reserved markers</strong>] Reserved";
} else if (markerCode >= MarkerCode.SOF00 && markerCode <= MarkerCode.COM) {
switch (markerCode) {
Expand Down Expand Up @@ -574,8 +577,11 @@ public static String getMarkerDescription(int markerCode) {
case MarkerCode.COM:
desc = "[<strong>Other markers</strong>] Comment";
break;
default:
desc = "Un-recognized markder: " + markerCode;
break;
}
} else if (markerCode == MarkerCode.max) {
} else if (markerCode == MarkerCode.MAX) {
desc = "MAX";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import org.freeinternals.format.jpeg.ps.PhotoshopImageResource;

/**
* An APP13 marker designates a Photoshop Image Resource (PSIR) that contains IPTC metadata.
*
* @author Amos Shi
*/
// an APP13 marker designates a Photoshop Image Resource (PSIR) that contains IPTC metadata
public class Marker_APP13 extends Marker {

public static final String identifier_Photoshop = "Photoshop";
public static final String IDENTIFIER_PHOTOSHOP = "Photoshop";
private String identifier;
private PhotoshopImageResource psir;

Expand All @@ -33,7 +33,7 @@ public class Marker_APP13 extends Marker {
protected void parse(final PosDataInputStream pDisMarker) throws IOException, FileFormatException {
super.parseInitSkip(pDisMarker);
this.identifier = super.parseIdentifier(pDisMarker);
if (this.identifier.contains(Marker_APP13.identifier_Photoshop)) {
if (this.identifier.contains(Marker_APP13.IDENTIFIER_PHOTOSHOP)) {
final int lengthPhir = this.marker_length - 2 - this.identifier.length() - 1;
final byte[] bytesPhir = new byte[lengthPhir];
System.arraycopy(pDisMarker.getBuf(), 2 + 2 + this.identifier.length() + 1, bytesPhir, 0, lengthPhir);
Expand Down Expand Up @@ -69,17 +69,15 @@ public void generateTreeNode(DefaultMutableTreeNode parentNode) {
this.identifier.length() + 1,
String.format("identifier: %s", this.identifier))));

if (this.identifier.contains(Marker_APP13.identifier_Photoshop)) {
if (this.identifier.contains(Marker_APP13.IDENTIFIER_PHOTOSHOP)) {
comp = new JTreeNodeFileComponent(
this.psir.getStartPos(),
this.psir.getLength(),
"Photoshop Image Resource Block");
markerNode.add(identifierNode = new DefaultMutableTreeNode(comp));
if (this.psir != null) {
this.psir.generateTreeNode(identifierNode);
}
this.psir.generateTreeNode(identifierNode);
} else {
markerNode.add(identifierNode = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
markerNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
lastPos = lastPos + this.identifier.length() + 1,
this.startPos + this.length - lastPos,
this.identifier)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void generateTreeNode(DefaultMutableTreeNode parentNode) {

// Ri
comp = new JTreeNodeFileComponent(
lastPos = lastPos + 2,
lastPos + 2,
2,
String.format("Ri: %d", this.Ri));
comp.setDescription("Restart interval – Specifies the number of MCU in the restart interval.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class Marker_SOFnn extends Marker {

private class Component {
private static class Component {

/** Component Identifier */
@SuppressWarnings("PackageVisibleField")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class Marker_SOS extends Marker {

private class Parameter {
private static class Parameter {

/** Scan component selector */
@SuppressWarnings("PackageVisibleField")
Expand Down
Loading

0 comments on commit 6ca83c0

Please sign in to comment.