|
|
Исходный текст программы GetChecksum.java
|
Оглавление |
Назад
// ==========================================
// GetChecksum.java
// (C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web: http://www.glasnet.ru/~frolov
// ==========================================
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.*;
public class GetChecksum
{
public static void main(String args[])
{
FrameWindow frame;
frame = new FrameWindow(
"File Checksum calculation");
frame.setVisible(true);
}
}
// =======================================
// Class FrameWindow
// =======================================
class FrameWindow extends Frame
implements ActionListener, WindowListener
{
TextArea ta;
MenuBar mb;
Menu mFile;
MenuItem miOpen;
MenuItem miExit;
FileDialog fdlg;
// ============================================
// FrameWindow
// ============================================
public FrameWindow(String szTitle)
{
super(szTitle);
setSize(400, 300);
mb = new MenuBar();
mFile = new Menu("File");
miOpen = new MenuItem("Open...");
mFile.add(miOpen);
mFile.add("-");
miExit = new MenuItem("Exit");
mFile.add(miExit);
mb.add(mFile);
miOpen.addActionListener(this);
miExit.addActionListener(this);
setMenuBar(mb);
this.addWindowListener(this);
ta = new TextArea(10, 30);
setLayout(new BorderLayout());
add("Center", ta);
}
// ============================================
// actionPerformed
// ============================================
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(miOpen))
{
fdlg = new FileDialog(this, "Open file",
FileDialog.LOAD);
fdlg.show();
String szPath = fdlg.getDirectory() +
fdlg.getFile();
ta.append("Open: " + szPath + "\n");
ta.append("CRC-32: " +
calculate(szPath, new CRC32()) + "\n");
ta.append("Adler-32: " +
calculate(szPath, new Adler32()) + "\n");
}
else if(e.getSource().equals(miExit))
{
setVisible(false);
System.exit(0);
}
}
// ============================================
// windowClosing
// ============================================
public void windowClosing(WindowEvent e)
{
setVisible(false);
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
// ============================================
// calculate
// ============================================
String calculate(String szPath, Checksum cs)
{
String s = "";
byte[] buf = new byte[8000];
int nLength = 0;
try
{
FileInputStream fis =
new FileInputStream(szPath);
while(true)
{
nLength = fis.read(buf);
if(nLength < 0)
break;
cs.update(buf, 0, nLength);
}
fis.close();
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
s = new Long(cs.getValue()).toString();
return s;
}
}
|
|
|