sun.misc.BASE64Decoder showing error in java application
In some java 开发者_JAVA技巧file there is a use of :
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
and when I put that java file in Eclipse IDE. It will not detecting these files. But these packages(sun.misc.BASE64Decoder , sun.misc.BASE64Encoder) are inside the rt.jar file. In the library of my project "rt.jar" is available. But why it shows error(red lines in eclipse) ?
Don't use classes in the sun.misc package. These are deprecated and terrible. Check out Apache commons codec for base64 encoding and decoding. Why not to use sun.misc.* classes
I wrote the following code for Base64 encoding and decoding:
public static String base64Encode(String filename, boolean padding) {
char[] base64Chars = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/").toCharArray();
FileInputStream dataToEncode;
StringBuilder encodedData = new StringBuilder();
byte[] dataBuffer = new byte[3];
int bytesRead;
try {
dataToEncode = new FileInputStream(filename);
} catch (FileNotFoundException fnfe) {
System.err.println("File not found!!");
return "";
}
try {
bytesRead = dataToEncode.read(dataBuffer);
// cast to int, to avoid sign issues on the byte
int buffer0 = dataBuffer[0] & 0xFF;
int buffer1 = dataBuffer[1] & 0xFF;
int buffer2 = dataBuffer[2] & 0xFF;
if (bytesRead == -1) {
System.err.println("Premature END OF FILE (nothing read)!!");
dataToEncode.close();
return "";
}
while (bytesRead == 3) {
// calculation of the base64 digits
int b641 = buffer0 >>> 2;
int b642 = ((buffer0 & 0x03) << 4) | (buffer1 >>> 4);
int b643 = ((buffer1 & 0x0F) << 2) | (buffer2 >>> 6);
int b644 = buffer2 & 0x3F;
// generation of the 4 base64 chars, for the 3 bytes
encodedData.append(base64Chars[b641]);
encodedData.append(base64Chars[b642]);
encodedData.append(base64Chars[b643]);
encodedData.append(base64Chars[b644]);
bytesRead = dataToEncode.read(dataBuffer);
buffer0 = dataBuffer[0] & 0xFF;
buffer1 = dataBuffer[1] & 0xFF;
buffer2 = dataBuffer[2] & 0xFF;
}
if (bytesRead == 2) {
encodedData.append(base64Chars[buffer0 >>> 2]);
encodedData.append(base64Chars[((buffer0 & 0x03) << 4) | (buffer1 >>> 4)]);
encodedData.append(base64Chars[((buffer1 & 0x0F) << 2)]); // exclude the last byte, that's just 0
if (padding) { // add the '=' character for padding, if the user wants
encodedData.append('=');
}
} else if (bytesRead == 1) {
encodedData.append(base64Chars[buffer0 >>> 2]);
encodedData.append(base64Chars[((buffer0 & 0x03) << 4)]);
if (padding) {
encodedData.append("==");
}
}
dataToEncode.close();
return encodedData.toString();
} catch (IOException e) {
System.out.println("Error reading file!!");
return "";
}
}
public static byte[] base64Decode(String encodedData) {
String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// remove padding
encodedData = encodedData.split("=")[0];
// create the byte buffer to unpack the bytes into
byte[] result = new byte[(int) (encodedData.length() / 4) * 3 + encodedData.length() % 4 - 1];
int readingPosition = 0, writingPosition = 0, b641, b642, b643, b644;
while (encodedData.length() - readingPosition > 3) {
b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
b644 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
result[writingPosition++] = (byte) ((b641 << 2) | (b642 >>> 4));
result[writingPosition++] = (byte) (((b642 & 0x0F) << 4) | (b643 >>> 2));
result[writingPosition++] = (byte) (((b643 & 0x03) << 6) | b644);
}
b641 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
b642 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
result[writingPosition++] = (byte) ((b641 << 2) | (b642 >>> 4));
if (encodedData.length() % 4 == 3) {
b643 = base64Chars.indexOf(encodedData.charAt(readingPosition++));
result[writingPosition++] = (byte) (((b642 & 0x0F) << 4) | (b643 >>> 2));
}
return result;
}
精彩评论