snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#8566

Java

GWT Base64

/*
Copyright (c) 2009, Mark Renouf
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY Mark Renouf ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
 * Custom Base64 encode/decode implementation suitable for use in
 * GWT applications (uses only translatable classes). 
 */
public class Base64 {

    private static final String etab =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    private static byte[] dtab = {
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
        -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
        -1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,
        55,56,57,58,59,60,61,-1,-1,-1,64,-1,-1,-1, 0, 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,-1,-1,-1,-1,-1,-1,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,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
    };

    public static String decode(String data) {
        StringBuffer out = new StringBuffer();
        
        // length must be multiple of 4 (with padding)
        if (data.length() % 4 != 0)
            return "";

        for (int i = 0; i < data.length();) {
            byte e0 = dtab[data.charAt(i++) & 0x7f];
            byte e1 = dtab[data.charAt(i++) & 0x7f];
            byte e2 = dtab[data.charAt(i++) & 0x7f];
            byte e3 = dtab[data.charAt(i++) & 0x7f];
            
            // Invalid characters in input
            if (e0 == -1 || e1 == -1 || e2 == -1 || e3 == -1)
                return "";
            
            byte d0 = (byte) ((e0 << 2) + ((e1 >>> 4) & 0x03));
            byte d1 = (byte) ((e1 << 4) + ((e2 >>> 2) & 0x0f));
            byte d2 = (byte) ((e2 << 6) + (e3 & 0x3f));
            
            out.append(Character.toString((char) d0));
            if (e2 != 64)
                out.append(Character.toString((char) d1));
            if (e3 != 64)
                out.append(Character.toString((char) d2));
        }
        return out.toString();
    }
    
