4 Replies - 467 Views - Last Post: 22 April 2009 - 06:55 AM Rate Topic: -----

#1 dr3m0ra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 09

Memory Problem With HexEdit Style Program

Posted 20 April 2009 - 05:00 PM

Hello, we are having memory problems with this program. We are supposed to create a hexedit style program which displays the hexadecimal numbers of a file. We were able to open the program and display the header. However, when we tried to make it display all the numbers, the program freezes for a while, and then displays a memory error. This program should work in theory, but it seems to be inefficient and sucks up all the memory. Any help would be greatly appreciated!

/****Bitmap to Hex**********************************************
 * NAME: Shuai Yuan, Peter Oliver, Arthur Yanthar
 *
 * COURSE: ICS4M
 *
 * FILE: BitmapHex.java
 *
 * VERSION: 2009/04/20 v0.1.1
 *
 * CONVENTIONS:
 * All JLabel components or add-ons start with jlb*
 * All JPanel components or add-ons start with jp*
 * All JMenu components or add-ons start with jm*
 * All JMenuItem components or add-ons start with jmi*
 * All JButton components or add-ons start with jbn*
 * All JTextField components or add-ons start with jtf*
 * All JTextArea componenets or add-ons start with jta*
 * All GridLayout components or add-ons start with gl*
 *
 * HISTORY:
 * 2009.04.15 - Creation
 * 2009.04.16 - Remodeling of GUI
 * 2009.04.17 - Added header interpretation
 *			  - Fixed non existant file bug
 * 2009.04.20 - Added reading of complete file(bugged)
 *
 *************************************************************/
 
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

public class BitmapHex implements ActionListener{
	
