Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 73   Methods: 3
NCLOC: 55   Classes: 1
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
Base64Binary.java 72,7% 79,4% 100% 78%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.instance.data;
 11   
 
 12   
 import org.jbind.util.other.StrBuffer;
 13   
 import org.jbind.xml.core.data.IBase64Binary;
 14   
 
 15   
 public class Base64Binary extends Binary implements IBase64Binary {
 16   
 
 17  1
   public Base64Binary(byte[] aBytes) {
 18  1
     super(aBytes);
 19   
   }
 20   
 
 21  2
   public String toString() {
 22  2
     StrBuffer res = new StrBuffer((myValue.length * 4) / 3 + 1);
 23  2
     for (int i = 0; i < myValue.length; i += 3) {
 24   
       // Base64 encoded strings must have lines with at most 76 characters.
 25   
       // Therefore after every 18th byte (i.e. 72 characters) a new line is inserted.
 26  4
       if ((i != 0) && (i % 18 == 0)) {
 27  0
         res.append('\n');
 28   
       }
 29  4
       int value;
 30  4
       int chars;
 31  4
       if (i < myValue.length - 2) {
 32  2
         value = (0x00FF0000 & (myValue[i] << 16)) | (0x0000FF00 & (myValue[i + 1] << 8)) | (0x000000FF & myValue[i + 2]);
 33  2
         chars = 4;
 34  2
       } else if (i < myValue.length - 1) {
 35  0
         value = (0x00FF0000 & (myValue[i] << 16)) | (0x0000FF00 & (myValue[i + 1] << 8));
 36  0
         chars = 3;
 37   
       } else {
 38  2
         value = (0x00FF0000 & (myValue[i] << 16));
 39  2
         chars = 2;
 40   
       }
 41  4
       while (chars-- > 0) {
 42  12
         int x = (0x00FC0000 & value) >> 18;
 43  12
         char c = getChar(x);
 44  12
         res.append(c);
 45  12
         value = value << 6;
 46   
       }
 47  4
       if (i == myValue.length - 1) {
 48  2
         res.append("==");
 49  2
       } else if (i == myValue.length - 2) {
 50  0
         res.append('=');
 51   
       }
 52   
     }
 53  2
     return res.toString();
 54   
   }
 55   
 
 56  12
   private static char getChar(int anInt) {
 57  12
     if (anInt < 26) {
 58  6
       return (char)('A' + anInt);
 59   
     }
 60  6
     if (anInt < 52) {
 61  4
       return (char)('a' + anInt - 26);
 62   
     }
 63  2
     if (anInt < 62) {
 64  2
       return (char)('0' + anInt - 52);
 65   
     }
 66  0
     if (anInt == 62) {
 67  0
       return '+';
 68   
     }
 69  0
     return '/';
 70   
   }
 71   
 
 72   
 }
 73