Groovy 언어 소스:

// Filename: testHexView_03.groovy
//
// Execute: groovy testHexView_03.groovy [filename]
//
// Date: 2013. 7. 28.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TestHexView_03 {

    public static void printUsage() {
        System.out.println("groovy testHexView_03.groovy [filename]");
    }

    public static String toHex(byte b) {
        String s = "";
        int x1, x2;
        x1 = (b & 0xF0) >> 4;
        x2 = b & 0x0F;
        if (x1 < 10)
            s += (char)(x1 + (byte)'0');
        else
            s += (char)((x1 - 10) + (byte)'A');
        if (x2 < 10)
            s += (char)(x2 + (byte)'0');
        else
            s += (char)((x2 - 10) + (byte)'A');
        return s;
    }

    public static String toHex8(int n) {
        String s = "";
        int x1, x2, x3, x4, x5, x6, x7, x8;
        x1 = (n & 0xF0000000) >> 28;
        x2 = (n & 0xF000000) >> 24;
        x3 = (n & 0xF00000) >> 20;
        x4 = (n & 0xF0000) >> 16;
        x5 = (n & 0xF000) >> 12;
        x6 = (n & 0xF00) >> 8;
        x7 = (n & 0xF0) >> 4;
        x8 = n & 0x0F;
        if (x1 < 10)
            s += (char)(x1 + (byte)'0');
        else
            s += (char)((x1 - 10) + (byte)'A');
        if (x2 < 10)
            s += (char)(x2 + (byte)'0');
        else
            s += (char)((x2 - 10) + (byte)'A');
        if (x3 < 10)
            s += (char)(x3 + (byte)'0');
        else
            s += (char)((x3 - 10) + (byte)'A');
        if (x4 < 10)
            s += (char)(x4 + (byte)'0');
        else
            s += (char)((x4 - 10) + (byte)'A');
        s += " ";
        if (x5 < 10)
            s += (char)(x5 + (byte)'0');
        else
            s += (char)((x5 - 10) + (byte)'A');
        if (x6 < 10)
            s += (char)(x6 + (byte)'0');
        else
            s += (char)((x6 - 10) + (byte)'A');
        if (x7 < 10)
            s += (char)(x7 + (byte)'0');
        else
            s += (char)((x7 - 10) + (byte)'A');
        if (x8 < 10)
            s += (char)(x8 + (byte)'0');
        else
            s += (char)((x8 - 10) + (byte)'A');
  
        return s;
    }

    public static void main(String[] args) {

        if (args.length < 1) {
            printUsage();
            System.exit(1);
        }
     
        String filename = args[0];

        try {

            File infile = new File(filename);
            if  (!infile.exists()) {
                 System.out.println("The file \"" + filename + "\" does not exist.");
                 System.exit(1);
            }

            if (!infile.canRead()) {
                 System.out.println("The file \"" + filename + "\" is not allowed to be read.");
                 System.exit(1);
            }   

            if  (infile.isDirectory()) {
                 System.out.println("The file \"" + filename + "\" is a directory.");
                 System.exit(1);
            }

            FileInputStream fileInput = new FileInputStream(infile);

            long fileSize = new File(args[0]).length();
            if  (fileSize < 1L) {
                 System.out.println("The file size is zero.");
                 System.exit(1);
            }

            System.out.println("The size of the file \"" + filename + "\" is " + fileSize + ".\n");
             
            long n = 0L;

            int data;
            String dum = "";

            while (n < fileSize && (data = fileInput.read()) != -1) {
                if (n % 16 == 0L) {
                    System.out.print(toHex8((int) (n & 0xFFFFFFFF)) + ": ");
                }
             
                if (n % 16 != 8L) {
                    System.out.print(" ");
                }
                else {
                    System.out.print("-");
                }
                System.out.print( toHex((byte) (data & 0xFF)) );
                
                if ((data & 0xFF) >= 0x20 && (data & 0xFF) <= 0x7E) {
                    dum += (char)  (data & 0xFF);
                }
                else {
                    dum += ".";
                }
                
                if (n > 0 && n % 16 == 15L) {
                    System.out.print("  |");
                    System.out.print(dum);
                    System.out.print("|");
                    System.out.println();
                    dum = "";
                }
             
                n++;
            }

            if (n > 0 && n % 16 > 0) {
                for (int i = (int)(n % 16); i < 16; i++) {
                    System.out.print("   ");
                }
                System.out.print("  |");
                System.out.print(dum);
                for (int i = (int)(n % 16); i < 16; i++) {
                    System.out.print(" ");
                }
                System.out.print("|");
                System.out.println();
                dum = "";
            }

            fileInput.close();

            System.out.println();
            System.out.println("Read " + n + " bytes.");
        }
        catch (IOException e) {
            System.out.println("Error message: " + e.getMessage());
        }   
    }

}

 

 

실행 예 1> groovy testHexView_03.groovy temp_1.bin
The size of the file "temp_1.bin" is 12.

0000 0000:  48 65 6C 6C 6F 20 74 68-65 72 65 0A              |Hello there.    |

Read 12 bytes.

 

실행 예 2> groovy testHexView_03.groovy myFile.ser
The size of the file "myFile.ser" is 130.

0000 0000:  AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31  |....sr..Person.1|
0000 0010:  46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C  |F...D....I..ageL|
0000 0020:  00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A  |..firstNamet..Lj|
0000 0030:  61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B  |ava/lang/String;|
0000 0040:  4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01  |L..lastNameq.~..|
0000 0050:  78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00  |xp....t..Jamest.|
0000 0060:  04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74  |.Ryansq.~......t|
0000 0070:  00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F  |..Obi-want..Keno|
0000 0080:  62 69                                            |bi              |

Read 130 bytes.

 

 

Posted by Scripter
,