	private JFrame jf = new JFrame("Bitmap HexEditor by Team Max Power");
	private Container c = jf.getContentPane();
	private JPanel jp1 = new JPanel();
	private GridLayout gl1 = new GridLayout(2,1);
	private JTextArea jtaOutput1 = new JTextArea("Hex editing: READY!");
	private JTextArea jtaOutput2 = new JTextArea("");
	private ArrayList<Byte> rawData = new ArrayList<Byte>();
	private String[] headerData;
	private JScrollPane jtaScroll1 = new JScrollPane (jtaOutput1,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	private JScrollPane jtaScroll2 = new JScrollPane (jtaOutput2,
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	private JMenu jmFile, jmHelp;
	private JMenuItem jmiExit, jmiOpen, jmiExport, jmiAbout;
	private String userFileName, buffer;
	private int fileSize, headerSize, fileWidth, fileLength, bitDepth;
	private Object[] temp = new Object[1];
	private Font normalFont = new Font(null, 0, 15);
	private boolean fileExist = true;
	
	public BitmapHex(){
		
		/*Java Menu*/
		jmFile = new JMenu("File");
		jmiOpen = new JMenuItem("Open");
		jmiOpen.addActionListener(this);
		jmFile.add(jmiOpen);
		jmiExport = new JMenuItem("Save as");
		jmiExport.addActionListener(this);
		jmFile.add(jmiExport);
		jmiExit = new JMenuItem("Exit");
		jmiExit.addActionListener(this);
		jmFile.add(jmiExit);
		jmHelp = new JMenu("Help");
		jmiAbout = new JMenuItem("About");
		jmiAbout.addActionListener(this);
		jmHelp.add(jmiAbout);
		jmiExit.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		jmiAbout.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_B, ActionEvent.CTRL_MASK));
		jmiOpen.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		jmiExport.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		JMenuBar mb = new JMenuBar();
		mb.add(jmFile);
		mb.add(jmHelp);
		jf.setJMenuBar(mb);
		
		
		/*Window*/
		jf.setPreferredSize(new Dimension(400,500));
		jf.setLocation(200,200);
		jf.setResizable(false);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		jtaOutput1.setFont(normalFont);
		jtaOutput1.setEditable(false);
		jtaOutput2.setFont(normalFont);
		jp1.setLayout(gl1);
		jp1.add(jtaScroll1);
		jp1.add(jtaScroll2);
		c.add(jp1);
		
		/*Visible*/
		jf.pack();
		jf.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == jmiExit){
			System.exit(0);
		} else if (e.getSource() == jmiAbout){
			JOptionPane.showMessageDialog(jf, "Bitmap HexEditor\n By Shuai Yuan, Peter Oliver, Arthur Yanthar", 
										  "About!", JOptionPane.INFORMATION_MESSAGE);
		} else if (e.getSource() == jmiOpen){
			userFileName = (String)JOptionPane.showInputDialog(jf, "Enter file name:", "Open",
																JOptionPane.INFORMATION_MESSAGE);
			if (userFileName == null){
				//user cancelled
			} else {
				inputFile(userFileName, 19);
				if (fileExist){
					getHeader();
					inputFile(userFileName, headerSize);
					displayInfo();
				}
			}		  
		} else if (e.getSource() == jmiExport){
			userFileName = (String)JOptionPane.showInputDialog(jf, "Enter file name:", "Save as",
																JOptionPane.INFORMATION_MESSAGE);
			if (userFileName == null){
				//user cancelled
			} else {
				JOptionPane.showMessageDialog(jf, "CURRENTLY NOT AVAILABLE", "ERROR", JOptionPane.WARNING_MESSAGE);
			} 
		}
	}
	
	public void inputFile(String fileName, int headerL){
		File file = null;
		byte   b_data = 0;
		headerData = new String[headerL];
		jtaOutput2.setText("");
		fileExist = true;
	
		if (file == null){	
			jtaOutput1.setText("File Name: " + fileName + "\n");
			file = new File (fileName);
		}
		
		int counter = 0;
		try {
			// Wrap the FileInputStream with a DataInputStream
			FileInputStream file_input = new FileInputStream (file);
			DataInputStream data_in	= new DataInputStream (file_input );
					
			for (int i = 1; i < headerL; i++) {
				counter ++;
				try {
					b_data = data_in.readByte();
				} catch (EOFException eof) {
					  break;
				}
			
				// Store hex
				if (i % 16 == 0){
					temp[0] = b_data;
					headerData[counter] = String.format("%02X",temp);
				} else {
					temp[0] = b_data;
					headerData[counter] = String.format("%02X",temp);
				}
			}
			
			counter = 0;	
											
			while (true) {
				counter ++;
				try {
					b_data = data_in.readByte();
				} catch (EOFException eof) {
					  break;
				}
			
				// Store hex
				if (counter % 16 == 0){
					temp[0] = b_data;
					jtaOutput2.append(String.format("%02X\n",temp));
					rawData.add(b_data);
				} else {
					temp[0] = b_data;
					jtaOutput2.append(String.format("%02X ",temp));
					rawData.add(b_data);
				}				
			}
			data_in.close ();
		} catch  (IOException e) {
			jtaOutput1.append("ERROR - The system cannot find the file specified");
			fileExist = false;
		}
	}
	
	public void displayInfo(){
		getFileSize();
		if (getFileType()){
			getDimensions();
			getBitDepth();
		}		
	}
	
	public void getHeader(){
		buffer = "";
		for (int i = 18; i > 14; i --){
			buffer += headerData[i];
		}
		headerSize = Integer.parseInt(buffer, 16) + 1;
	}
	
	public boolean getFileType(){
		if (headerData[1].equalsIgnoreCase("42") && headerData[2].equalsIgnoreCase("4d")){
			jtaOutput1.append("File Type: Bitmap Image\n");
			return true;
		} else {
			jtaOutput1.append("File Type: Unknown\n");
			return false;
		}		
	}
	
	public void getFileSize(){
		buffer = "";
		for (int i = 6; i > 2; i --){
			buffer += headerData[i];
		}
		fileSize = Integer.parseInt(buffer, 16);
		jtaOutput1.append("File Size: " + fileSize + "bytes\n");
	}
	
	public void getDimensions(){
		buffer = "";
		for (int i = 22; i > 18; i --){
			buffer += headerData[i];
		}
		fileWidth = Integer.parseInt(buffer, 16);
				
		buffer = "";
		for (int i = 26; i > 22; i --){
			buffer += headerData[i];
		}
		fileLength = Integer.parseInt(buffer, 16);
		
		jtaOutput1.append("Dimensions: " + fileWidth + " x " + fileLength + "\n");
		jtaOutput1.append("Width: " + fileWidth + " pixels\n");
		jtaOutput1.append("Length: " + fileLength + " pixels\n");
	}
	
	public void getBitDepth(){
		buffer = "";
		for (int i = 30; i > 28; i --){
			buffer += headerData[i];
		}
		bitDepth = Integer.parseInt(buffer, 16);
		jtaOutput1.append("Bit Depth: " + bitDepth+ "\n");
	}

	public static void main(String[] args) {
		new BitmapHex();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Memory Problem With HexEdit Style Program

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Memory Problem With HexEdit Style Program

Posted 20 April 2009 - 05:36 PM

I add a simple println() statement:

 
   public void inputFile(String fileName, int headerL){
		File file = null;
		byte   b_data = 0;
		
		System.out.println("HeasderL: " + headerL);



and ran your program using BitmapHex.java as input file
The program shows:

HeasderL: 19
HeasderL: 1986342517

don't expect to create an array of String of 1,986,342,517 elements

I guess you will have to review the way to evaluate your hearderSize :D
Was This Post Helpful? 0
  • +
  • -

#3 dr3m0ra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 09

Re: Memory Problem With HexEdit Style Program

Posted 21 April 2009 - 06:26 AM

I forgot to say that this program also must allow the user to edit the numbers, and save the file. But first, we must be able to display all the numbers and make sure that the user is able to edit it.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Memory Problem With HexEdit Style Program

Posted 21 April 2009 - 07:14 PM

View Postdr3m0ra, on 21 Apr, 2009 - 05:26 AM, said:

I forgot to say that this program also must allow the user to edit the numbers, and save the file. But first, we must be able to display all the numbers and make sure that the user is able to edit it.

Your BitmapHex.java file as 9,000 characters
Even if you had the worst imaginable algorithm that required a String for each hex digit you will never need: 1,986,342,517 String to do that.

Review your code you have something wrong somewhere in the way you calculate the hex array. Hundreds of hex editors exist on the web using 1/1000 of the resource you require.

This post has been edited by pbl: 21 April 2009 - 07:20 PM

Was This Post Helpful? 1
  • +
  • -

#5 dr3m0ra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-April 09

Re: Memory Problem With HexEdit Style Program

Posted 22 April 2009 - 06:55 AM

Thanks! I decided to just let the program analyze and change the data instead of displaying it. It works fine now!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1