📄 base64.java
字号:
// bytes are considered to be zero when absent. // the four bytes are then mapped to common ASCII symbols // A's: first six bits of first byte out.write(base64Chars[ inBuffer[0] >> 2 ]); if (inBuffer[1] != END_OF_INPUT){ // B's: last two bits of first byte, first four bits of second byte out.write(base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]); if (inBuffer[2] != END_OF_INPUT){ // C's: last four bits of second byte, first two bits of third byte out.write(base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]); // D's: last six bits of third byte out.write(base64Chars [inBuffer[2] & 0x3F]); } else { // C's: last four bits of second byte out.write(base64Chars [((inBuffer[1] << 2) & 0x3c)]); // an equals sign for a character that is not a Base64 character out.write('='); done = true; } } else { // B's: last two bits of first byte out.write(base64Chars [(( inBuffer[0] << 4 ) & 0x30)]); // an equal signs for characters that is not a Base64 characters out.write('='); out.write('='); done = true; } lineCount += 4; if (lineBreaks && lineCount >= 76){ out.write('\n'); lineCount = 0; } } if (lineBreaks && lineCount >= 1){ out.write('\n'); lineCount = 0; } out.flush(); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * The String is converted to and from bytes according to the platform's * default character encoding. * * @param string The data to decode. * @return A decoded String. * * @since ostermillerutils 1.00.00 */ public static String decode(String string){ return new String(decode(string.getBytes())); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param enc Character encoding to use when converting to and from bytes. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return A decoded String. * * @since ostermillerutils 1.00.00 */ public static String decode(String string, String enc) throws UnsupportedEncodingException { return new String(decode(string.getBytes(enc)), enc); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param encIn Character encoding to use when converting input to bytes (should not matter because Base64 data is designed to survive most character encodings) * @param encOut Character encoding to use when converting decoded bytes to output. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return A decoded String. * * @since ostermillerutils 1.00.00 */ public static String decode(String string, String encIn, String encOut) throws UnsupportedEncodingException { return new String(decode(string.getBytes(encIn)), encOut); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * The String is converted to and from bytes according to the platform's * default character encoding. * * @param string The data to decode. * @return A decoded String. * * @since ostermillerutils 1.02.16 */ public static String decodeToString(String string){ return new String(decode(string.getBytes())); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param enc Character encoding to use when converting to and from bytes. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return A decoded String. * * @since ostermillerutils 1.02.16 */ public static String decodeToString(String string, String enc) throws UnsupportedEncodingException { return new String(decode(string.getBytes(enc)), enc); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param encIn Character encoding to use when converting input to bytes (should not matter because Base64 data is designed to survive most character encodings) * @param encOut Character encoding to use when converting decoded bytes to output. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return A decoded String. * * @since ostermillerutils 1.02.16 */ public static String decodeToString(String string, String encIn, String encOut) throws UnsupportedEncodingException { return new String(decode(string.getBytes(encIn)), encOut); } /** * Decode a Base64 encoded String to an OutputStream. * Characters that are not part of the Base64 alphabet are ignored * in the input. * The String is converted from bytes according to the platform's * default character encoding. * * @param string The data to decode. * @param out Stream to which to write decoded data. * @throws IOException if an IO error occurs. * * @since ostermillerutils 1.02.16 */ public static void decodeToStream(String string, OutputStream out) throws IOException { decode(new ByteArrayInputStream(string.getBytes()), out); } /** * Decode a Base64 encoded String to an OutputStream. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param enc Character encoding to use when converting to and from bytes. * @param out Stream to which to write decoded data. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @throws IOException if an IO error occurs. * * @since ostermillerutils 1.02.16 */ public static void decodeToStream(String string, String enc, OutputStream out) throws UnsupportedEncodingException, IOException { decode(new ByteArrayInputStream(string.getBytes(enc)), out); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * The String is converted from bytes according to the platform's * default character encoding. * * @param string The data to decode. * @return decoded data. * * @since ostermillerutils 1.02.16 */ public static byte[] decodeToBytes(String string){ return decode(string.getBytes()); } /** * Decode a Base64 encoded String. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param string The data to decode. * @param enc Character encoding to use when converting from bytes. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return decoded data. * * @since ostermillerutils 1.02.16 */ public static byte[] decodeToBytes(String string, String enc) throws UnsupportedEncodingException { return decode(string.getBytes(enc)); } /** * Decode Base64 encoded bytes. * Characters that are not part of the Base64 alphabet are ignored * in the input. * The String is converted to bytes according to the platform's * default character encoding. * * @param bytes The data to decode. * @return A decoded String. * * @since ostermillerutils 1.02.16 */ public static String decodeToString(byte[] bytes){ return new String(decode(bytes)); } /** * Decode Base64 encoded bytes. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param bytes The data to decode. * @param enc Character encoding to use when converting to and from bytes. * @throws UnsupportedEncodingException if the character encoding specified is not supported. * @return A decoded String. * * @since ostermillerutils 1.02.16 */ public static String decodeToString(byte[] bytes, String enc) throws UnsupportedEncodingException { return new String(decode(bytes), enc); } /** * Decode Base64 encoded bytes. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param bytes The data to decode. * @return Decoded bytes. * * @since ostermillerutils 1.02.16 */ public static byte[] decodeToBytes(byte[] bytes){ return decode(bytes); } /** * Decode Base64 encoded bytes. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param bytes The data to decode. * @return Decoded bytes. * * @since ostermillerutils 1.00.00 */ public static byte[] decode(byte[] bytes){ ByteArrayInputStream in = new ByteArrayInputStream(bytes); // calculate the length of the resulting output. // in general it will be at most 3/4 the size of the input // but the input length must be divisible by four. // If it isn't the next largest size that is divisible // by four is used. int mod; int length = bytes.length; if ((mod = length % 4) != 0){ length += 4 - mod; } length = length * 3 / 4; ByteArrayOutputStream out = new ByteArrayOutputStream(length); try { decode(in, out, false); } catch (IOException x){ // This can't happen. // The input and output streams were constructed // on memory structures that don't actually use IO. throw new RuntimeException(x); } return out.toByteArray(); } /** * Decode Base64 encoded bytes to the an OutputStream. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param bytes The data to decode. * @param out Stream to which to write decoded data. * @throws IOException if an IO error occurs. * @return Decoded bytes. * * @since ostermillerutils 1.00.00 */ public static void decode(byte[] bytes, OutputStream out) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(bytes); decode(in, out, false); } /** * Decode Base64 encoded bytes to the an OutputStream. * Characters that are not part of the Base64 alphabet are ignored * in the input. * * @param bytes The data to decode. * @param out Stream to which to write decoded data. * @throws IOException if an IO error occurs. * @return Decoded bytes. * * @since ostermillerutils 1.02.16 */ public static void decodeToStream(byte[] bytes, OutputStream out) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(bytes); decode(in, out, false); } /** * Decode Base64 encoded data from one file to the other. * Characters in the Base64 alphabet, white space and equals sign are * expected to be in urlencoded data. The presence of other characters * could be a sign that the data is corrupted. * * @param fIn File to be decoded (will be overwritten). * @throws IOException if an IO error occurs. * @throws Base64DecodingException if unexpected data is encountered. * * @since ostermillerutils 1.00.00 */ public static void decode(File fIn) throws IOException { decode(fIn, fIn, true); } /** * Decode Base64 encoded data from one file to the other. * Characters in the Base64 alphabet, white space and equals sign are * expected to be in urlencoded data. The presence of other characters * could be a sign that the data is corrupted. * * @param fIn File to be decoded (will be overwritten). * @param throwExceptions Whether to throw exceptions when unexpected data is encountered. * @throws IOException if an IO error occurs. * @throws Base64DecodingException if unexpected data is encountered when throwExceptions is specified. * * @since ostermillerutils 1.00.00 */ public static void decode(File fIn, boolean throwExceptions) throws IOException { decode(fIn, fIn, throwExceptions); } /** * Decode Base64 encoded data from one file to the other. * Characters in the Base64 alphabet, white space and equals sign are * expected to be in urlencoded data. The presence of other characters * could be a sign that the data is corrupted. * * @param fIn File to be decoded. * @param fOut File to which the results should be written (may be the same as fIn). * @throws IOException if an IO error occurs. * @throws Base64DecodingException if unexpected data is encountered. * * @since ostermillerutils 1.00.00 */ public static void decode(File fIn, File fOut) throws IOException { decode(fIn, fOut, true); } /** * Decode Base64 encoded data from one file to the other. * Characters in the Base64 alphabet, white space and equals sign are * expected to be in urlencoded data. The presence of other characters
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -