Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello experts, please i need help with this code, i created it to view hex of files, but it doesn't read large files because it stores the data in memory, which in turn gives out"OutOfMemory" error. I want to be able to view the hex like this http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.github.javadev%22%20AND%20a%3A%22hexeditor%22. It can view files of up to 9 exabytes as it claims;

1. how can i change my code to do the same?
2. What do i need to add or remove or change please?

I'm actually a newbie so if you'll help me with code please some explanation will be good i don't mean to be rude or if i'm asking too much please forgive. Thanks

Java
<pre>import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;

/**
 *
 * @author kingamada
 */
public class HexViewer {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        InputStream is = null;
        JFileChooser open = new JFileChooser();
            open.showOpenDialog(null);
            File f = open.getSelectedFile();
            is = new BufferedInputStream(new FileInputStream(f));
            int bytesCounter = 0;
            int value;
            StringBuilder set = new StringBuilder();
            StringBuilder sbResult = new StringBuilder();
            while ((value = is.read()) != -1) {
                //convert to hex value with "X" formatter
                set.append(String.format("%02X ", value));
                //if 90 bytes are read, reset the counter,
                if (bytesCounter == 90) {
                    sbResult.append(set).append("\n");
                    bytesCounter = 0;
                    
                } else {
                    bytesCounter++;   
                }
                System.out.print(set);
                set.setLength(0);
                
            }       
            if (bytesCounter != 0) {
                //add spaces more formatting purpose only
                for (; bytesCounter < 90; bytesCounter++) {
                    //1 character 3 spaces
                    set.append("   ");
                }
                sbResult.append(set).append("\n");
                
            }
            
    }
    
}


What I have tried:

I used byte[] to read the file in chunks but it doesn't write the hex
<pre lang="java"><pre>import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JFileChooser;

/**
 *
 * @author kingamada
 */
public class HexViewer {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        InputStream is = null;
        JFileChooser open = new JFileChooser();
            open.showOpenDialog(null);
            File f = open.getSelectedFile();
            is = new BufferedInputStream(new FileInputStream(f));
            int bytesCounter = 0;
            byte[] buffer= new byte[1024];
            int value;
            StringBuilder set = new StringBuilder();
            StringBuilder sbResult = new StringBuilder();
            while ((value = is.read(buffer)) != -1) {
                //convert to hex value with "X" formatter
                set.append(String.format("%02X ", value));
                //if 90 bytes are read, reset the counter,
                if (bytesCounter == 90) {
                    sbResult.append(set).append("\n");
                    bytesCounter = 0;
                    
                } else {
                    bytesCounter++;   
                }
                System.out.print(set);
                set.setLength(0);
                
            }       
            if (bytesCounter != 0) {
                //add spaces more formatting purpose only
                for (; bytesCounter < 90; bytesCounter++) {
                    //1 character 3 spaces
                    set.append("   ");
                }
                sbResult.append(set).append("\n");
                
            }
            
    }
    
}


I also check this hex editor http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.github.javadev%22%20AND%20a%3A%22hexeditor%22 , but when reading through the source code i can't find out where or how the author accomplished it.
Posted
Updated 1-Jan-18 13:37pm

1 solution

First, you're just reading bytes, not "Hex". Hex is just a representation of a value in base 16 used by humans.

You just have to read "pages" of data from the file. What's a "page"? Easy, how many bytes can you show in your display of the file data? There's a page. You read that many bytes from the file starting at an offset. If you show, say, 512 bytes on screen, the first page is at offset 0, the next page is at offset 512, 1024, ... Just read the page you need to show.

How can you support such massive files? Don't use 32-bit integers for tracking the offset. Use a 64-bit unsigned integer instead, what would normally be called a "unsigned long". For example, reading the 250,000,000 page would be offset 128,000,000,000 bytes into the file. That's not going to fit into a 32-bit unsigned integer, but it will fit into a 64-bit integer.

I can't tell you how to modify your code. I don't do Java. But, I've just given you the framework.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900