import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BNHexEdit extends Frame
{
boolean bLaden = false;
boolean bSpeichern = false;

String FileName = new String("");
String FilePfad = new String("");

TextArea TextAre = new TextArea();
TextField TextFiel = new TextField();

Button Butt = new Button("Laden");
Button Butt2 = new Button("Speichern");
Button Butt3 = new Button("Beenden");

Panel PANEL_oben = new Panel(new FlowLayout(FlowLayout.LEFT));

MenuBar mainMenuBar;
Menu menu1;
MenuItem laden;
MenuItem speichern;
MenuItem beenden;

public BNHexEdit()
{

setVisible(false);
setTitle("BNHexEdit");

SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);

SymAction lSymAction = new SymAction();
Butt.addActionListener(lSymAction);
Butt2.addActionListener(lSymAction);
Butt3.addActionListener(lSymAction);


//
mainMenuBar = new MenuBar();

menu1 = new Menu("Datei");
mainMenuBar.add(menu1);

laden = new MenuItem("Laden", new MenuShortcut('L'));
laden .addActionListener(lSymAction);
menu1.add(laden);

speichern = new MenuItem("Speichern", new MenuShortcut('S'));
speichern .addActionListener(lSymAction);
menu1.add(speichern);

menu1.addSeparator();

beenden = new MenuItem("Beenden", new MenuShortcut('B'));
beenden .addActionListener(lSymAction);
menu1.add(beenden);

setMenuBar(mainMenuBar);

//

Butt .addActionListener(lSymAction);
Butt2 .addActionListener(lSymAction);
Butt3 .addActionListener(lSymAction);

this.setBackground(Color.lightGray);
this.setLayout(new BorderLayout());

PANEL_oben.add(Butt);
PANEL_oben.add(Butt2);
PANEL_oben.add(Butt3);

this.add("North", PANEL_oben);
this.add("Center", TextAre);
this.add("South", TextFiel);

TextAre.setFont(new Font("Courier", Font.PLAIN, 12));

this.setSize(getToolkit().getScreenSize().width / 2, getToolkit().getScreenSize().height / 2);

}

public synchronized void show()
{
move(0, 0);
super.show();
}

void BNHexEdit_WindowClosing(java.awt.event.WindowEvent event)
{
hide();
dispose();
System.exit(0);
}

public String toHex(int n){
String Stri = new String("");

Stri = "" + (byte) (n);
return Stri;
}

static public void main(String args[])
{
BNHexEdit f;
f = new BNHexEdit();
f.show();
}

class SymWindow extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
Object object = event.getSource();
if (object == BNHexEdit.this){
BNHexEdit_WindowClosing(event);
}
}
}

class SymAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Object object = event.getSource();
if (object == laden || object == Butt){
if(bLaden == false){
bLaden = true;
try {
FileDialog d = new FileDialog(BNHexEdit.this, "Laden", FileDialog.LOAD);
d.show();
if(d.getFile() != null){
FileName = d.getFile();
FilePfad = d.getDirectory();
String Datei = new String(FilePfad + FileName);
File f = new File(Datei);
long lSize = f.length();
long lBytes_read = 0;
FileInputStream in = new FileInputStream(f);
byte [] abData = new byte[(int) (lSize)];
while(lBytes_read < lSize){
lBytes_read += in.read(abData, (int) (lBytes_read), (int) (lSize - lBytes_read));
}
in.close();

StringBuffer Stri = new StringBuffer("");
int nCounter = 0;
String Stri2 = new String("");
byte c;
int nWert;

while(lBytes_read > 0 && nCounter < lBytes_read && nCounter < 1000){
nWert = nCounter / 16;
if(nWert < 10){
//Stri.append("000" + nWert + ": ");
} else if(nWert < 100){
//Stri.append("00" + nWert + ": ");
} else if(nWert < 1000){
//Stri.append("0" + nWert + ": ");
} else {
//Stri.append(nWert + ": ");
}

for(int i = 0; nCounter < lBytes_read && i < 16; i++){
c = abData[nCounter];
if(c < 10){
Stri.append("00" + toHex(c) + " ");
} else if(c < 100){
Stri.append("0" + toHex(c) + " ");
} else {
Stri.append(toHex(c) + " ");
}
nCounter++;
}
Stri.append("\n");
Stri2 = "Lade " + FileName + " : " + nCounter * 100 / lBytes_read + " %";
TextFiel.setText(Stri2);
}
setTitle("BNHexEdit - " + FileName);
TextAre.setText(new String(Stri));
TextFiel.setText(" ");
}
} catch (IOException e){
}
} else {
bLaden = false;
}
} else if (object == speichern || object == Butt2){
if(bSpeichern == false){
bSpeichern = true;
try {
if(FileName == null){
FileDialog d = new FileDialog(BNHexEdit.this, "Speichern", FileDialog.SAVE);
d.show();
if(d.getFile() != null){
FileName = d.getFile();
FilePfad = d.getDirectory();
}
}
if(FileName != null){
String Datei = new String(FilePfad + FileName);

StringBuffer Stri = new StringBuffer("");
int nCounter = 0;
String StriNeu = new String("");
String StriAlt = new String(TextAre.getText());

while(nCounter < StriAlt.length() - 3){

String Str = StriAlt.substring(nCounter, nCounter + 3);
Integer Integ = new Integer(Str);
StriNeu += "" + (char) (Integ.intValue());
nCounter += 4;

String Stri2 = new String("");
Stri2 = "Speichere " + FileName + " : " + StriNeu.length() * 100 / StriAlt.length() / 4 + " %";
TextFiel.setText(Stri2);
}

File f = new File(Datei);
long lSize = StriNeu.length();
FileOutputStream out = new FileOutputStream(f);
byte [] abData = new byte[(int) (lSize)];
for(int i = 0; i < lSize; i++){
abData[i] = (byte) (StriNeu.charAt(i));
}
out.write(abData);
out.close();

TextFiel.setText(" ");
}
} catch (IOException e){
}
} else {
bSpeichern = false;
}
} else if (object == beenden || object == Butt3){
BNHexEdit_WindowClosing(null);
}
}
}

}