GVKun编程网logo

导入sun.misc.BASE64Encoder导致在Eclipse中编译错误

22

在本文中,您将会了解到关于导入sun.misc.BASE64Encoder导致在Eclipse中编译错误的新资讯,并给出一些关于android–如何获取sun.misc.BASE64Encoder类的

在本文中,您将会了解到关于导入sun.misc.BASE64Encoder导致在Eclipse中编译错误的新资讯,并给出一些关于android – 如何获取sun.misc.BASE64Encoder类的JAR文件?、base64encode base64decode utf16to8 utf8to16、base64encode 编码原理、BASE64Encoder的实用技巧。

本文目录一览:

导入sun.misc.BASE64Encoder导致在Eclipse中编译错误

导入sun.misc.BASE64Encoder导致在Eclipse中编译错误

对于这两个进口;

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

我收到此错误:

Access restriction: The type BASE64Decoder is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

如何解决此错误?

答案1

小编典典

该错误是由您的Eclipse配置引起的。您可以将其减少为警告。更好的是,使用不属于非公共API的Base64编码器。Apache
Commons有一个,或者当您已经使用Java
1.8时,请使用java.util.Base64

android – 如何获取sun.misc.BASE64Encoder类的JAR文件?

android – 如何获取sun.misc.BASE64Encoder类的JAR文件?

我的Activity类中有一些使用sun.misc.BASE64Encoder类的代码.但它显示“ java.lang.NoClassDefFoundError:sun.misc.BASE64Encoder”.我需要罐子吗?我在哪里可以下载?请帮我.

解决方法

不要使用太阳.*类.对于 Android中的Base64,使用它是 native class或者为你的项目添加一个库(Apache commons等).或者,只需将Android源代码复制到您的项目(在您自己的包中),如果您需要在2.2之前的设备上使用它.

base64encode base64decode utf16to8 utf8to16

base64encode base64decode utf16to8 utf8to16


<script type="text/javascript">
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,

52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,

-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,

15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,

-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,

41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

//编码的方法

function base64encode(str) {

var out, i, len;

var c1, c2, c3;

len = str.length;

i = 0;

out = "";

while(i < len) {

c1 = str.charCodeAt(i++) & 0xff;

if(i == len)

{

out += base64EncodeChars.charAt(c1 >> 2);

out += base64EncodeChars.charAt((c1 & 0x3) << 4);

out += "==";

break;

}

c2 = str.charCodeAt(i++);

if(i == len)

{

out += base64EncodeChars.charAt(c1 >> 2);

out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));

out += base64EncodeChars.charAt((c2 & 0xF) << 2);

out += "=";

break;

}

c3 = str.charCodeAt(i++);

out += base64EncodeChars.charAt(c1 >> 2);

out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));

out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));

out += base64EncodeChars.charAt(c3 & 0x3F);

}

return out;

}

//解码的方法

function base64decode(str) {

var c1, c2, c3, c4;

var i, len, out;

len = str.length;

i = 0;

out = "";

while(i < len) {

do {

c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];

} while (i < len && c1 == -1);

if(c1 == -1)

break;

do {

c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];

} while (i < len && c2 == -1);

if(c2 == -1)

break;

out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

do {

c3 = str.charCodeAt(i++) & 0xff;

if(c3 == 61)

return out;

c3 = base64DecodeChars[c3];

} while (i < len && c3 == -1);

if(c3 == -1)

break;

out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

do {

c4 = str.charCodeAt(i++) & 0xff;

if(c4 == 61)

return out;

c4 = base64DecodeChars[c4];

} while (i < len && c4 == -1);

if(c4 == -1)

break;

out += String.fromCharCode(((c3 & 0x03) << 6) | c4);

}

return out;

}

function utf16to8(str) {

var out, i, len, c;

out = "";

len = str.length;

for(i = 0; i < len; i++) {

c = str.charCodeAt(i);

if((c >= 0x0001) && (c <= 0x007F)) {

out += str.charAt(i);

} else if(c > 0x07FF) {

out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));

out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));

out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));

} else {

out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));

out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));

}

}

return out;

}

function utf8to16(str) {

var out, i, len, c;

var char2, char3;

out = "";

len = str.length;

i = 0;

while(i < len) {

c = str.charCodeAt(i++);

switch(c >> 4)

{

case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:

// 0xxxxxxx

out += str.charAt(i - 1);

break;

case 12:
case 13:

// 110x xxxx 10xx xxxx

char2 = str.charCodeAt(i++);

out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));

break;

case 14:

// 1110 xxxx 10xx xxxx 10xx xxxx

char2 = str.charCodeAt(i++);

char3 = str.charCodeAt(i++);

out += String.fromCharCode(((c & 0x0F) << 12) |

((char2 & 0x3F) << 6) |

((char3 & 0x3F) << 0));

break;

}

}

