import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
public class Week03 {
private JFrame frame;
private final JLabel lblNewLabel = new JLabel("@ ! @ Hex Dump @ ! @");
private final JLabel lblInstructions = new JLabel("Hex Dump program that " +
"will read a file and save the data that has been converted to " +
"hexadecimal form in a new file.");
private final JLabel lblFind = new JLabel("Find the following file:");
private final JTextField txtFind = new JTextField();
private final JButton btnFind = new JButton("Find");
private final JLabel lblSave = new JLabel("Save the following file:");
private final JTextField txtSave = new JTextField();
private final JButton btnSave = new JButton("Save");
private final JButton btnDump = new JButton("DUMP");
private static final int[] HEXARR = null;;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Week03 window = new Week03();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Week03() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
txtFind.setFont(new Font("Andy", Font.PLAIN, 14));
txtFind.setBounds(10, 107, 589, 32);
txtFind.setColumns(10);
frame = new JFrame();
frame.setTitle("Belinda Frederick - Week03");
frame.setBackground(new Color(70, 130, 180));
frame.setBounds(100, 100, 715, 303);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
lblNewLabel.setForeground(new Color(46, 139, 87));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("Fine Hand", Font.BOLD, 30));
lblNewLabel.setBounds(10, 11, 679, 50);
frame.getContentPane().add(lblNewLabel);
lblInstructions.setForeground(new Color(139, 0, 0));
lblInstructions.setHorizontalAlignment(SwingConstants.CENTER);
lblInstructions.setFont(new Font("Corbel", Font.BOLD, 13));
lblInstructions.setBounds(10, 60, 679, 25);
frame.getContentPane().add(lblInstructions);
lblFind.setForeground(new Color(46, 139, 87));
lblFind.setFont(new Font("Harrington", Font.BOLD, 14));
lblFind.setBounds(10, 82, 227, 25);
frame.getContentPane().add(lblFind);
frame.getContentPane().add(txtFind);
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc = new JFileChooser();
int inFile = jfc.showOpenDialog(null);
if(inFile == JFileChooser.APPROVE_OPTION) {
txtFind.setText(
jfc.getSelectedFile().getAbsolutePath());
}
txtSave.setText(txtFind.getText() + ".dmp");
}
});
btnFind.setForeground(new Color(248, 248, 255));
btnFind.setBackground(new Color(139, 0, 0));
btnFind.setFont(new Font("Batang", Font.BOLD, 14));
btnFind.setBounds(610, 109, 79, 30);
frame.getContentPane().add(btnFind);
lblSave.setForeground(new Color(46, 139, 87));
lblSave.setFont(new Font("Harrington", Font.BOLD, 14));
lblSave.setBounds(10, 151, 227, 25);
frame.getContentPane().add(lblSave);
txtSave.setFont(new Font("Andy", Font.PLAIN, 14));
txtSave.setColumns(10);
txtSave.setBounds(10, 175, 589, 32);
frame.getContentPane().add(txtSave);
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
int inFile = jfc.showSaveDialog(null);
if(inFile == JFileChooser.APPROVE_OPTION) {
txtSave.setText(
jfc.getSelectedFile().getAbsolutePath());
}
}
});
btnSave.setForeground(new Color(248, 248, 255));
btnSave.setFont(new Font("Batang", Font.BOLD, 14));
btnSave.setBackground(new Color(139, 0, 0));
btnSave.setBounds(610, 177, 79, 30);
frame.getContentPane().add(btnSave);
btnDump.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String inStr = txtFind.getText();
String outStr = txtSave.getText();
//String hexData = null;
FileInputStream fis = null;
PrintWriter pw = null;
try {
fis = new FileInputStream(inStr);
pw = new PrintWriter(outStr);
int k;
byte[] buffer = new byte[16];
while((k = fis.read(buffer)) != -1) {
for(int i = 0; i < k; i++) {
int j = buffer[i];
if(j < 0) j += 256;
pw.print(HEXARR);
}
pw.printf("\n");
}
} catch (Exception fe) {
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e_a) {
e_a.printStackTrace();
}
} if(pw != null) {
pw.close();
}
}
JOptionPane.showMessageDialog(null, "The hex dump is " +
"complete.");
}
});
btnDump.setForeground(new Color(248, 248, 255));
btnDump.setBackground(new Color(46, 139, 87));
btnDump.setFont(new Font("Fine Hand", Font.BOLD, 18));
btnDump.setBounds(280, 222, 133, 32);
frame.getContentPane().add(btnDump);
}
public static String formatDump(int address, int[] hexArr) {
String hexStr = "";
address = hexArr.length;
for(int i = 0; i < address; i ++) {
if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
hexStr += hexArr + " ";
} else {
hexStr += "\n";
}
}
return hexStr;
/**char[] hexChar = "0123456789ABCDEF".toCharArray();
address =
8
+ 3;
int firstHex = address;
int fd = (address - 8)/4;
for(int i = fd; i < address; i++) {
if((hexArr[i] > 32) && (hexArr[i] < 127) || (hexArr[i] > 160)) {
hexStr += hexArr[i];
} else {
hexStr += ".";
}
}
int i = 0;
String hexStr = "";
StringBuffer sb = new StringBuffer(hexStr);
do {
for(int j = 0; j < hexArr.length; j++) {
i += address;
}
} while (i < hexArr.length);
int[] hexArr2 = new int[i];
return sb.toString();*/
}
public String hex2Str(int convert, int numDigit) {
StringBuilder strBldr = new StringBuilder(Integer.toString(convert, 16));
if(strBldr.length() < numDigit) {
int hex = (numDigit - strBldr.length());
for(int i = 0; i <= hex; i++) {
strBldr.insert(0, "0");
}
} else if(strBldr.length() > numDigit) {
strBldr = new StringBuilder();
for(int i = 1; i <= numDigit; i++) {
strBldr.append(".");
}
}
/**
//String str = String.format("%08x", Integer.valueOf(numDigit));
//String hexStr = "";
int num = 0;
for(int i = convert; i < convert + numDigit; i++) {
num = (convert >> 4 * (numDigit - i - 1)) & 0x0f;
//str = Integer.toString(num + 0x100, 16).substring(i);
}
return (HEXARR[num]);*/
return strBldr.toString();
}
}