sun base64解码实现

java 代码
  1. // Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2007-6-11 11:45:48   
  2. // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!   
  3. // Decompiler options: packimports(3)    
  4. // Source File Name:   CharacterDecoder.java   
  5.   
  6. package sun.misc;   
  7.   
  8. import java.io.*;   
  9. import java.nio.ByteBuffer;   
  10.   
  11. // Referenced classes of package sun.misc:   
  12. //            CEStreamExhausted   
  13.   
  14. public abstract class CharacterDecoder   
  15. {   
  16.   
  17.     public CharacterDecoder()   
  18.     {   
  19.     }   
  20.   
  21.     protected abstract int bytesPerAtom();   
  22.   
  23.     protected abstract int bytesPerLine();   
  24.   
  25.     protected void decodeBufferPrefix(PushbackInputStream pushbackinputstream, OutputStream outputstream)   
  26.         throws IOException   
  27.     {   
  28.     }   
  29.   
  30.     protected void decodeBufferSuffix(PushbackInputStream pushbackinputstream, OutputStream outputstream)   
  31.         throws IOException   
  32.     {   
  33.     }   
  34.   
  35.     protected int decodeLinePrefix(PushbackInputStream pushbackinputstream, OutputStream outputstream)   
  36.         throws IOException   
  37.     {   
  38.         return bytesPerLine();   
  39.     }   
  40.   
  41.     protected void decodeLineSuffix(PushbackInputStream pushbackinputstream, OutputStream outputstream)   
  42.         throws IOException   
  43.     {   
  44.     }   
  45.   
  46.     protected void decodeAtom(PushbackInputStream pushbackinputstream, OutputStream outputstream, int i)   
  47.         throws IOException   
  48.     {   
  49.         throw new CEStreamExhausted();   
  50.     }   
  51.   
  52.     protected int readFully(InputStream inputstream, byte abyte0[], int i, int j)   
  53.         throws IOException   
  54.     {   
  55.         for(int k = 0; k < j; k++)   
  56.         {   
  57.             int l = inputstream.read();   
  58.             if(l == -1)   
  59.                 return k != 0 ? k : -1;   
  60.             abyte0[k + i] = (byte)l;   
  61.         }   
  62.   
  63.         return j;   
  64.     }   
  65.   
  66.     public void decodeBuffer(InputStream inputstream, OutputStream outputstream)   
  67.         throws IOException   
  68.     {   
  69.         int j = 0;   
  70.         PushbackInputStream pushbackinputstream = new PushbackInputStream(inputstream);   
  71.         decodeBufferPrefix(pushbackinputstream, outputstream);   
  72.         try  
  73.         {   
  74.             do  
  75.             {   
  76.                 int k = decodeLinePrefix(pushbackinputstream, outputstream);   
  77.                 int i;   
  78.                 for(i = 0; i + bytesPerAtom() < k; i += bytesPerAtom())   
  79.                 {   
  80.                     decodeAtom(pushbackinputstream, outputstream, bytesPerAtom());   
  81.                     j += bytesPerAtom();   
  82.                 }   
  83.   
  84.                 if(i + bytesPerAtom() == k)   
  85.                 {   
  86.                     decodeAtom(pushbackinputstream, outputstream, bytesPerAtom());   
  87.                     j += bytesPerAtom();   
  88.                 } else  
  89.                 {   
  90.                     decodeAtom(pushbackinputstream, outputstream, k - i);   
  91.                     j += k - i;   
  92.                 }   
  93.                 decodeLineSuffix(pushbackinputstream, outputstream);   
  94.             } while(true);   
  95.         }   
  96.         catch(CEStreamExhausted cestreamexhausted)   
  97.         {   
  98.             decodeBufferSuffix(pushbackinputstream, outputstream);   
  99.         }   
  100.     }   
  101.   
  102.     public byte[] decodeBuffer(String s)   
  103.         throws IOException   
  104.     {   
  105.         byte abyte0[] = new byte[s.length()];   
  106.         s.getBytes(0, s.length(), abyte0, 0);   
  107.         ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);   
  108.         ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();   
  109.         decodeBuffer(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));   
  110.         return bytearrayoutputstream.toByteArray();   
  111.     }   
  112.   
  113.     public byte[] decodeBuffer(InputStream inputstream)   
  114.         throws IOException   
  115.     {   
  116.         ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();   
  117.         decodeBuffer(inputstream, ((OutputStream) (bytearrayoutputstream)));   
  118.         return bytearrayoutputstream.toByteArray();   
  119.     }   
  120.   
  121.     public ByteBuffer decodeBufferToByteBuffer(String s)   
  122.         throws IOException   
  123.     {   
  124.         return ByteBuffer.wrap(decodeBuffer(s));   
  125.     }   
  126.   
  127.     public ByteBuffer decodeBufferToByteBuffer(InputStream inputstream)   
  128.         throws IOException   
  129.     {   
  130.         return ByteBuffer.wrap(decodeBuffer(inputstream));   
  131.     }   
  132. }   
  133.   
  134.   
  135. // Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2007-6-11 11:45:29   
  136. // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!   
  137. // Decompiler options: packimports(3)    
  138. // Source File Name:   BASE64Decoder.java   
  139.   
  140. package sun.misc;   
  141.   
  142. import java.io.*;   
  143.   
  144. // Referenced classes of package sun.misc:   
  145. //            CharacterDecoder, CEFormatException, CEStreamExhausted   
  146.   
  147. public class BASE64Decoder extends CharacterDecoder   
  148. {   
  149.   
  150.     public BASE64Decoder()   
  151.     {   
  152.         decode_buffer = new byte[4];   
  153.     }   
  154.   
  155.     protected int bytesPerAtom()   
  156.     {   
  157.         return 4;   
  158.     }   
  159.   
  160.     protected int bytesPerLine()   
  161.     {   
  162.         return 72;   
  163.     }   
  164.   
  165.     protected void decodeAtom(PushbackInputStream pushbackinputstream, OutputStream outputstream, int i)   
  166.         throws IOException   
  167.     {   
  168.         byte byte0 = -1;   
  169.         byte byte1 = -1;   
  170.         byte byte2 = -1;   
  171.         byte byte3 = -1;   
  172.         if(i < 2)   
  173.             throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");   
  174.         int j;   
  175.         do  
  176.         {   
  177.             j = pushbackinputstream.read();   
  178.             if(j == -1)   
  179.                 throw new CEStreamExhausted();   
  180.         } while(j == 10 || j == 13);   
  181.         decode_buffer[0] = (byte)j;   
  182.         j = readFully(pushbackinputstream, decode_buffer, 1, i - 1);   
  183.         if(j == -1)   
  184.             throw new CEStreamExhausted();   
  185.         if(i > 3 && decode_buffer[3] == 61)   
  186.             i = 3;   
  187.         if(i > 2 && decode_buffer[2] == 61)   
  188.             i = 2;   
  189.         switch(i)   
  190.         {   
  191.         case 4// '\004'   
  192.             byte3 = pem_convert_array[decode_buffer[3] & 0xff];   
  193.             // fall through   
  194.   
  195.         case 3// '\003'   
  196.             byte2 = pem_convert_array[decode_buffer[2] & 0xff];   
  197.             // fall through   
  198.   
  199.         case 2// '\002'   
  200.             byte1 = pem_convert_array[decode_buffer[1] & 0xff];   
  201.             byte0 = pem_convert_array[decode_buffer[0] & 0xff];   
  202.             // fall through   
  203.   
  204.         default:   
  205.             switch(i)   
  206.             {   
  207.             case 2// '\002'   
  208.                 outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));   
  209.                 break;   
  210.   
  211.             case 3// '\003'   
  212.                 outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));   
  213.                 outputstream.write((byte)(byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));   
  214.                 break;   
  215.   
  216.             case 4// '\004'   
  217.                 outputstream.write((byte)(byte0 << 2 & 0xfc | byte1 >>> 4 & 3));   
  218.                 outputstream.write((byte)(byte1 << 4 & 0xf0 | byte2 >>> 2 & 0xf));   
  219.                 outputstream.write((byte)(byte2 << 6 & 0xc0 | byte3 & 0x3f));   
  220.                 break;   
  221.             }   
  222.             break;   
  223.         }   
  224.     }   
  225.   
  226.     private static final char pem_array[] = {   
  227.         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',    
  228.         'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',    
  229.         'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',    
  230.         'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',    
  231.         'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',    
  232.         'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',    
  233.         '8', '9', '+', '/'   
  234.     };   
  235.     private static final byte pem_convert_array[];   
  236.     byte decode_buffer[];   
  237.   
  238.     static    
  239.     {   
  240.         pem_convert_array = new byte[256];   
  241.         for(int i = 0; i < 255; i++)   
  242.             pem_convert_array[i] = -1;   
  243.   
  244.         for(int j = 0; j < pem_array.length; j++)   
  245.             pem_convert_array[pem_array[j]] = (byte)j;   
  246.   
  247.     }   
  248. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用Base64类来进行图片的Base64编码和解码操作。推荐使用java.util.Base64类进行操作,而不是使用sun.misc.BASE64Encoder和sun.misc.BASE64Decoder类,因为后者在Java 8中已被标记为过时。 要将图片文件转换为Base64编码字符串,可以使用以下代码: ```java import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Base64; public class ImageBase64Encoder { public static String getImageFileToBase64(String imgFile) { try { Path path = Paths.get(imgFile); byte[] imageBytes = Files.readAllBytes(path); return Base64.getEncoder().encodeToString(imageBytes); } catch (IOException e) { e.printStackTrace(); return null; } } } ``` 要将Base64编码字符串转换为图片文件,可以使用以下代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.util.Base64; public class ImageBase64Decoder { public static boolean getBase64ToImageFile(String base64String, String imgPath) { try { byte[] imageBytes = Base64.getDecoder().decode(base64String); FileOutputStream outputStream = new FileOutputStream(imgPath); outputStream.write(imageBytes); outputStream.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } } ``` 以上代码示例中,ImageBase64Encoder类包含了一个静态方法`getImageFileToBase64`用于将图片文件转换为Base64编码字符串;ImageBase64Decoder类包含了一个静态方法`getBase64ToImageFile`用于将Base64编码字符串转换为图片文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值