return out;

}

console.log(base64encode("123"))
console.log(base64decode("MTIz"))
</script>

base64encode 编码原理

base64encode 编码原理

Base64编码,是我们程序开发中经常使用到的编码方法。它是一种基于用64个可打印字符来表示二进制数据的表示方法。它通常用作存储、传输一些二进制数据编码方法!也是MIME(多用途互联网邮件扩展,主要用作电子邮件标准)中一种可打印字符表示二进制数据的常见编码方法!它其实只是定义用可打印字符传输内容一种方法,并不会产生新的字符集!

Base64实现转换原理

它是用64个可打印字符表示二进制所有数据方法,因而才取名 BASE64。由于2的6次方等于64,所以可以用每6个位元为一个单元,对应某个可打印字符。我们知道三个字节有24个位元,就可以刚好对应于4个Base64单元,即3个字节需要用4个Base64的可打印字符来表示。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9 ,这样共有62个字符,此外两个可打印符号在不同的系统中一般有所不同。但是,我们经常所说的Base64另外2个字符是:“+/”。这64个字符,所对应表如下。

 

编号 字符   编号 字符   编号 字符   编号 字符
0 A 16 Q 32 g 48 w
1 B 17 R 33 h 49 x
2 C 18 S 34 i 50 y
3 D 19 T 35 j 51 z
4 E 20 U 36 k 52 0
5 F 21 V 37 l 53 1
6 G 22 W 38 m 54 2
7 H 23 X 39 n 55 3
8 I 24 Y 40 o 56 4
9 J 25 Z 41 p 57 5
10 K 26 a 42 q 58 6
11 L 27 b 43 r 59 7
12 M 28 c 44 s 60 8
13 N 29 d 45 t 61 9
14 O 30 e 46 u 62 +
15 P 31 f 47 v 63 /

转换的时候,将三个byte的数据,先后放入一个24bit的缓冲区中,先来的byte占高位。数据不足3byte的话,于缓冲区中剩下的bit用0补足。然后,每次取出6个bit,按照其值选择ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/中的字符作为编码后的输出。不断进行,直到全部输入数据转换完成。
如果最后剩下两个输入数据,在编码结果后加1个“=”;如果最后剩下一个输入数据,编码结果后加2个“=;如果没有剩下任何数据,就什么都不要加,这样才可以保证资料还原的正确性。 编码后的数据比原始数据略长,为原来的4/3。无论什么样的字符都会全部被编码,因此不像Quoted-printable 编码,还保留部分可打印字符。所以,它的可读性不如Quoted-printable 编码!

 

文本 M a n
ASCII编码 77 97 110
二进制位 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0
索引 19 22 5 46
Base64编码 T W F u

M的Ascii码是77,前六位对应值为19,对应base64字符是T,如此类推。其它字符编码就可以自动转换得到!我们看看另外不是刚好是3个字节的情况!

 

文本(1 Byte) A    
二进制位 0 1 0 0 0 0 0 1                                
二进制位(补0) 0 1 0 0 0 0 0 1 0 0 0 0                        
Base64编码 Q Q = =
文本(2 Byte) B C  
二进制位 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1     x x x x x x
二进制位(补0) 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 x x x x x x
Base64编码 Q k M  =

 

BASE64Encoder

BASE64Encoder

/**
 * A Base64 Encoder/Decoder.
 * 
 * <p>
 * This class is used to encode and decode data in Base64 format as described in
 * RFC 1521.
 * 
 * <p>
 * This is "Open Source" software and released under the <a
 * href="http://www.gnu.org/licenses/lgpl.html">GNU/LGPL</a> license.<br>
 * It is provided "as is" without warranty of any kind.<br>
 * Home page: <a href="http://www.source-code.biz">www.source-code.biz</a><br>
 * 
 * <p>
 * &nbsp; Method encode(String) renamed to encodeString(String).<br>
 * &nbsp; Method decode(String) renamed to decodeString(String).<br>
 * &nbsp; New method encode(byte[],int) added.<br>
 * &nbsp; New method decode(String) added.<br>
 */

public class BASE64Encoder {

	// Mapping table from 6-bit nibbles to Base64 characters.
	private static char[] map1 = new char[64];
	static {
		int i = 0;
		for (char c = ''A''; c <= ''Z''; c++)
			map1[i++] = c;
		for (char c = ''a''; c <= ''z''; c++)
			map1[i++] = c;
		for (char c = ''0''; c <= ''9''; c++)
			map1[i++] = c;
		map1[i++] = ''+'';
		map1[i++] = ''/'';
	}

