< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/util/Base64.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  30 
  31 /**
  32  * This class provides encode/decode for RFC 2045 Base64 as
  33  * defined by RFC 2045, N. Freed and N. Borenstein.
  34  * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
  35  * Part One: Format of Internet Message Bodies. Reference
  36  * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
  37  * This class is used by XML Schema binary format validation
  38  *
  39  * @author Jeffrey Rodriguez
  40  * @version
  41  */
  42 public final class Base64 {
  43 
  44 
  45     static private final int  BASELENGTH         = 255;
  46     static private final int  LOOKUPLENGTH       = 63;
  47     static private final int  TWENTYFOURBITGROUP = 24;
  48     static private final int  EIGHTBIT           = 8;
  49     static private final int  SIXTEENBIT         = 16;
  50     static private final int  SIXBIT             = 6;
  51     static private final int  FOURBYTE           = 4;
  52 
  53 
  54     static private final byte PAD               = ( byte ) '=';
  55     static private byte [] base64Alphabet       = new byte[BASELENGTH];
  56     static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
  57 
  58     static {
  59 
  60         for (int i = 0; i<BASELENGTH; i++ ) {
  61             base64Alphabet[i] = -1;
  62         }
  63         for ( int i = 'Z'; i >= 'A'; i-- ) {
  64             base64Alphabet[i] = (byte) (i-'A');
  65         }
  66         for ( int i = 'z'; i>= 'a'; i--) {
  67             base64Alphabet[i] = (byte) ( i-'a' + 26);
  68         }
  69 
  70         for ( int i = '9'; i >= '0'; i--) {


 227         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 228             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 229             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
 230             52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
 231             64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 232             15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
 233             64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 234             41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
 235             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 236             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 237             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 238             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 239             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 240             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 241             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 242             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
 243     };
 244 
 245     public static String base64Decode( String orig ) {
 246         char chars[]=orig.toCharArray();
 247         StringBuffer sb=new StringBuffer();
 248         int i=0;
 249 
 250         int shift = 0;   // # of excess bits stored in accum
 251         int acc = 0;
 252 
 253         for (i=0; i<chars.length; i++) {
 254             int v = base64[ chars[i] & 0xFF ];
 255 
 256             if ( v >= 64 ) {
 257                 if( chars[i] != '=' )
 258                     System.out.println("Wrong char in base64: " + chars[i]);
 259             } else {
 260                 acc= ( acc << 6 ) | v;
 261                 shift += 6;
 262                 if ( shift >= 8 ) {
 263                     shift -= 8;
 264                     sb.append( (char) ((acc >> shift) & 0xff));
 265                 }
 266             }
 267         }
   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  30 
  31 /**
  32  * This class provides encode/decode for RFC 2045 Base64 as
  33  * defined by RFC 2045, N. Freed and N. Borenstein.
  34  * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
  35  * Part One: Format of Internet Message Bodies. Reference
  36  * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
  37  * This class is used by XML Schema binary format validation
  38  *
  39  * @author Jeffrey Rodriguez
  40  * @version
  41  */
  42 public final class Base64 {
  43 
  44 
  45     static private final int  BASELENGTH         = 255;
  46     static private final int  LOOKUPLENGTH       = 63;
  47     static private final int  TWENTYFOURBITGROUP = 24;
  48     static private final int  EIGHTBIT           = 8;
  49     static private final int  SIXTEENBIT         = 16;

  50     static private final int  FOURBYTE           = 4;
  51 
  52 
  53     static private final byte PAD               = ( byte ) '=';
  54     static private byte [] base64Alphabet       = new byte[BASELENGTH];
  55     static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
  56 
  57     static {
  58 
  59         for (int i = 0; i<BASELENGTH; i++ ) {
  60             base64Alphabet[i] = -1;
  61         }
  62         for ( int i = 'Z'; i >= 'A'; i-- ) {
  63             base64Alphabet[i] = (byte) (i-'A');
  64         }
  65         for ( int i = 'z'; i>= 'a'; i--) {
  66             base64Alphabet[i] = (byte) ( i-'a' + 26);
  67         }
  68 
  69         for ( int i = '9'; i >= '0'; i--) {


 226         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 227             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 228             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
 229             52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
 230             64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
 231             15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
 232             64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 233             41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
 234             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 235             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 236             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 237             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 238             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 239             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 240             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
 241             64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
 242     };
 243 
 244     public static String base64Decode( String orig ) {
 245         char chars[]=orig.toCharArray();
 246         StringBuilder sb=new StringBuilder();
 247         int i=0;
 248 
 249         int shift = 0;   // # of excess bits stored in accum
 250         int acc = 0;
 251 
 252         for (i=0; i<chars.length; i++) {
 253             int v = base64[ chars[i] & 0xFF ];
 254 
 255             if ( v >= 64 ) {
 256                 if( chars[i] != '=' )
 257                     System.out.println("Wrong char in base64: " + chars[i]);
 258             } else {
 259                 acc= ( acc << 6 ) | v;
 260                 shift += 6;
 261                 if ( shift >= 8 ) {
 262                     shift -= 8;
 263                     sb.append( (char) ((acc >> shift) & 0xff));
 264                 }
 265             }
 266         }
< prev index next >