Android 自用Base64编码类
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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | /** * Created by tab on 2017/10/10 */ public class Base64Encoder extends FilterOutputStream { private static final char [] chars = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '+' , '/' }; private int charCount; private int carryOver; /*** * Constructs a new Base64 encoder that writes output to the given * OutputStream. * * @param out the output stream */ public Base64Encoder(OutputStream out) { super (out); } /*** * Writes the given byte to the output stream in an encoded form. * * @throws IOException if an I/O error occurs */ public void write( int b) throws IOException { // Take 24-bits from three octets, translate into four encoded chars // Break lines at 76 chars // If necessary, pad with 0 bits on the right at the end // Use = signs as padding at the end to ensure encodedLength % 4 == 0 // Remove the sign bit, // thanks to Christian Schweingruber <chrigu@lorraine.ch> if (b < 0 ) { b += 256 ; } // First byte use first six bits, save last two bits if (charCount % 3 == 0 ) { int lookup = b >> 2 ; carryOver = b & 3 ; // last two bits out.write(chars[lookup]); } // Second byte use previous two bits and first four new bits, // save last four bits else if (charCount % 3 == 1 ) { int lookup = ((carryOver << 4 ) + (b >> 4 )) & 63 ; carryOver = b & 15 ; // last four bits out.write(chars[lookup]); } // Third byte use previous four bits and first two new bits, // then use last six new bits else if (charCount % 3 == 2 ) { int lookup = ((carryOver << 2 ) + (b >> 6 )) & 63 ; out.write(chars[lookup]); lookup = b & 63 ; // last six bits out.write(chars[lookup]); carryOver = 0 ; } charCount++; // Add newline every 76 output chars (that's 57 input chars) if (charCount % 57 == 0 ) { out.write( '\n' ); } } /*** * Writes the given byte array to the output stream in an encoded form. * * @param buf the data to be written * @param off the start offset of the data * @param len the length of the data * @throws IOException if an I/O error occurs */ public void write( byte [] buf, int off, int len) throws IOException { // This could of course be optimized for ( int i = 0 ; i < len; i++) { write(buf[off + i]); } } /*** * Closes the stream, this MUST be called to ensure proper padding is * written to the end of the output stream. * * @throws IOException if an I/O error occurs */ public void close() throws IOException { // Handle leftover bytes if (charCount % 3 == 1 ) { // one leftover int lookup = (carryOver << 4 ) & 63 ; out.write(chars[lookup]); out.write( '=' ); out.write( '=' ); } else if (charCount % 3 == 2 ) { // two leftovers int lookup = (carryOver << 2 ) & 63 ; out.write(chars[lookup]); out.write( '=' ); } super .close(); } /*** * Returns the encoded form of the given unencoded string. The encoder uses * the ISO-8859-1 (Latin-1) encoding to convert the string to bytes. For * greater control over the encoding, encode the string to bytes yourself * and use encode(byte[]). * * @param unencoded the string to encode * @return the encoded form of the unencoded string */ public static String encode(String unencoded) { byte [] bytes = null ; try { bytes = unencoded.getBytes( "UTF-8" ); } catch (UnsupportedEncodingException ignored) { } return encode(bytes); } /*** * Returns the encoded form of the given unencoded string. * * @param bytes the bytes to encode * @return the encoded form of the unencoded string */ public static String encode( byte [] bytes) { ByteArrayOutputStream out = new ByteArrayOutputStream( ( int ) (bytes.length * 1.37 )); Base64Encoder encodedOut = new Base64Encoder(out); try { encodedOut.write(bytes); encodedOut.close(); return out.toString( "UTF-8" ); } catch (IOException ignored) { return null ; } } /** * file 2 base64 * * @param filename filename * @return base64 string */ public static String file2Base64(String filename) { File file = new File(filename); ULogger.d( "filename=" + file.getAbsolutePath()); FileInputStream fileInputStream = null ; if (file.exists()) { try { fileInputStream = new FileInputStream(file); int len = ( int ) file.length(); byte bytes[] = new byte [len]; int count, total = 0 ; while ((count = fileInputStream.read(bytes, total, len - total)) > 0 ) { total += count; } return Base64.encodeToString(bytes, Base64.DEFAULT); } catch (Exception ex) { ex.printStackTrace(); } finally { if (fileInputStream != null ) { try { fileInputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } return null ; } public static void main(String[] args) throws Exception { if (args.length != 1 ) { System.err .println( "Usage: java com.oreilly.servlet.Base64Encoder fileToEncode" ); return ; } Base64Encoder encoder = null ; BufferedInputStream in = null ; try { encoder = new Base64Encoder(System.out); in = new BufferedInputStream( new FileInputStream(args[ 0 ])); byte [] buf = new byte [ 4 * 1024 ]; // 4K buffer int bytesRead; while ((bytesRead = in.read(buf)) != - 1 ) { encoder.write(buf, 0 , bytesRead); } } finally { if (in != null ) in.close(); if (encoder != null ) encoder.close(); } } } |
标签:
Android
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
2021-05-27 MMKV使用笔记