3

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;
        }
}
2
  • What does the "charToByteConverter" do exactly? I guess it's not just converting from a Java char to a Java byte... Commented May 4, 2012 at 7:12
  • 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.
    – Martin
    Commented May 4, 2012 at 7:56

2 Answers 2

3

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.

4
  • 1
    It seems that this person who self-reports not knowing Java has been tasked with extending a legacy library. Although this advice is "bare minimum correct", it doesn't really give a lot of guidance as to how to accomplish the task. Commented May 4, 2012 at 8:25
  • 1
    If the OP doesn't know Java, no answer will help him except the complete code to replace the current one. My advice would be: learn Java. Programming in Java without knowing Java is a sure path to disaster.
    – JB Nizet
    Commented May 4, 2012 at 8:29
  • 1
    Conceptually I'm inclined to agree with you. Though one could argue that if the problem is approached very specifically, using a TDD driven approach that scopes down the task to specifically 1) make an interface, 2) make a class, 3) have your class import and use another object, this is a different and simpler learning task than "go learn Java and the NIO package. Try again in 2 months." Modern programming techniques can give us ways of having our programming languages conform to how our minds want them to work, instead of having to beat our minds around the programming language. Commented May 4, 2012 at 8:34
  • Looking for this replacement has got me five days. too bad for a juvenile. could find none. but i have to use this , not on my will, it was choice for the writer of readseq api.
    – Martin
    Commented May 4, 2012 at 8:45
1

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.