Skip to content

Commit

Permalink
#3 Use Java Default Icon instead of image
Browse files Browse the repository at this point in the history
  • Loading branch information
amosshi committed Jun 6, 2019
1 parent 8e384d4 commit 6667733
Show file tree
Hide file tree
Showing 36 changed files with 216 additions and 237 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.freeinternals.commonlib.core.FileFormat;
import org.freeinternals.biv.plugin.PluginManager;
import org.freeinternals.commonlib.ui.JBinaryViewer;
import org.freeinternals.commonlib.ui.JFrameTool;
import org.freeinternals.commonlib.ui.UITool;
import org.freeinternals.commonlib.ui.JPanelForTree;
import org.freeinternals.commonlib.ui.JTreeCellRenderer;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
Expand Down Expand Up @@ -123,6 +123,6 @@ private void treeSelectionChanged(final TreeSelectionEvent evt) {
}

private void treeDoubleClickPopup(JPanel panel, String title) {
JFrameTool.showPopup(this.topLevelFrame, panel, title);
UITool.showPopup(this.topLevelFrame, panel, title);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.freeinternals.biv.plugin.PluginManager;
import org.freeinternals.commonlib.ui.JFrameTool;
import org.freeinternals.commonlib.ui.UITool;
import org.freeinternals.format.FileFormatException;

/**
Expand All @@ -48,7 +48,7 @@ private Main() {
this.setTitle("Binary Internals Viewer " + PluginManager.getPlugedExtensions());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JFrameTool.centerJFrame(this);
UITool.centerJFrame(this);
this.createMenu();
this.filedropPanel.setBackground(Color.WHITE);
this.filedropPanel.setLayout(new BorderLayout());
Expand Down Expand Up @@ -96,7 +96,7 @@ public void actionPerformed(final ActionEvent e) {
menuFile.add(menuItem_FileOpen);

// File --> Close
final JMenuItem menuItem_FileClose = new JMenuItem("Close");
final JMenuItem menuItem_FileClose = new JMenuItem("Close", UIManager.getIcon("InternalFrame.iconifyIcon"));
menuItem_FileClose.setMnemonic(KeyEvent.VK_C);
menuItem_FileClose.addActionListener(new ActionListener() {

Expand Down Expand Up @@ -173,12 +173,10 @@ public void actionPerformed(final ActionEvent e) {
}
});
menuHelp.add(menuItem_HelpAbout);

}

private void enalbeFileDrop(){
// only the 1st file are handled
//new FileDrop(System.out, this.dropPanel, new FileDrop.Listener() {
new FileDrop(this.filedropPanel, new FileDrop.Listener() {

public void filesDropped(java.io.File[] files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2007, FreeInternals.org. All rights reserved.
* Use is subject to license terms.
*/
package org.freeinternals.commonlib.util;
package org.freeinternals.commonlib.core;

import java.io.File;
import java.io.IOException;
Expand All @@ -16,114 +16,28 @@
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;

/**
* Utility Class.
*
* @author Amos Shi
*/
public final class Tool {
public final class BytesTool {

/**
* Returns byte array from the {@code file}
*
* @param file The file
* @return Byte array of the {@code file}, or {@code null} if error
* happened.
*/
public static byte[] readFileAsBytes(final File file) {
byte[] fileBytes = null;
try {
fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
} catch (IOException ex) {
Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex);
}

return fileBytes;
}

/**
* Read byte array from {@code zipFile} of entry {@code zipEntry}
*
* @param zipFile The {@code jar} or {@code zip} file
* @param zipEntry The entry to be read
* @return Byte array of the class file, or {@code null} if error happened.
* @throws java.io.IOException Error happened when reading the zip file
*/
public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException {
if (zipFile == null) {
throw new IllegalArgumentException("Parameter 'zipFile' is null.");
}
if (zipEntry == null) {
throw new IllegalArgumentException("Parameter 'zipEntry' is null.");
}

final long fileSize = zipEntry.getSize();
final byte contents[] = new byte[(int) fileSize];
ByteBuffer byteBuf = ByteBuffer.allocate(contents.length);
InputStream is = null;
int bytesRead = 0;
int bytesAll = 0;
public static boolean isByteArrayEmpty(byte[] buff, int startPos, int length) {
boolean result = false;

try {
is = zipFile.getInputStream(zipEntry);
while (true) {
bytesRead = is.read(contents);
if (bytesRead != -1) {
byteBuf.put(contents, 0, bytesRead);
bytesAll += bytesRead;
} else {
if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) {
result = true;
for (int i = 1; i <= length; i++) {
if (buff[startPos + i] != buff[startPos]) {
result = false;
break;
}
}
} catch (IOException ex) {
Logger.getLogger(Tool.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}

if (bytesAll == fileSize) {
return byteBuf.array();
} else {
throw new IOException(String.format(
"File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s",
fileSize,
byteBuf.array().length,
zipFile.getName(),
zipEntry.getName()));
}
}

/**
* 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();
return result;
}

/**
Expand Down Expand Up @@ -195,44 +109,103 @@ public static boolean isByteArraySame(byte[] bin1, byte[] bin2, int start) {
}

/**
*
* @param parentNode
* @param lastEnd
* @param diff
* @param buff
* @param buffStartPos
* 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 void generateTreeNode_Diff(
DefaultMutableTreeNode parentNode,
int lastEnd,
int diff,
byte[] buff, int buffStartPos) {
String diffStr;

if (Tool.isBuffEmpty(buff, lastEnd - buffStartPos, diff - 1)) {
diffStr = String.format("Empty [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff);
} else {
diffStr = String.format("Unknown [0x%04X, 0x%04X] length = %d", lastEnd, lastEnd + diff - 1, diff);
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;
}
}
parentNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
lastEnd,
diff,
diffStr)));
sb.append('\n');

return sb.toString();
}

private static boolean isBuffEmpty(byte[] buff, int startPos, int length) {
boolean result = false;
/**
* Returns byte array from the {@code file}
*
* @param file The file
* @return Byte array of the {@code file}, or {@code null} if error
* happened.
*/
public static byte[] readFileAsBytes(final File file) {
byte[] fileBytes = null;
try {
fileBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
} catch (IOException ex) {
Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex);
}

if (buff[startPos] == 0x00 || buff[startPos] == ((byte) 0xFF)) {
result = true;
for (int i = 1; i <= length; i++) {
if (buff[startPos + i] != buff[startPos]) {
result = false;
return fileBytes;
}

/**
* Read byte array from {@code zipFile} of entry {@code zipEntry}
*
* @param zipFile The {@code jar} or {@code zip} file
* @param zipEntry The entry to be read
* @return Byte array of the class file, or {@code null} if error happened.
* @throws java.io.IOException Error happened when reading the zip file
*/
public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry zipEntry) throws IOException {
if (zipFile == null) {
throw new IllegalArgumentException("Parameter 'zipFile' is null.");
}
if (zipEntry == null) {
throw new IllegalArgumentException("Parameter 'zipEntry' is null.");
}

final long fileSize = zipEntry.getSize();
final byte contents[] = new byte[(int) fileSize];
ByteBuffer byteBuf = ByteBuffer.allocate(contents.length);
InputStream is = null;
int bytesRead = 0;
int bytesAll = 0;

try {
is = zipFile.getInputStream(zipEntry);
while (true) {
bytesRead = is.read(contents);
if (bytesRead != -1) {
byteBuf.put(contents, 0, bytesRead);
bytesAll += bytesRead;
} else {
break;
}
}
} catch (IOException ex) {
Logger.getLogger(BytesTool.class.getName()).log(Level.SEVERE, null, ex);
throw ex;
}

return result;
if (bytesAll == fileSize) {
return byteBuf.array();
} else {
throw new IOException(String.format(
"File read error: expected = %d bytes, result = %d bytes.\nzipFile = %s\nzipEntry = %s",
fileSize,
byteBuf.array().length,
zipFile.getName(),
zipEntry.getName()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.SortedMap;
import java.util.TreeMap;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.util.Tool;
import org.freeinternals.format.FileFormatException;

/**
Expand Down Expand Up @@ -44,7 +43,7 @@ public FileFormat(final File file) throws IOException, FileFormatException {
throw new FileFormatException(
String.format("The file content is empty.\nname = %s", file.getPath()));
}
this.fileByteArray = Tool.readFileAsBytes(file);
this.fileByteArray = BytesTool.readFileAsBytes(file);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.EOFException;
import java.io.IOException;
import java.math.BigInteger;
import org.freeinternals.commonlib.util.Tool;

/**
*
Expand Down Expand Up @@ -478,7 +477,7 @@ public int backwardTo(byte[] b) {
PosByteArrayInputStream posIn = ((PosByteArrayInputStream) this.in);
byte[] buf = posIn.getBuf();
for (int i = posIn.getPos() - b.length; i > -1; i--) {
if (Tool.isByteArraySame(b, buf, i)) {
if (BytesTool.isByteArraySame(b, buf, i)) {
result = i;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private void toolbar_CollapseAll() {
}

private void toolbar_ShowDetails() {
JFrameTool.showPopup(this.topLevelFrame, this.details_panel, this.details_title);
UITool.showPopup(this.topLevelFrame, this.details_panel, this.details_title);
}

private void tree_SelectionChanged(final TreeSelectionEvent e) {
Expand Down
Loading

0 comments on commit 6667733

Please sign in to comment.