`
balaschen
  • 浏览: 190353 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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. }  

 

  • doc.rar (5.5 KB)
  • 下载次数: 40
  • Base64.rar (6.5 KB)
  • 描述: novell实现
  • 下载次数: 56
分享到:
评论

相关推荐

    sun的BASE64Decoder

    sun公司的base64解码编码,里面附带使用说明,尤其是在eclipse下如何使用

    java实现Base64加解密jar包

    BASE64Encoder.jar是用于java、android项目中Base64数据加解密工具,简单易用

    sun.misc.BASE64Decoder相关类

    sun.misc.BASEDecoder、sun.misc.BASEEncoder等 在编码解码是用到的工具类,导入jar包之后,就可以正常使用此类

    Kotlin Base64编解码类

    Kotlin Base64编解码类,可以对AES/DES的加密密文作编码和解码,防止出现加解密出错的问题。 encrypt before size: 16 encrypt after size: 24 使用DES作加密解密时,咱们会发现加密后的密码变长了,这样会直接导致...

    java Base64精简代码

    包含sun.misc.编码解码、org.apache.commons.codec.binary.Base64编码解码、自定义的、myBase64Encode/Dencode 以及对对几种方式的区别

    BASE64 jar包 + BASE64 传文件实例

    网上经常有用编码、解码来传图片的例子。但是都使用的是sun.misc.BASE64Encoder和sun.misc.BASE64Dncoder。虽然在rt.jar中,但是是内部使用的。 所以建议使用apache的commons-codec。

    Base64转图片.zip

    Java中实现图片转Base64,Base64转图片。SSM中直接使用Spring的jar包实现,Android客户端上传的base64会无法解码,使用这个Jar包可以解决这个问题。

    javabase64-1.3.1.zip

    Sun公司提供的Base64转码/解码工具,包含jar包和源代码。 Sun公司提供的Base64转码/解码工具,包含jar包和源代码。

    java RSA 加密签名工具包(开发)

    密文经过sun 内部的Base64Encoder编码成为字符串后返回密文字符串。解密的时候先使用Base64Decoder先解码密文,然后再解密。 数字签名也是同样道理。 该工具在jre7以及以上的环境能够更好的运行。 使用范例: ...

    HashCalc:用 Delphi XE7 编写的适用于 Win32、Win64 和 OS X 的文件哈希计算器

    在 OS X 上,您可能想要创建一个指向 /Applications/HashCalc.app/Contents/MacOS/HashCalc 的链接文件,以便您可以输入 hashcalc path/file 第二个选项卡上还有一个 Base64 文本编码器和解码器作为便利工具。...

    java-servlet-api.doc

    这是一份关于2.1版JavaServletAPI的说明文档,作为对这本文档的补充,你可以到http://java.sun.com/products/servlet/index.html下面下载Javadoc格式的文档。 谁需要读这份文档 这份文档描述了JavaServletAPI的最新...

Global site tag (gtag.js) - Google Analytics