/** * 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 a URL-safe variation of the base64 algorithm but does not chunk the * output. The url-safe variation emits - and _ instead of + and / characters. * * @param binaryData * binary data to encode * @return byte[] containing Base64 characters in their UTF-8 representation. * @since 1.4 */ public static byte[] encodeBase64URLSafe(final byte[] binaryData) { return encodeBase64(binaryData, false, true); }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 * alphabet. Currently the method treats whitespace as valid. * * @param arrayOctet * 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(final byte[] arrayOctet) { for (byte octet : arrayOctet) { if (!isBase64(octet) && !isWhiteSpace(octet)) { return false; } } 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; }
reset(); if ((pArray == null) || (pArray.length == 0)) long len = getEncodeLength(pArray, lineLength, lineSeparator); byte[] buf = new byte[(int)len]; setInitialBuffer(buf, 0, buf.length); encode(pArray, 0, pArray.length); encode(pArray, 0, -1); // Notify encoder of EOF. readResults(buf, 0, buf.length); if (isUrlSafe() && (pos < buf.length))
/** * 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(final byte[] pArray) { reset(); if ((pArray == null) || (pArray.length == 0)) { return pArray; } long len = Math.max(1, (pArray.length * 3) / 4); byte[] buf = new byte[(int)len]; setInitialBuffer(buf, 0, buf.length); decode(pArray, 0, pArray.length); decode(pArray, 0, -1); // Notify decoder of EOF. // Would be nice to just return buf (like we sometimes do in the encode // logic), but we have no idea what the line-length was (could even be // variable). So we cannot determine ahead of time exactly how big an // array is necessary. Hence the need to construct a 2nd byte array to // hold the final result: byte[] result = new byte[pos]; readResults(result, 0, result.length); return result; }
long len = getEncodeLength(binaryData, CHUNK_SIZE, CHUNK_SEPARATOR); if (len > maxResultSize) Base64 b64 = isChunked ? new Base64(urlSafe) : new Base64(0, CHUNK_SEPARATOR, urlSafe); return b64.encode(binaryData);
/** * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature * * @param bigInt * a BigInteger * @return A byte array containing base64 character data * @throws NullPointerException * if null is passed in * @since 1.4 */ public static byte[] encodeInteger(final BigInteger bigInt) { if (bigInt == null) { throw new NullPointerException("encodeInteger called with null parameter"); } return encodeBase64(toIntegerBytes(bigInt), false); }
/** * 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); }
/** * Encodes an Object using the base64 algorithm. This method is provided in order to satisfy the * requirements of the Encoder interface, and will throw an EncoderException if the supplied * object is not of type byte[]. * * @param pObject * Object to encode * @return An object (of type byte[]) containing the base64 encoded data which corresponds to * the byte[] supplied. * @throws IllegalArgumentException * if the parameter supplied is not of type byte[] */ public Object encode(Object pObject) { if (!(pObject instanceof byte[])) { throw new IllegalArgumentException( "Parameter supplied to Base64 encode is not a byte[]"); } return encode((byte[])pObject); }
/** * Tests a given byte array to see if it contains only valid characters within the Base64 * alphabet. * * @param arrayOctet * byte array to test * @return <code>true</code> if any byte is a valid character in the Base64 alphabet; false * herwise */ private static boolean containsBase64Byte(final byte[] arrayOctet) { for (byte octet : arrayOctet) { if (isBase64(octet)) { return true; } } return false; }
base64Data = discardNonBase64(base64Data);
/** * 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; }
/** * 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); }
/** * Encodes an Object using the base64 algorithm. This method is provided in order to satisfy the * requirements of the Encoder interface, and will throw an EncoderException if the supplied * object is not of type byte[]. * * @param pObject * Object to encode * @return An object (of type byte[]) containing the base64 encoded data which corresponds to * the byte[] supplied. * @throws IllegalArgumentException * if the parameter supplied is not of type byte[] */ public Object encode(Object pObject) { if (!(pObject instanceof byte[])) { throw new IllegalArgumentException( "Parameter supplied to Base64 encode is not a byte[]"); } return encode((byte[])pObject); }
/** * Discards any characters outside of the base64 alphabet, per the requirements on page 25 of * RFC 2045 - "Any characters outside of the base64 alphabet are to be ignored in base64 encoded * data." * * @param data * The base-64 encoded data to groom * @return The data, less non-base64 characters (see RFC 2045). */ static byte[] discardNonBase64(byte[] data) { byte groomedData[] = new byte[data.length]; int bytesCopied = 0; for (int i = 0; i < data.length; i++) { if (isBase64(data[i])) { groomedData[bytesCopied++] = data[i]; } } byte packedData[] = new byte[bytesCopied]; System.arraycopy(groomedData, 0, packedData, 0, bytesCopied); return packedData; }
base64Data = discardNonBase64(base64Data);
/** * 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 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); }