	// Mapping table from Base64 characters to 6-bit nibbles.
	private static byte[] map2 = new byte[128];
	static {
		for (int i = 0; i < map2.length; i++)
			map2[i] = -1;
		for (int i = 0; i < 64; i++)
			map2[map1[i]] = (byte) i;
	}

	/**
	 * Encodes a string into Base64 format. No blanks or line breaks are
	 * inserted.
	 * 
	 * @param s
	 *            a String to be encoded.
	 * @return A String with the Base64 encoded data.
	 */
	public static String encodeString(String s) {
		return new String(encode(s.getBytes()));
	}

	/**
	 * Encodes a byte array into Base64 format. No blanks or line breaks are
	 * inserted.
	 * 
	 * @param in
	 *            an array containing the data bytes to be encoded.
	 * @return A character array with the Base64 encoded data.
	 */
	public static char[] encode(byte[] in) {
		return encode(in, in.length);
	}

	/**
	 * Encodes a byte array into Base64 format. No blanks or line breaks are
	 * inserted.
	 * 
	 * @param in
	 *            an array containing the data bytes to be encoded.
	 * @param iLen
	 *            number of bytes to process in <code>in</code>.
	 * @return A character array with the Base64 encoded data.
	 */
	public static char[] encode(byte[] in, int iLen) {
		int oDataLen = (iLen * 4 + 2) / 3; // output length without padding
		int oLen = ((iLen + 2) / 3) * 4; // output length including padding
		char[] out = new char[oLen];
		int ip = 0;
		int op = 0;
		while (ip < iLen) {
			int i0 = in[ip++] & 0xff;
			int i1 = ip < iLen ? in[ip++] & 0xff : 0;
			int i2 = ip < iLen ? in[ip++] & 0xff : 0;
			int o0 = i0 >>> 2;
			int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
			int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
			int o3 = i2 & 0x3F;
			out[op++] = map1[o0];
			out[op++] = map1[o1];
			out[op] = op < oDataLen ? map1[o2] : ''='';
			op++;
			out[op] = op < oDataLen ? map1[o3] : ''='';
			op++;
		}
		return out;
	}

	/**
	 * Decodes a string from Base64 format.
	 * 
	 * @param s
	 *            a Base64 String to be decoded.
	 * @return A String containing the decoded data.
	 * @throws IllegalArgumentException
	 *             if the input is not valid Base64 encoded data.
	 */
	public static String decodeString(String s) {
		return new String(decode(s));
	}

	/**
	 * Decodes a byte array from Base64 format.
	 * 
	 * @param s
	 *            a Base64 String to be decoded.
	 * @return An array containing the decoded data bytes.
	 * @throws IllegalArgumentException
	 *             if the input is not valid Base64 encoded data.
	 */
	public static byte[] decode(String s) {
		return decode(s.toCharArray());
	}

	/**
	 * Decodes a byte array from Base64 format. No blanks or line breaks are
	 * allowed within the Base64 encoded data.
	 * 
	 * @param in
	 *            a character array containing the Base64 encoded data.
	 * @return An array containing the decoded data bytes.
	 * @throws IllegalArgumentException
	 *             if the input is not valid Base64 encoded data.
	 */
	public static byte[] decode(char[] in) {
		int iLen = in.length;
		if (iLen % 4 != 0)
			throw new IllegalArgumentException(
					"Length of Base64 encoded input string is not a multiple of 4.");
		while (iLen > 0 && in[iLen - 1] == ''='')
			iLen--;
		int oLen = (iLen * 3) / 4;
		byte[] out = new byte[oLen];
		int ip = 0;
		int op = 0;
		while (ip < iLen) {
			int i0 = in[ip++];
			int i1 = in[ip++];
			int i2 = ip < iLen ? in[ip++] : ''A'';
			int i3 = ip < iLen ? in[ip++] : ''A'';
			if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
				throw new IllegalArgumentException(
						"Illegal character in Base64 encoded data.");
			int b0 = map2[i0];
			int b1 = map2[i1];
			int b2 = map2[i2];
			int b3 = map2[i3];
			if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
				throw new IllegalArgumentException(
						"Illegal character in Base64 encoded data.");
			int o0 = (b0 << 2) | (b1 >>> 4);
			int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);
			int o2 = ((b2 & 3) << 6) | b3;
			out[op++] = (byte) o0;
			if (op < oLen)
				out[op++] = (byte) o1;
			if (op < oLen)
				out[op++] = (byte) o2;
		}
		return out;
	}

	// Dummy constructor.
	public BASE64Encoder() {
	}

} // end class Base64Coder


关于导入sun.misc.BASE64Encoder导致在Eclipse中编译错误的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于android – 如何获取sun.misc.BASE64Encoder类的JAR文件?、base64encode base64decode utf16to8 utf8to16、base64encode 编码原理、BASE64Encoder的相关知识,请在本站寻找。

本文标签: