Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Class chartobyteconverter Type deprecated

I am working on DNA proteins alignment project "readseq" . Its "flybase " package contains java code having " charToByteConverter" class which does not compile and gives the " type deprecated " message. (http://iubio.bio.indiana.edu/soft/molbio/readseq/java/). Here readseq source can be foundI need to add some more functionality into this application, don't know how to fix it to proceed towards my goal. I am a kind of new bie in java. Plz help if possible. Readseq is with its gui is easily available on net. It just converts an array of given characters to bytes. Here is some info about it: (docjar.com/docs/api/sun/io/CharToByteConverter.html) . I don't know what to do about this being deprecated. It is an abstract class used as under:

protected byte[] getBytes(CharToByteConverter ctb) {
        ctb.reset();
        int estLength = ctb.getMaxBytesPerChar() * count;
        byte[] result = new byte[estLength];
        int length;

        try {
            length = ctb.convert(value, offset, offset + count,
                     result, 0, estLength);
            length += ctb.flush(result, ctb.nextByteIndex(), estLength);
        } catch (CharConversionException e) {
            length = ctb.nextByteIndex();
        }

        if (length < estLength) {
            // A short format was used:  Trim the byte array.
            byte[] trimResult = new byte[length];
            System.arraycopy(result, 0, trimResult, 0, length);
            return trimResult;
        }
        else {
            return result;
        }
}
like image 946
Martin Avatar asked Feb 05 '26 14:02

Martin


2 Answers

The javadoc comment says it all:

Deprecated! Replaced - by java.nio.charset

Look for a replacement class/method in the java.nio.charset package.

Note that using classes in the JDK that are not part of the officially documented API is a bad idea in the first place.

like image 137
JB Nizet Avatar answered Feb 08 '26 03:02

JB Nizet


This is a perfect case for Adapt Parameter, from Michael Feathers book Working Effectively With Legacy Code.

Shameless self-plug: Here's a short prezi I did on it. It has a step-by-step breakdown of what you need to do.

Essentially, you're going to have to modify the code you have and apply the Adapter Pattern to the parameter. You'll want to define your own interface (let's call it ByteSource), make getBytes() take your interface instead (getBytes(ByteSource ctb)), then make the Adapter that internally has a CharToByteConverter for testing. To fix the broken library, you should make one that has a java.nio.charset instead.

like image 36
Visionary Software Solutions Avatar answered Feb 08 '26 02:02

Visionary Software Solutions



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!