sun.misc.BASE64Decoder 在 java 应用程序中显示错误

发布于 2024-12-08 03:10:03 字数 265 浏览 0 评论 0原文

在某些 java 文件中使用 :

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

,当我将该 java 文件放入 Eclipse IDE 中时。它不会检测这些文件。 但这些包(sun.misc.BASE64Decoder、sun.misc.BASE64Encoder)位于 rt.jar 文件内。在我的项目库中“rt.jar”可用。 但为什么它显示错误(日食中的红线)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏已如深秋| 2024-12-15 03:10:03

不要使用 sun.misc 包中的类。这些已被弃用并且很糟糕。查看 Apache commons 编解码器 进行 Base64 编码和解码。 为什么不使用 sun.misc.* 类

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

总攻大人 2024-12-15 03:10:03

我编写了以下Base64编码和解码的代码:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文