⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.util.crypt;/** * Provides Base64 encoding and decoding as defined by RFC 2045. <p/> This class is taken from the * Apache commons-codec, and adjusted to fit the Wicket framework's needs, especially external * dependencies have been removed. * </p> * <p/> This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC * 2045 <cite>Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message * Bodies</cite> by Freed and Borenstein. * </p> *  * @author Apache Software Foundation * @since 1.2 */public class Base64{	/**	 * Chunk size per RFC 2045 section 6.8. <p/>	 * <p>	 * The {@value} character limit does not count the trailing CRLF, but counts all other	 * characters, including any equal signs.	 * </p>	 * 	 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a>	 */	static final int CHUNK_SIZE = 76;	/**	 * Chunk separator per RFC 2045 section 2.1.	 * 	 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a>	 */	static final byte[] CHUNK_SEPARATOR = "\r\n".getBytes();	/**	 * The base length.	 */	static final int BASELENGTH = 255;	/**	 * Lookup length.	 */	static final int LOOKUPLENGTH = 64;	/**	 * Used to calculate the number of bits in a byte.	 */	static final int EIGHTBIT = 8;	/**	 * Used when encoding something which has fewer than 24 bits.	 */	static final int SIXTEENBIT = 16;	/**	 * Used to determine how many bits data contains.	 */	static final int TWENTYFOURBITGROUP = 24;	/**	 * Used to get the number of Quadruples.	 */	static final int FOURBYTE = 4;	/**	 * Used to test the sign of a byte.	 */	static final int SIGN = -128;	/**	 * Byte used to pad output.	 */	static final byte PAD = (byte)'=';	/**	 * Contains the Base64 values <code>0</code> through <code>63</code> accessed by using	 * character encodings as indices. <p/> For example, <code>base64Alphabet['+']</code> returns	 * <code>62</code>.	 * </p>	 * <p/> The value of undefined encodings is <code>-1</code>.	 * </p>	 */	private static byte[] base64Alphabet = new byte[BASELENGTH];	/**	 * <p/> Contains the Base64 encodings <code>A</code> through <code>Z</code>, followed by	 * <code>a</code> through <code>z</code>, followed by <code>0</code> through	 * <code>9</code>, followed by <code>+</code>, and <code>/</code>.	 * </p>	 * <p/> This array is accessed by using character values as indices.	 * </p>	 * <p/> For example, <code>lookUpBase64Alphabet[62] </code> returns <code>'+'</code>.	 * </p>	 */	private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];	// Populating the lookup and character arrays	static	{		for (int i = 0; i < BASELENGTH; i++)		{			base64Alphabet[i] = (byte)-1;		}		for (int i = 'Z'; i >= 'A'; i--)		{			base64Alphabet[i] = (byte)(i - 'A');		}		for (int i = 'z'; i >= 'a'; i--)		{			base64Alphabet[i] = (byte)(i - 'a' + 26);		}		for (int i = '9'; i >= '0'; i--)		{			base64Alphabet[i] = (byte)(i - '0' + 52);		}		base64Alphabet['+'] = 62;		base64Alphabet['/'] = 63;		for (int i = 0; i <= 25; i++)		{			lookUpBase64Alphabet[i] = (byte)('A' + i);		}		for (int i = 26, j = 0; i <= 51; i++, j++)		{			lookUpBase64Alphabet[i] = (byte)('a' + j);		}		for (int i = 52, j = 0; i <= 61; i++, j++)		{			lookUpBase64Alphabet[i] = (byte)('0' + j);		}		lookUpBase64Alphabet[62] = (byte)'+';		lookUpBase64Alphabet[63] = (byte)'/';	}	/**	 * Returns whether or not the <code>octet</code> is in the base 64 alphabet.	 * 	 * @param octet	 *            The value to test	 * @return <code>true</code> if the value is defined in the the base 64 alphabet,	 *         <code>false</code> otherwise.	 */	private static boolean isBase64(byte octet)	{		if (octet == PAD)		{			return true;		}		else if (octet < 0 || base64Alphabet[octet] == -1)		{			return false;		}		else		{			return true;		}	}	/**	 * Tests a given byte array to see if it contains only valid characters within the Base64	 * alphabet.	 * 	 * @param arrayOctect	 *            byte array to test	 * @return <code>true</code> if all bytes are valid characters in the Base64 alphabet or if	 *         the byte array is empty; false, otherwise	 */	public static boolean isArrayByteBase64(byte[] arrayOctect)	{		arrayOctect = discardWhitespace(arrayOctect);		int length = arrayOctect.length;		if (length == 0)		{			// shouldn't a 0 length array be valid base64 data?			// return false;			return true;		}		for (int i = 0; i < length; i++)		{			if (!isBase64(arrayOctect[i]))			{				return false;			}		}		return true;	}	/**	 * Encodes binary data using the base64 algorithm but does not chunk the output.	 * 	 * @param binaryData	 *            binary data to encode	 * @return Base64 characters	 */	public static byte[] encodeBase64(byte[] binaryData)	{		return encodeBase64(binaryData, false);	}	/**	 * Encodes binary data using the base64 algorithm and chunks the encoded output into 76	 * character blocks	 * 	 * @param binaryData	 *            binary data to encode	 * @return Base64 characters chunked in 76 character blocks	 */	public static byte[] encodeBase64Chunked(byte[] binaryData)	{		return encodeBase64(binaryData, true);	}	/**	 * Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the	 * requirements of the Decoder interface, and will throw a DecoderException if the supplied	 * object is not of type byte[].	 * 	 * @param pObject	 *            Object to decode	 * @return An object (of type byte[]) containing the binary data which corresponds to the byte[]	 *         supplied.	 * @throws IllegalArgumentException	 *             if the parameter supplied is not of type byte[]	 */	public Object decode(Object pObject)	{		if (!(pObject instanceof byte[]))		{			throw new IllegalArgumentException(					"Parameter supplied to Base64 decode is not a byte[]");		}		return decode((byte[])pObject);	}	/**	 * Decodes a byte[] containing containing characters in the Base64 alphabet.	 * 	 * @param pArray	 *            A byte array containing Base64 character data	 * @return a byte array containing binary data	 */	public byte[] decode(byte[] pArray)	{		return decodeBase64(pArray);	}	/**	 * Encodes binary data using the base64 algorithm, optionally chunking the output into 76	 * character blocks.	 * 	 * @param binaryData	 *            Array containing binary data to encode.	 * @param isChunked	 *            if <code>true</code> this encoder will chunk the base64 output into 76 character	 *            blocks	 * @return Base64-encoded data.	 */	public static byte[] encodeBase64(byte[] binaryData, boolean isChunked)	{		int lengthDataBits = binaryData.length * EIGHTBIT;		int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;		int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;		byte encodedData[] = null;		int encodedDataLength = 0;		int nbrChunks = 0;		if (fewerThan24bits != 0)		{			// data not divisible by 24 bit			encodedDataLength = (numberTriplets + 1) * 4;		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -