1

In some 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) ?

Subrat
  • 3,928
  • 6
  • 39
  • 48

2 Answers2

7

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

DwB
  • 37,124
  • 11
  • 56
  • 82
  • @Subrat: Another reason why to use `commons-codec` over the `sun.misc` is that they will cause you no end of trouble if used in an IBM JRE... – beny23 Oct 03 '11 at 13:32
  • 2
    They are **not** deprecated and not terrible. It's not advisable/recommended to use any `sun.*` packages. – Buhake Sindi Oct 03 '11 at 13:33
  • 4
    Note that [`javax.xml.bind.DataTypeConverter.printBase64Binary()`](http://download.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html#printBase64Binary(byte[])) provides an easy way to convert `byte[]` to a Base64 encoded `String` that is *documented* and *officially* part of the Java SE platform. `parseBase64Binary()` is the counter-part to this. – Joachim Sauer Oct 03 '11 at 13:41
  • @Joachim Sauer, same as `java.util.prefs.Base64`, which has `byteArrayToBase64` and `base64ToByteArray` static methods. – Buhake Sindi Oct 03 '11 at 14:12
  • 1
    @TheEliteGentleman: no, that class is package-private, not public. – Joachim Sauer Oct 03 '11 at 14:16
  • Thanks,now I am using it for my Android Application by Importing android.util.Base64 – Subrat Oct 04 '11 at 10:43
2

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;
}
Daniel Gray
  • 1,697
  • 1
  • 21
  • 41