    public static String encode(String data) {
        StringBuffer out = new StringBuffer();
        
        int i = 0;
        int r = data.length();
        while (r > 0) {
            byte d0, d1, d2;
            byte e0, e1, e2, e3;
            
            d0 = (byte) data.charAt(i++); --r;
            e0 = (byte) (d0 >>> 2);
            e1 = (byte) ((d0 & 0x03) << 4);
            
            if (r > 0) {
                d1 = (byte) data.charAt(i++); --r;
                e1 += (byte) (d1 >>> 4);
                e2 = (byte) ((d1 & 0x0f) << 2);
            }
            else {
                e2 = 64;
            }
            
            if (r > 0) {
                d2 = (byte) data.charAt(i++); --r;
                e2 += (byte) (d2 >>> 6);
                e3 = (byte) (d2 & 0x3f);
            }
            else {
                e3 = 64;
            }
            out.append(etab.charAt(e0));
            out.append(etab.charAt(e1));
            out.append(etab.charAt(e2));
            out.append(etab.charAt(e3));
        }

        return out.toString();
    }
}
https://snipt.net/embed/dc9c3175ca1fb6d1431b51360125cbbe/
https://snipt.net/raw/dc9c3175ca1fb6d1431b51360125cbbe/
dc9c3175ca1fb6d1431b51360125cbbe
java
Java
116
2014-04-25T00:36:33
True
False
/api/public/snipt/8566/
gwt-base64
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10"> 10</a> <a href="#L-11"> 11</a> <a href="#L-12"> 12</a> <a href="#L-13"> 13</a> <a href="#L-14"> 14</a> <a href="#L-15"> 15</a> <a href="#L-16"> 16</a> <a href="#L-17"> 17</a> <a href="#L-18"> 18</a> <a href="#L-19"> 19</a> <a href="#L-20"> 20</a> <a href="#L-21"> 21</a> <a href="#L-22"> 22</a> <a href="#L-23"> 23</a> <a href="#L-24"> 24</a> <a href="#L-25"> 25</a> <a href="#L-26"> 26</a> <a href="#L-27"> 27</a> <a href="#L-28"> 28</a> <a href="#L-29"> 29</a> <a href="#L-30"> 30</a> <a href="#L-31"> 31</a> <a href="#L-32"> 32</a> <a href="#L-33"> 33</a> <a href="#L-34"> 34</a> <a href="#L-35"> 35</a> <a href="#L-36"> 36</a> <a href="#L-37"> 37</a> <a href="#L-38"> 38</a> <a href="#L-39"> 39</a> <a href="#L-40"> 40</a> <a href="#L-41"> 41</a> <a href="#L-42"> 42</a> <a href="#L-43"> 43</a> <a href="#L-44"> 44</a> <a href="#L-45"> 45</a> <a href="#L-46"> 46</a> <a href="#L-47"> 47</a> <a href="#L-48"> 48</a> <a href="#L-49"> 49</a> <a href="#L-50"> 50</a> <a href="#L-51"> 51</a> <a href="#L-52"> 52</a> <a href="#L-53"> 53</a> <a href="#L-54"> 54</a> <a href="#L-55"> 55</a> <a href="#L-56"> 56</a> <a href="#L-57"> 57</a> <a href="#L-58"> 58</a> <a href="#L-59"> 59</a> <a href="#L-60"> 60</a> <a href="#L-61"> 61</a> <a href="#L-62"> 62</a> <a href="#L-63"> 63</a> <a href="#L-64"> 64</a> <a href="#L-65"> 65</a> <a href="#L-66"> 66</a> <a href="#L-67"> 67</a> <a href="#L-68"> 68</a> <a href="#L-69"> 69</a> <a href="#L-70"> 70</a> <a href="#L-71"> 71</a> <a href="#L-72"> 72</a> <a href="#L-73"> 73</a> <a href="#L-74"> 74</a> <a href="#L-75"> 75</a> <a href="#L-76"> 76</a> <a href="#L-77"> 77</a> <a href="#L-78"> 78</a> <a href="#L-79"> 79</a> <a href="#L-80"> 80</a> <a href="#L-81"> 81</a> <a href="#L-82"> 82</a> <a href="#L-83"> 83</a> <a href="#L-84"> 84</a> <a href="#L-85"> 85</a> <a href="#L-86"> 86</a> <a href="#L-87"> 87</a> <a href="#L-88"> 88</a> <a href="#L-89"> 89</a> <a href="#L-90"> 90</a> <a href="#L-91"> 91</a> <a href="#L-92"> 92</a> <a href="#L-93"> 93</a> <a href="#L-94"> 94</a> <a href="#L-95"> 95</a> <a href="#L-96"> 96</a> <a href="#L-97"> 97</a> <a href="#L-98"> 98</a> <a href="#L-99"> 99</a> <a href="#L-100">100</a> <a href="#L-101">101</a> <a href="#L-102">102</a> <a href="#L-103">103</a> <a href="#L-104">104</a> <a href="#L-105">105</a> <a href="#L-106">106</a> <a href="#L-107">107</a> <a href="#L-108">108</a> <a href="#L-109">109</a> <a href="#L-110">110</a> <a href="#L-111">111</a> <a href="#L-112">112</a> <a href="#L-113">113</a> <a href="#L-114">114</a> <a href="#L-115">115</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cm">/*</span> </span><span id="L-2"><a name="L-2"></a><span class="cm">Copyright (c) 2009, Mark Renouf</span> </span><span id="L-3"><a name="L-3"></a><span class="cm">All rights reserved.</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="cm">Redistribution and use in source and binary forms, with or without</span> </span><span id="L-6"><a name="L-6"></a><span class="cm">modification, are permitted provided that the following conditions are met:</span> </span><span id="L-7"><a name="L-7"></a><span class="cm"> * Redistributions of source code must retain the above copyright</span> </span><span id="L-8"><a name="L-8"></a><span class="cm"> notice, this list of conditions and the following disclaimer.</span> </span><span id="L-9"><a name="L-9"></a><span class="cm"> * Redistributions in binary form must reproduce the above copyright</span> </span><span id="L-10"><a name="L-10"></a><span class="cm"> notice, this list of conditions and the following disclaimer in the</span> </span><span id="L-11"><a name="L-11"></a><span class="cm"> documentation and/or other materials provided with the distribution.</span> </span><span id="L-12"><a name="L-12"></a><span class="cm"> * Neither the name of the &lt;organization&gt; nor the</span> </span><span id="L-13"><a name="L-13"></a><span class="cm"> names of its contributors may be used to endorse or promote products</span> </span><span id="L-14"><a name="L-14"></a><span class="cm"> derived from this software without specific prior written permission.</span> </span><span id="L-15"><a name="L-15"></a> </span><span id="L-16"><a name="L-16"></a><span class="cm">THIS SOFTWARE IS PROVIDED BY Mark Renouf &#39;&#39;AS IS&#39;&#39; AND ANY</span> </span><span id="L-17"><a name="L-17"></a><span class="cm">EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED</span> </span><span id="L-18"><a name="L-18"></a><span class="cm">WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span> </span><span id="L-19"><a name="L-19"></a><span class="cm">DISCLAIMED. IN NO EVENT SHALL &lt;copyright holder&gt; BE LIABLE FOR ANY</span> </span><span id="L-20"><a name="L-20"></a><span class="cm">DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES</span> </span><span id="L-21"><a name="L-21"></a><span class="cm">(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span> </span><span id="L-22"><a name="L-22"></a><span class="cm">LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND</span> </span><span id="L-23"><a name="L-23"></a><span class="cm">ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span> </span><span id="L-24"><a name="L-24"></a><span class="cm">(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span> </span><span id="L-25"><a name="L-25"></a><span class="cm">SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span> </span><span id="L-26"><a name="L-26"></a><span class="cm">*/</span> </span><span id="L-27"><a name="L-27"></a><span class="cm">/**</span> </span><span id="L-28"><a name="L-28"></a><span class="cm"> * Custom Base64 encode/decode implementation suitable for use in</span> </span><span id="L-29"><a name="L-29"></a><span class="cm"> * GWT applications (uses only translatable classes). </span> </span><span id="L-30"><a name="L-30"></a><span class="cm"> */</span> </span><span id="L-31"><a name="L-31"></a><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Base64</span> <span class="o">{</span> </span><span id="L-32"><a name="L-32"></a> </span><span id="L-33"><a name="L-33"></a> <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">String</span> <span class="n">etab</span> <span class="o">=</span> </span><span id="L-34"><a name="L-34"></a> <span class="s">&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=&quot;</span><span class="o">;</span> </span><span id="L-35"><a name="L-35"></a> </span><span id="L-36"><a name="L-36"></a> <span class="kd">private</span> <span class="kd">static</span> <span class="kt">byte</span><span class="o">[]</span> <span class="n">dtab</span> <span class="o">=</span> <span class="o">{</span> </span><span id="L-37"><a name="L-37"></a> <span class="o">-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span> </span><span id="L-38"><a name="L-38"></a> <span class="o">-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span> </span><span id="L-39"><a name="L-39"></a> <span class="o">-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span><span class="mi">62</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span><span class="mi">63</span><span class="o">,</span><span class="mi">52</span><span class="o">,</span><span class="mi">53</span><span class="o">,</span><span class="mi">54</span><span class="o">,</span> </span><span id="L-40"><a name="L-40"></a> <span class="mi">55</span><span class="o">,</span><span class="mi">56</span><span class="o">,</span><span class="mi">57</span><span class="o">,</span><span class="mi">58</span><span class="o">,</span><span class="mi">59</span><span class="o">,</span><span class="mi">60</span><span class="o">,</span><span class="mi">61</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span><span class="mi">64</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span> <span class="mi">0</span><span class="o">,</span> <span class="mi">1</span><span class="o">,</span> <span class="mi">2</span><span class="o">,</span> </span><span id="L-41"><a name="L-41"></a> <span class="mi">3</span><span class="o">,</span> <span class="mi">4</span><span class="o">,</span> <span class="mi">5</span><span class="o">,</span> <span class="mi">6</span><span class="o">,</span> <span class="mi">7</span><span class="o">,</span> <span class="mi">8</span><span class="o">,</span> <span class="mi">9</span><span class="o">,</span><span class="mi">10</span><span class="o">,</span><span class="mi">11</span><span class="o">,</span><span class="mi">12</span><span class="o">,</span><span class="mi">13</span><span class="o">,</span><span class="mi">14</span><span class="o">,</span><span class="mi">15</span><span class="o">,</span><span class="mi">16</span><span class="o">,</span><span class="mi">17</span><span class="o">,</span><span class="mi">18</span><span class="o">,</span><span class="mi">19</span><span class="o">,</span> </span><span id="L-42"><a name="L-42"></a> <span class="mi">20</span><span class="o">,</span><span class="mi">21</span><span class="o">,</span><span class="mi">22</span><span class="o">,</span><span class="mi">23</span><span class="o">,</span><span class="mi">24</span><span class="o">,</span><span class="mi">25</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,</span><span class="mi">26</span><span class="o">,</span><span class="mi">27</span><span class="o">,</span><span class="mi">28</span><span class="o">,</span><span class="mi">29</span><span class="o">,</span><span class="mi">30</span><span class="o">,</span> </span><span id="L-43"><a name="L-43"></a> <span class="mi">31</span><span class="o">,</span><span class="mi">32</span><span class="o">,</span><span class="mi">33</span><span class="o">,</span><span class="mi">34</span><span class="o">,</span><span class="mi">35</span><span class="o">,</span><span class="mi">36</span><span class="o">,</span><span class="mi">37</span><span class="o">,</span><span class="mi">38</span><span class="o">,</span><span class="mi">39</span><span class="o">,</span><span class="mi">40</span><span class="o">,</span><span class="mi">41</span><span class="o">,</span><span class="mi">42</span><span class="o">,</span><span class="mi">43</span><span class="o">,</span><span class="mi">44</span><span class="o">,</span><span class="mi">45</span><span class="o">,</span><span class="mi">46</span><span class="o">,</span><span class="mi">47</span><span class="o">,</span> </span><span id="L-44"><a name="L-44"></a> <span class="mi">48</span><span class="o">,</span><span class="mi">49</span><span class="o">,</span><span class="mi">50</span><span class="o">,</span><span class="mi">51</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span><span class="o">,-</span><span class="mi">1</span> </span><span id="L-45"><a name="L-45"></a> <span class="o">};</span> </span><span id="L-46"><a name="L-46"></a> </span><span id="L-47"><a name="L-47"></a> <span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span> <span class="nf">decode</span><span class="o">(</span><span class="n">String</span> <span class="n">data</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-48"><a name="L-48"></a> <span class="n">StringBuffer</span> <span class="n">out</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuffer</span><span class="o">();</span> </span><span id="L-49"><a name="L-49"></a> </span><span id="L-50"><a name="L-50"></a> <span class="c1">// length must be multiple of 4 (with padding)</span> </span><span id="L-51"><a name="L-51"></a> <span class="k">if</span> <span class="o">(</span><span class="n">data</span><span class="o">.</span><span class="na">length</span><span class="o">()</span> <span class="o">%</span> <span class="mi">4</span> <span class="o">!=</span> <span class="mi">0</span><span class="o">)</span> </span><span id="L-52"><a name="L-52"></a> <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="o">;</span> </span><span id="L-53"><a name="L-53"></a> </span><span id="L-54"><a name="L-54"></a> <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">data</span><span class="o">.</span><span class="na">length</span><span class="o">();)</span> <span class="o">{</span> </span><span id="L-55"><a name="L-55"></a> <span class="kt">byte</span> <span class="n">e0</span> <span class="o">=</span> <span class="n">dtab</span><span class="o">[</span><span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++)</span> <span class="o">&amp;</span> <span class="mh">0x7f</span><span class="o">];</span> </span><span id="L-56"><a name="L-56"></a> <span class="kt">byte</span> <span class="n">e1</span> <span class="o">=</span> <span class="n">dtab</span><span class="o">[</span><span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++)</span> <span class="o">&amp;</span> <span class="mh">0x7f</span><span class="o">];</span> </span><span id="L-57"><a name="L-57"></a> <span class="kt">byte</span> <span class="n">e2</span> <span class="o">=</span> <span class="n">dtab</span><span class="o">[</span><span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++)</span> <span class="o">&amp;</span> <span class="mh">0x7f</span><span class="o">];</span> </span><span id="L-58"><a name="L-58"></a> <span class="kt">byte</span> <span class="n">e3</span> <span class="o">=</span> <span class="n">dtab</span><span class="o">[</span><span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++)</span> <span class="o">&amp;</span> <span class="mh">0x7f</span><span class="o">];</span> </span><span id="L-59"><a name="L-59"></a> </span><span id="L-60"><a name="L-60"></a> <span class="c1">// Invalid characters in input</span> </span><span id="L-61"><a name="L-61"></a> <span class="k">if</span> <span class="o">(</span><span class="n">e0</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">e1</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">e2</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span> <span class="o">||</span> <span class="n">e3</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="o">)</span> </span><span id="L-62"><a name="L-62"></a> <span class="k">return</span> <span class="s">&quot;&quot;</span><span class="o">;</span> </span><span id="L-63"><a name="L-63"></a> </span><span id="L-64"><a name="L-64"></a> <span class="kt">byte</span> <span class="n">d0</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">((</span><span class="n">e0</span> <span class="o">&lt;&lt;</span> <span class="mi">2</span><span class="o">)</span> <span class="o">+</span> <span class="o">((</span><span class="n">e1</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">4</span><span class="o">)</span> <span class="o">&amp;</span> <span class="mh">0x03</span><span class="o">));</span> </span><span id="L-65"><a name="L-65"></a> <span class="kt">byte</span> <span class="n">d1</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">((</span><span class="n">e1</span> <span class="o">&lt;&lt;</span> <span class="mi">4</span><span class="o">)</span> <span class="o">+</span> <span class="o">((</span><span class="n">e2</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">2</span><span class="o">)</span> <span class="o">&amp;</span> <span class="mh">0x0f</span><span class="o">));</span> </span><span id="L-66"><a name="L-66"></a> <span class="kt">byte</span> <span class="n">d2</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">((</span><span class="n">e2</span> <span class="o">&lt;&lt;</span> <span class="mi">6</span><span class="o">)</span> <span class="o">+</span> <span class="o">(</span><span class="n">e3</span> <span class="o">&amp;</span> <span class="mh">0x3f</span><span class="o">));</span> </span><span id="L-67"><a name="L-67"></a> </span><span id="L-68"><a name="L-68"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">Character</span><span class="o">.</span><span class="na">toString</span><span class="o">((</span><span class="kt">char</span><span class="o">)</span> <span class="n">d0</span><span class="o">));</span> </span><span id="L-69"><a name="L-69"></a> <span class="k">if</span> <span class="o">(</span><span class="n">e2</span> <span class="o">!=</span> <span class="mi">64</span><span class="o">)</span> </span><span id="L-70"><a name="L-70"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">Character</span><span class="o">.</span><span class="na">toString</span><span class="o">((</span><span class="kt">char</span><span class="o">)</span> <span class="n">d1</span><span class="o">));</span> </span><span id="L-71"><a name="L-71"></a> <span class="k">if</span> <span class="o">(</span><span class="n">e3</span> <span class="o">!=</span> <span class="mi">64</span><span class="o">)</span> </span><span id="L-72"><a name="L-72"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">Character</span><span class="o">.</span><span class="na">toString</span><span class="o">((</span><span class="kt">char</span><span class="o">)</span> <span class="n">d2</span><span class="o">));</span> </span><span id="L-73"><a name="L-73"></a> <span class="o">}</span> </span><span id="L-74"><a name="L-74"></a> <span class="k">return</span> <span class="n">out</span><span class="o">.</span><span class="na">toString</span><span class="o">();</span> </span><span id="L-75"><a name="L-75"></a> <span class="o">}</span> </span><span id="L-76"><a name="L-76"></a> </span><span id="L-77"><a name="L-77"></a> <span class="kd">public</span> <span class="kd">static</span> <span class="n">String</span> <span class="n">encode</span><span class="o">(</span><span class="n">String</span> <span class="n">data</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-78"><a name="L-78"></a> <span class="n">StringBuffer</span> <span class="n">out</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuffer</span><span class="o">();</span> </span><span id="L-79"><a name="L-79"></a> </span><span id="L-80"><a name="L-80"></a> <span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> </span><span id="L-81"><a name="L-81"></a> <span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="na">length</span><span class="o">();</span> </span><span id="L-82"><a name="L-82"></a> <span class="k">while</span> <span class="o">(</span><span class="n">r</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-83"><a name="L-83"></a> <span class="kt">byte</span> <span class="n">d0</span><span class="o">,</span> <span class="n">d1</span><span class="o">,</span> <span class="n">d2</span><span class="o">;</span> </span><span id="L-84"><a name="L-84"></a> <span class="kt">byte</span> <span class="n">e0</span><span class="o">,</span> <span class="n">e1</span><span class="o">,</span> <span class="n">e2</span><span class="o">,</span> <span class="n">e3</span><span class="o">;</span> </span><span id="L-85"><a name="L-85"></a> </span><span id="L-86"><a name="L-86"></a> <span class="n">d0</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++);</span> <span class="o">--</span><span class="n">r</span><span class="o">;</span> </span><span id="L-87"><a name="L-87"></a> <span class="n">e0</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">(</span><span class="n">d0</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">2</span><span class="o">);</span> </span><span id="L-88"><a name="L-88"></a> <span class="n">e1</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">((</span><span class="n">d0</span> <span class="o">&amp;</span> <span class="mh">0x03</span><span class="o">)</span> <span class="o">&lt;&lt;</span> <span class="mi">4</span><span class="o">);</span> </span><span id="L-89"><a name="L-89"></a> </span><span id="L-90"><a name="L-90"></a> <span class="k">if</span> <span class="o">(</span><span class="n">r</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-91"><a name="L-91"></a> <span class="n">d1</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++);</span> <span class="o">--</span><span class="n">r</span><span class="o">;</span> </span><span id="L-92"><a name="L-92"></a> <span class="n">e1</span> <span class="o">+=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">(</span><span class="n">d1</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">4</span><span class="o">);</span> </span><span id="L-93"><a name="L-93"></a> <span class="n">e2</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">((</span><span class="n">d1</span> <span class="o">&amp;</span> <span class="mh">0x0f</span><span class="o">)</span> <span class="o">&lt;&lt;</span> <span class="mi">2</span><span class="o">);</span> </span><span id="L-94"><a name="L-94"></a> <span class="o">}</span> </span><span id="L-95"><a name="L-95"></a> <span class="k">else</span> <span class="o">{</span> </span><span id="L-96"><a name="L-96"></a> <span class="n">e2</span> <span class="o">=</span> <span class="mi">64</span><span class="o">;</span> </span><span id="L-97"><a name="L-97"></a> <span class="o">}</span> </span><span id="L-98"><a name="L-98"></a> </span><span id="L-99"><a name="L-99"></a> <span class="k">if</span> <span class="o">(</span><span class="n">r</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span> </span><span id="L-100"><a name="L-100"></a> <span class="n">d2</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="n">data</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">i</span><span class="o">++);</span> <span class="o">--</span><span class="n">r</span><span class="o">;</span> </span><span id="L-101"><a name="L-101"></a> <span class="n">e2</span> <span class="o">+=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">(</span><span class="n">d2</span> <span class="o">&gt;&gt;&gt;</span> <span class="mi">6</span><span class="o">);</span> </span><span id="L-102"><a name="L-102"></a> <span class="n">e3</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span> <span class="o">(</span><span class="n">d2</span> <span class="o">&amp;</span> <span class="mh">0x3f</span><span class="o">);</span> </span><span id="L-103"><a name="L-103"></a> <span class="o">}</span> </span><span id="L-104"><a name="L-104"></a> <span class="k">else</span> <span class="o">{</span> </span><span id="L-105"><a name="L-105"></a> <span class="n">e3</span> <span class="o">=</span> <span class="mi">64</span><span class="o">;</span> </span><span id="L-106"><a name="L-106"></a> <span class="o">}</span> </span><span id="L-107"><a name="L-107"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">etab</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">e0</span><span class="o">));</span> </span><span id="L-108"><a name="L-108"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">etab</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">e1</span><span class="o">));</span> </span><span id="L-109"><a name="L-109"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">etab</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">e2</span><span class="o">));</span> </span><span id="L-110"><a name="L-110"></a> <span class="n">out</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="n">etab</span><span class="o">.</span><span class="na">charAt</span><span class="o">(</span><span class="n">e3</span><span class="o">));</span> </span><span id="L-111"><a name="L-111"></a> <span class="o">}</span> </span><span id="L-112"><a name="L-112"></a> </span><span id="L-113"><a name="L-113"></a> <span class="k">return</span> <span class="n">out</span><span class="o">.</span><span class="na">toString</span><span class="o">();</span> </span><span id="L-114"><a name="L-114"></a> <span class="o">}</span> </span><span id="L-115"><a name="L-115"></a><span class="o">}</span> </span></pre></div> </td></tr></table>
ajax, base64, bsd-license, gwt, java