为什么我制作的 Basic Java Hex 编辑器会冻结大约 2MB 及以上大小的文件?

发布于 2024-09-27 01:30:58 字数 3454 浏览 1 评论 0原文

好吧,我正在用 Java 编写一个十六进制编辑器,当我加载大约 2MB 或更大的文件时,它就会冻结。但没有显示任何错误,所以我不确定发生了什么。谁能帮助我吗?这是代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class HexEditor extends JFrame{
 JScrollPane hexScroll;
 JScrollPane byteScroll;
 JPanel panel;
 JTextArea hexArea;
 JTextArea byteArea;
 JFileChooser chooser;// = new JFileChooser();
 FileInputStream fin;
 JMenuBar menuBar;
 JMenu file;
  JMenuItem load;

 public HexEditor(){
  super("Cypri's java hex editor");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  chooser = new JFileChooser();

  load = new JMenuItem("Load");
   load.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event) {

     try{

      openFile();
      fin = new FileInputStream(chooser.getSelectedFile());

      int ch;
      StringBuffer strContent = new StringBuffer("");

      for(int i = 0; (ch = fin.read()) != -1; i++){
       String s = Integer.toHexString(ch);

       if(s.length() < 2)
        s = "0" + Integer.toHexString(ch);

       if(i < 10)
        strContent.append(" " + s.toUpperCase());

       else{
        strContent.append(" " + s.toUpperCase() + "\n");
        i = 0;
       }
      }

      hexArea.setText(strContent.toString());

      byte[] b = hexStringToByteArray(strContent.toString().trim());
      char[] chars = new char[b.length];
      String byteText = "";
      int newLine = 0;
      for(int i = 0; i < b.length; i++){
       chars[i] = (char) b[i];
       byteText += chars[i];

       newLine++;
       if(newLine > 10){
        byteText += "\n";
        newLine = 0;
       }
      }

      hexArea.setText(strContent.toString());
      byteArea.setText(byteText);
      packMe();
     }

     catch(Exception e){
      e.printStackTrace();
     }
    }
   });

  file = new JMenu("File");
  file.add(load);

  menuBar = new JMenuBar();

  menuBar.add(file);

  hexArea = new JTextArea();
  byteArea = new JTextArea();

  hexScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  byteScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

  panel = new JPanel();

  panel.add(hexScroll);
  panel.add(byteScroll);
  hexScroll.setViewportView(hexArea);
  byteScroll.setViewportView(byteArea);

  hexArea.setPreferredSize(new Dimension(200,200));
  byteArea.setPreferredSize(new Dimension(200,200));

  hexScroll.setPreferredSize(new Dimension(200,200));
  byteScroll.setPreferredSize(new Dimension(200,200));

  getContentPane().setLayout(new BorderLayout());
  getContentPane().add(BorderLayout.NORTH, menuBar);
  getContentPane().add(BorderLayout.CENTER, panel);
  pack();
  setVisible(true);
 }

 public static byte[] hexStringToByteArray(String s) {
     int len = s.length() -1;
     byte[] data = new byte[(len / 2) + 1];
     for (int i = 0; i < len; i += 2) {
         data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
     }
     return data;
 }


 public void openFile(){
  chooser.showOpenDialog(null);
 }

 public void packMe(){
  pack();
 }

 public static void main(String[] args){
     HexEditor app = new HexEditor();
 }
}

Alright, so I'm writing a hex editor in Java and when I load in a file around 2MB or bigger it just freezes. No errors show up though, so I'm not sure whats going on. Can anyone help me? This is the code:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import java.util.Vector;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class HexEditor extends JFrame{
 JScrollPane hexScroll;
 JScrollPane byteScroll;
 JPanel panel;
 JTextArea hexArea;
 JTextArea byteArea;
 JFileChooser chooser;// = new JFileChooser();
 FileInputStream fin;
 JMenuBar menuBar;
 JMenu file;
  JMenuItem load;

 public HexEditor(){
  super("Cypri's java hex editor");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  chooser = new JFileChooser();

  load = new JMenuItem("Load");
   load.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event) {

     try{

      openFile();
      fin = new FileInputStream(chooser.getSelectedFile());

      int ch;
      StringBuffer strContent = new StringBuffer("");

      for(int i = 0; (ch = fin.read()) != -1; i++){
       String s = Integer.toHexString(ch);

       if(s.length() < 2)
        s = "0" + Integer.toHexString(ch);

       if(i < 10)
        strContent.append(" " + s.toUpperCase());

       else{
        strContent.append(" " + s.toUpperCase() + "\n");
        i = 0;
       }
      }

      hexArea.setText(strContent.toString());

      byte[] b = hexStringToByteArray(strContent.toString().trim());
      char[] chars = new char[b.length];
      String byteText = "";
      int newLine = 0;
      for(int i = 0; i < b.length; i++){
       chars[i] = (char) b[i];
       byteText += chars[i];

       newLine++;
       if(newLine > 10){
        byteText += "\n";
        newLine = 0;
       }
      }

      hexArea.setText(strContent.toString());
      byteArea.setText(byteText);
      packMe();
     }

     catch(Exception e){
      e.printStackTrace();
     }
    }
   });

  file = new JMenu("File");
  file.add(load);

  menuBar = new JMenuBar();

  menuBar.add(file);

  hexArea = new JTextArea();
  byteArea = new JTextArea();

  hexScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  byteScroll = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

  panel = new JPanel();

  panel.add(hexScroll);
  panel.add(byteScroll);
  hexScroll.setViewportView(hexArea);
  byteScroll.setViewportView(byteArea);

  hexArea.setPreferredSize(new Dimension(200,200));
  byteArea.setPreferredSize(new Dimension(200,200));

  hexScroll.setPreferredSize(new Dimension(200,200));
  byteScroll.setPreferredSize(new Dimension(200,200));

  getContentPane().setLayout(new BorderLayout());
  getContentPane().add(BorderLayout.NORTH, menuBar);
  getContentPane().add(BorderLayout.CENTER, panel);
  pack();
  setVisible(true);
 }

 public static byte[] hexStringToByteArray(String s) {
     int len = s.length() -1;
     byte[] data = new byte[(len / 2) + 1];
     for (int i = 0; i < len; i += 2) {
         data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
     }
     return data;
 }


 public void openFile(){
  chooser.showOpenDialog(null);
 }

 public void packMe(){
  pack();
 }

 public static void main(String[] args){
     HexEditor app = new HexEditor();
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-10-04 01:30:58

2MB 就已经是一个很大的字符串了。您需要将文件分成页面进行显示,并且一次只转换几KB的缓冲区。

2MB would already result in a big String. You need to break the file into pages for display, and only convert a buffer of a few KB at once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文