Hello all,
One of my application used following code which is developed in java 1.2.2 but now we are migrating this application to java se 6 where the following code is depracted.

sun.misc.HexDumpEncoder.encodeBuffer

Can any one guide for the above code replacement in java se 6

Thanks & Regards,

Dani AI

Generated

sun.misc.HexDumpEncoder is an internal Sun/Oracle class (in the sun.misc package). Relying on it is brittle: it isn’t part of the supported public API and can change or disappear between JVMs. is right to move away from it. ’s suggestion to compile with deprecation warnings is useful to find all call sites; ’s NIO tip is helpful for efficient I/O but NIO does not provide a direct drop‑in for hex formatting.

If you only need a plain hex string for a byte array, use a supported API or a small utility rather than sun.misc. In Java 6 you can use the JAXB helper:

import javax.xml.bind.DatatypeConverter;

public static String toHex(byte[] bytes) {
    return DatatypeConverter.printHexBinary(bytes); // uppercase hex
}

Or use a well‑known library (Apache Commons Codec):

import org.apache.commons.codec.binary.Hex;

String hex = Hex.encodeHexString(bytes); // lowercase by default

If you need the full “hexdump” layout (offsets, grouped hex bytes and ASCII column) like HexDumpEncoder produced, here’s a compact, standalone replacement you can drop in and test. It doesn’t depend on internal classes and works in Java 6:

public static String hexDump(byte[] data) {
    StringBuilder sb = new StringBuilder();
    for (int off = 0; off < data.length; off += 16) {
        int len = Math.min(16, data.length - off);
        sb.append(String.format("%08X: ", off));
        for (int i = 0; i < 16; i++) {
            if (i < len) sb.append(String.format("%02X ", data[off + i]));
            else sb.append("   ");
            if (i == 7) sb.append(" ");
        }
        sb.append(" |");
        for (int i = 0; i < len; i++) {
            int b = data[off + i] & 0xFF;
            sb.append((b >= 32 && b <= 126) ? (char) b : '.');
        }
        sb.append("|\n");
    }
    return sb.toString();
}

Notes: prefer a library for production (tested formatting, internationalization, performance). If you read large files, combine this with streaming (NIO or buffered streams) so you don’t allocate huge byte arrays. Replace calls incrementally: find usages (as suggested), pick the simple hex or full dump option above depending on what the code expects, and run unit tests to confirm output format matches any callers that parse the dump.

Recommended Answers

All 4 Replies

Hi msalahu, you could try compiling the source code from the command line and add the "deprecation" switch, which will show you a description of members/classes that have been used and have been deprecated. Then you ought to refer to the appropriate class documentation for what part of the API you should use as a replacement. E.G.

javac [I]ClassName[/I].java -deprecation

I hope this helps!

hi,
i compiled the code with ur mentioned switch only then i got the deprecation message.
I am unable to get this code replacement in java se 6 API
kinldy help if u know any such class/function
Thanks

Hi msalahu, you could try compiling the source code from the command line and add the "deprecation" switch, which will show you a description of members/classes that have been used and have been deprecated. Then you ought to refer to the appropriate class documentation for what part of the API you should use as a replacement. E.G.

javac [I]ClassName[/I].java -deprecation

I hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.