Coverage Report - org.jaudiotagger.tag.vorbiscomment.util.Base64Coder
 
Classes in this File Line Coverage Branch Coverage Complexity
Base64Coder
92%
67/73
67%
28/42
3.75
 
 1  
 package org.jaudiotagger.tag.vorbiscomment.util;
 2  
 
 3  
 import org.jaudiotagger.audio.generic.Utils;
 4  
 
 5  
 /**
 6  
  * Base64Coder
 7  
  */
 8  0
 public class Base64Coder
 9  
 {
 10  
     // Mapping table from 6-bit nibbles to Base64 characters.
 11  3
     private static final char[] map1 = new char[64];
 12  
 
 13  
     static
 14  
     {
 15  3
         int i = 0;
 16  81
         for (char c = 'A'; c <= 'Z'; c++)
 17  
         {
 18  78
             map1[i++] = c;
 19  
         }
 20  81
         for (char c = 'a'; c <= 'z'; c++)
 21  
         {
 22  78
             map1[i++] = c;
 23  
         }
 24  33
         for (char c = '0'; c <= '9'; c++)
 25  
         {
 26  30
             map1[i++] = c;
 27  
         }
 28  3
         map1[i++] = '+';
 29  3
         map1[i++] = '/';
 30  
     }
 31  
 
 32  
     // Mapping table from Base64 characters to 6-bit nibbles.
 33  3
     private static final byte[] map2 = new byte[128];
 34  
 
 35  
     static
 36  
     {
 37  387
         for (int i = 0; i < map2.length; i++)
 38  
         {
 39  384
             map2[i] = -1;
 40  
         }
 41  195
         for (int i = 0; i < 64; i++)
 42  
         {
 43  192
             map2[map1[i]] = (byte) i;
 44  
         }
 45  3
     }
 46  
 
 47  
     /**
 48  
      * Encodes a string into Base64 format.
 49  
      * No blanks or line breaks are inserted.
 50  
      *
 51  
      * @param s a String to be encoded.
 52  
      * @return A String with the Base64 encoded data.
 53  
      */
 54  
     public static String encode(final String s)
 55  
     {
 56  0
         return new String(encode(Utils.getDefaultBytes(s, "ISO-8859-1")));
 57  
     }
 58  
 
 59  
     /**
 60  
      * Encodes a byte array into Base64 format.
 61  
      * No blanks or line breaks are inserted.
 62  
      *
 63  
      * @param in an array containing the data bytes to be encoded.
 64  
      * @return A character array with the Base64 encoded data.
 65  
      */
 66  
     public static char[] encode(final byte[] in)
 67  
     {
 68  7
         final int iLen = in.length;
 69  7
         final int oDataLen = (iLen * 4 + 2) / 3;       // output length without padding
 70  7
         final int oLen = ((iLen + 2) / 3) * 4;         // output length including padding
 71  7
         final char[] out = new char[oLen];
 72  7
         int ip = 0;
 73  7
         int op = 0;
 74  77118
         while (ip < iLen)
 75  
         {
 76  77111
             final int i0 = in[ip++] & 0xff;
 77  77111
             final int i1 = ip < iLen ? in[ip++] & 0xff : 0;
 78  77111
             final int i2 = ip < iLen ? in[ip++] & 0xff : 0;
 79  77111
             final int o0 = i0 >>> 2;
 80  77111
             final int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
 81  77111
             final int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
 82  77111
             final int o3 = i2 & 0x3F;
 83  77111
             out[op++] = map1[o0];
 84  77111
             out[op++] = map1[o1];
 85  77111
             out[op] = op < oDataLen ? map1[o2] : '=';
 86  77111
             op++;
 87  77111
             out[op] = op < oDataLen ? map1[o3] : '=';
 88  77111
             op++;
 89  77111
         }
 90  7
         return out;
 91  
     }
 92  
 
 93  
     /**
 94  
      * Decodes a Base64 string.
 95  
      *
 96  
      * @param s a Base64 String to be decoded.
 97  
      * @return A String containing the decoded data.
 98  
      * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
 99  
      */
 100  
     public static String decode(final String s)
 101  
     {
 102  0
         return new String(decode(s.toCharArray()));
 103  
     }
 104  
 
 105  
     /**
 106  
      * Decodes Base64 data.
 107  
      * No blanks or line breaks are allowed within the Base64 encoded data.
 108  
      *
 109  
      * @param in a character array containing the Base64 encoded data.
 110  
      * @return An array containing the decoded data bytes.
 111  
      * @throws IllegalArgumentException if the input is not valid Base64 encoded data.
 112  
      */
 113  
     public static byte[] decode(final char[] in)
 114  
     {
 115  22
         int iLen = in.length;
 116  22
         if (iLen % 4 != 0)
 117  
         {
 118  0
             throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4.");
 119  
         }
 120  44
         while (iLen > 0 && in[iLen - 1] == '=')
 121  
         {
 122  22
             iLen--;
 123  
         }
 124  22
         final int oLen = (iLen * 3) / 4;
 125  22
         final byte[] out = new byte[oLen];
 126  22
         int ip = 0;
 127  22
         int op = 0;
 128  440559
         while (ip < iLen)
 129  
         {
 130  440537
             final int i0 = in[ip++];
 131  440537
             final int i1 = in[ip++];
 132  440537
             final int i2 = ip < iLen ? in[ip++] : 'A';
 133  440537
             final int i3 = ip < iLen ? in[ip++] : 'A';
 134  440537
             if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
 135  
             {
 136  0
                 throw new IllegalArgumentException("Illegal character in Base64 encoded data.");
 137  
             }
 138  440537
             final int b0 = map2[i0];
 139  440537
             final int b1 = map2[i1];
 140  440537
             final int b2 = map2[i2];
 141  440537
             final int b3 = map2[i3];
 142  440537
             if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
 143  
             {
 144  0
                 throw new IllegalArgumentException("Illegal character in Base64 encoded data.");
 145  
             }
 146  440537
             final int o0 = (b0 << 2) | (b1 >>> 4);
 147  440537
             final int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);
 148  440537
             final int o2 = ((b2 & 3) << 6) | b3;
 149  440537
             out[op++] = (byte) o0;
 150  440537
             if (op < oLen)
 151  
             {
 152  440537
                 out[op++] = (byte) o1;
 153  
             }
 154  440537
             if (op < oLen)
 155  
             {
 156  440515
                 out[op++] = (byte) o2;
 157  
             }
 158  440537
         }
 159  22
         return out;
 160  
     }
 161  
 }