问题
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;
}
}
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/10444129/java-class-chartobyteconverter-type-deprecated