xml-xang-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdier...@locus.apache.org
Subject cvs commit: xml-xang/java/src/org/apache/xang/util/encoding BASE64Decoder.java BASE64Encoder.java CEFormatException.java CEStreamExhausted.java CharacterDecoder.java CharacterEncoder.java
Date Fri, 14 Apr 2000 21:38:37 GMT
mdierken    00/04/14 14:38:37

  Modified:    java/src/org/apache/xang/util/encoding BASE64Decoder.java
                        BASE64Encoder.java
  Added:       java/src/org/apache/xang/util/base64 Base64.java
  Removed:     java/src/org/apache/xang/util/encoding
                        CEFormatException.java CEStreamExhausted.java
                        CharacterDecoder.java CharacterEncoder.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  xml-xang/java/src/org/apache/xang/util/base64/Base64.java
  
  Index: Base64.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. 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.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xerces" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS 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.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.xang.util.base64;
  
  import java.lang.*;
  
  
  /**
   * This class provides encode/decode for RFC 2045 Base64 as
   * defined by RFC 2045, N. Freed and N. Borenstein. 
   * RFC 2045: Multipurpose Internet Mail Extensions (MIME)
   * Part One: Format of Internet Message Bodies. Reference
   * 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt 
   * This class is used by XML Schema binary format validation
   *
   * @author Jeffrey Rodriguez
   * @version
   */
  
  public final class  Base64 {
      static private final int  BASELENGTH         = 255;       
      static private final int  LOOKUPLENGTH       = 63;
      static private final int  TWENTYFOURBITGROUP = 24;
      static private final int  EIGHTBIT           = 8;
      static private final int  SIXTEENBIT         = 16;
      static private final int  SIXBIT             = 6;
      static private final int  FOURBYTE           = 4;
  
  
      static private final byte PAD               = ( byte ) '=';
      static private byte [] base64Alphabet       = new byte[BASELENGTH]; 
      static private byte [] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
  
      static {
  
          for (int i = 0; i<BASELENGTH; i++ ) {
              base64Alphabet[i] = -1; 
          }
          for ( int i = 'Z'; i >= 'A'; i-- ) {
              base64Alphabet[i] = (byte) (i-'A');
          }
          for ( int i = 'z'; i>= 'a'; i--) {
              base64Alphabet[i] = (byte) ( i-'a' + 26);
          }
  
          for ( int i = '9'; i >= '0'; i--) {
              base64Alphabet[i] = (byte) (i-'0' + 52);
          }
  
          base64Alphabet['+']  = 62; 
          base64Alphabet['/']  = 63;
  
         for (int i = 0; i<=25; i++ )
              lookUpBase64Alphabet[i] = (byte) ('A'+i );
  
          for (int i = 26,  j = 0; i<=51; i++, j++ )
              lookUpBase64Alphabet[i] = (byte) ('a'+ j );
  
          for (int i = 52,  j = 0; i<=61; i++, j++ )
              lookUpBase64Alphabet[i] = (byte) ('0' + j );
  
      }
  
  
      static boolean isBase64( byte octect ) {
          //shall we ignore white space? JEFF??
          return(octect == PAD || base64Alphabet[octect] != -1 ); 
      }
  
  
      static boolean isArrayByteBase64( byte[] arrayOctect ) {
          int length = arrayOctect.length;
          if ( length == 0 )
              return false;
          for ( int i=0; i < length; i++ ) {
              if ( Base64.isBase64( arrayOctect[i] ) == false)
                  return false;
          }
          return true;
      }
  
      /**
       * Encodes hex octects into Base64
       * 
       * @param binaryData Array containing binaryData
       * @return Encoded Base64 array
       */
      public byte[] encode( byte[] binaryData ) {
          int      lengthDataBits    = binaryData.length*EIGHTBIT;
          int      fewerThan24bits   = lengthDataBits%TWENTYFOURBITGROUP; 
          int      numberTriplets    = lengthDataBits/TWENTYFOURBITGROUP;
          byte     encodedData[]     = null;
  
  
          if ( fewerThan24bits != 0 ) //data not divisible by 24 bit
              encodedData = new byte[ (numberTriplets + 1 )*4  ];
          else // 16 or 8 bit
              encodedData = new byte[ numberTriplets*4 ];
  
          byte k=0, l=0, b1=0,b2=0,b3=0;
  
          int encodedIndex = 0;
          int dataIndex   = 0;
          int i           = 0;
          for ( i = 0; i<numberTriplets; i++ ) {
  
              dataIndex = i*3;
              b1 = binaryData[dataIndex]; 
              b2 = binaryData[dataIndex + 1];
              b3 = binaryData[dataIndex + 2];
  
              l  = (byte)(b2 & 0x0f);
              k  = (byte)(b1 & 0x03);
  
              encodedIndex = i*4;
              encodedData[encodedIndex]   = lookUpBase64Alphabet[ b1 >>2 ]; 
              encodedData[encodedIndex+1] = lookUpBase64Alphabet[(b2 >>4 ) | ( k<<4
)];
              encodedData[encodedIndex+2] = lookUpBase64Alphabet[ (l <<2 ) | ( b3>>6)];

              encodedData[encodedIndex+3] = lookUpBase64Alphabet[ b3 & 0x3f ];
          }
  
          // form integral number of 6-bit groups
          dataIndex    = i*3;
          encodedIndex = i*4;
          if (fewerThan24bits == EIGHTBIT ) {
              b1 = binaryData[dataIndex];
              k = (byte) ( b1 &0x03 );
              encodedData[encodedIndex]     = lookUpBase64Alphabet[ b1 >>2 ];
              encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ k<<4 ]; 
              encodedData[encodedIndex + 2] = PAD;
              encodedData[encodedIndex + 3] = PAD;
          } else if ( fewerThan24bits == SIXTEENBIT ) {
  
              b1 = binaryData[dataIndex];
              b2 = binaryData[dataIndex +1 ];
              l = ( byte ) ( b2 &0x0f );
              k = ( byte ) ( b1 &0x03 );
              encodedData[encodedIndex]     = lookUpBase64Alphabet[ b1 >>2 ];
              encodedData[encodedIndex + 1] = lookUpBase64Alphabet[ (b2 >>4 ) | ( k<<4
)];
              encodedData[encodedIndex + 2] = lookUpBase64Alphabet[ l<<2 ]; 
              encodedData[encodedIndex + 3] = PAD;
          }
          return encodedData;
      }
  
  
      /**
       * Decodes Base64 data into octects
       * 
       * @param binaryData Byte array containing Base64 data
       * @return Array containind decoded data.
       */
      public byte[] decode( byte[] base64Data ) {
          int      numberQuadruple    = base64Data.length/FOURBYTE;
          byte     decodedData[]      = null;
          byte     b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0;
  
          // Throw away anything not in base64Data
          // Adjust size
  
          int encodedIndex = 0;
          int dataIndex    = 0;
          decodedData      = new byte[ numberQuadruple*3 + 1 ];
  
          for (int i = 0; i<numberQuadruple; i++ ) {
              dataIndex = i*4;
              marker0   = base64Data[dataIndex +2]; 
              marker1   = base64Data[dataIndex +3]; 
  
              b1 = base64Alphabet[base64Data[dataIndex]]; 
              b2 = base64Alphabet[base64Data[dataIndex +1]];
  
              if ( marker0 != PAD && marker1 != PAD ) {     //No PAD e.g 3cQl
                  b3 = base64Alphabet[ marker0 ];
                  b4 = base64Alphabet[ marker1 ];
  
                  decodedData[encodedIndex]   = (byte)(  b1 <<2 | b2>>4 ) ;
                  decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2)
& 0xf) );
                  decodedData[encodedIndex+2] = (byte)( b3<<6 | b4 ); 
              } else if ( marker0 == PAD ) {               //Two PAD e.g. 3c[Pad][Pad]
                  decodedData[encodedIndex]   = (byte)(  b1 <<2 | b2>>4 ) ; 
                  decodedData[encodedIndex+1] = (byte)((b2 & 0xf)<<4 );  
                  decodedData[encodedIndex+2] = (byte) 0; 
              } else if ( marker1 == PAD ) {              //One PAD e.g. 3cQ[Pad]
                  b3 = base64Alphabet[ marker0 ];
  
                  decodedData[encodedIndex]   = (byte)(  b1 <<2 | b2>>4 );
                  decodedData[encodedIndex+1] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2)
& 0xf) );
                  decodedData[encodedIndex+2] = (byte)( b3<<6); 
              }
              encodedIndex += 3;
          }
          return decodedData;
  
      }
  }                                                        
  
  
  
  1.2       +85 -198   xml-xang/java/src/org/apache/xang/util/encoding/BASE64Decoder.java
  
  Index: BASE64Decoder.java
  ===================================================================
  RCS file: /home/cvs/xml-xang/java/src/org/apache/xang/util/encoding/BASE64Decoder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BASE64Decoder.java	2000/03/13 22:55:43	1.1
  +++ BASE64Decoder.java	2000/04/14 21:38:36	1.2
  @@ -1,223 +1,110 @@
   /*
  - * @(#)BASE64Decoder.java	1.9 95/10/08 Chuck McManis
  + * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights
  + * reserved.
    *
  - * Permission to use, copy, modify, and distribute this software
  - * and its documentation for NON-COMMERCIAL purposes and without
  - * fee is hereby granted provided that this copyright notice
  - * appears in all copies.
  - *
  - * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  - * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
  - * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  - * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS
  - * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
  - * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. 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.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Xerces" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS 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.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation and was
  + * originally based on software copyright (c) 1999, International
  + * Business Machines, Inc., http://www.apache.org.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
    */
  +
   package org.apache.xang.util.encoding;
  +import java.io.*;
  +import org.apache.xang.util.StreamUtil;
  +import org.apache.xang.util.base64.Base64;
   
  -import java.io.OutputStream;
  -import java.io.InputStream;
  -import java.io.PrintStream;
   
   /**
  - * This class implements a BASE64 Character decoder as specified in RFC1521.
  + * This class wraps the Base64 implementation borrowed from Xerces.
    *
  - * This RFC is part of the MIME specification which is published by the
  - * Internet Engineering Task Force (IETF). Unlike some other encoding
  - * schemes there is nothing in this encoding that tells the decoder
  - * where a buffer starts or stops, so to use it you will need to isolate
  - * your encoded data into a single chunk and then feed them this decoder.
  - * The simplest way to do that is to read all of the encoded data into a
  - * string and then use:
  - * <pre>
  - *	byte	mydata[];
  - *	BASE64Decoder base64 = new BASE64Decoder();
  - *
  - *	mydata = base64.decodeBuffer(bufferString);
  - * </pre>
  - * This will decode the String in <i>bufferString</i> and give you an array
  - * of bytes in the array <i>myData</i>.
  - *
  - * On errors, this class throws a CEFormatException with the following detail
  - * strings:
  - * <pre>
  - *    "BASE64Decoder: Bad Padding byte (2)."
  - *    "BASE64Decoder: Bad Padding byte (1)."
  - * </pre>
  - *
  - * @version	1.9, 08 Oct 1995
  - * @author	Chuck McManis
  - * @see		CharacterEncoder
  - * @see		BASE64Decoder
  + * @see		org.apache.xang.util.base64.Base64
    */
  -
  -public class BASE64Decoder extends CharacterDecoder {
  -
  -    /** This class has 3 bytes per atom */
  -    int bytesPerAtom() {
  -	return (3);
  -    }
   
  -    /** This class has 57 bytes per encoded line */
  -    int bytesPerLine() {
  -	return (4096);
  -    }
  +public class BASE64Decoder
  +{
  +    private static Base64 decoder = new Base64();
   
  -    /**
  -     * This character array provides the character to value map
  -     * based on RFC1521.
  -     */
  -    private final static char pem_array[] = {
  -	//   0   1   2   3   4   5   6   7
  -		'A','B','C','D','E','F','G','H', // 0
  -		'I','J','K','L','M','N','O','P', // 1
  -		'Q','R','S','T','U','V','W','X', // 2
  -		'Y','Z','a','b','c','d','e','f', // 3
  -		'g','h','i','j','k','l','m','n', // 4
  -		'o','p','q','r','s','t','u','v', // 5
  -		'w','x','y','z','0','1','2','3', // 6
  -		'4','5','6','7','8','9','+','/'  // 7
  -	};
  -
  -    byte decode_buffer[] = new byte[4];
  -
  -    byte getValue(byte t)
  +    public void process(InputStream input, OutputStream output) 
  +        throws IOException
       {
  -        byte r = -1;
  -        
  -        if (t >= 'A' && t <= 'Z')
  -        {
  -            r = (byte)(t - 'A');
  -        }
  -        else
  -        if (t >= 'a' && t <= 'z')
  -        {
  -            r = (byte)(26 + t - 'a');
  -        }
  -        else
  -        if (t >= '0' && t <= '9')
  -        {
  -            r = (byte)(52 + t - '0');
  -        }
  -        else
  -        if (t == '+')
  -        {
  -            r = (byte)(62);
  -        }
  -        else
  -        if (t == '-')
  -        {
  -            r = (byte)(63);
  -        }
  -        
  -        return r;
  +        output.write(process(input));
       }
       
       /**
  -     * Decode one BASE64 atom into 1, 2, or 3 bytes of data.
  +     * Decode base64 data
        */
  -    void decodeAtom(InputStream inStream, OutputStream outStream, int l)
  -	throws java.io.IOException
  +    public byte[] process(byte[] buffer)
  +        throws IOException 
       {
  -	int	i;
  -	byte	a = (byte)-1, b = (byte)-1, c = (byte)-1, d = (byte)-1;
  -
  -	decode_buffer[0] = (byte) inStream.read();
  -	if (decode_buffer[0] == -1) {
  -	    throw new CEStreamExhausted();
  -	}
  -
  -	// check for end of line. decodeLinePrefix() doesn't help in this case.
  -	if (decode_buffer[0] == '\r') {
  -		decode_buffer[0] = (byte) inStream.read();
  -		if (decode_buffer[0] == -1) {
  -			throw new CEStreamExhausted();
  -		}
  -	} 
  -
  -	// check to see if we caught the trailing end of a <CR><LF>
  -	if (decode_buffer[0] == '\n') {
  -	    i = readFully(inStream, decode_buffer, 0, 4);
  -	} else {
  -	    i = readFully(inStream, decode_buffer, 1, 3);
  -	}
  -	if (i == -1) {
  -	    throw new CEStreamExhausted();
  -	}
  -
  -
  -    a = getValue(decode_buffer[0]);
  -    b = getValue(decode_buffer[1]);
  -    c = getValue(decode_buffer[2]);
  -    d = getValue(decode_buffer[3]);
  -    
  -    /*
  -    for (i = 0; i < 64; i++) {
  -	    if (decode_buffer[0] == pem_array[i]) {
  -		a = (byte) i;
  -	    }
  -	    if (decode_buffer[1] == pem_array[i]) {
  -		b = (byte) i;
  -	    }
  -	    if (decode_buffer[2] == pem_array[i]) {
  -		c = (byte) i;
  -	    }
  -	    if (decode_buffer[3] == pem_array[i]) {
  -		d = (byte) i;
  -	    }
  -	}
  -    */
  -
  -	if ( decode_buffer[3] == '=') {	// correct length based on pad byte
  -	    l = (decode_buffer[2] == '=') ? 1 : 2;
  -	}
  -
  -	if ((l == 2) && (decode_buffer[3] != '=')) {
  -	    throw new CEFormatException("BASE64Decoder: Bad Padding byte (2).");
  -	}
  -
  -	if ((l == 1) &&
  -	    ((decode_buffer[2] != '=') || (decode_buffer[3] != '='))) {
  -	    throw new CEFormatException("BASE64Decoder: Bad Padding byte (1).");
  -	}
  -
  -	switch (l) {
  -	case 1:
  -	    outStream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3))
);
  -	    break;
  -	case 2:
  -	    outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3))
);
  -	    outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf))
);
  -	    break;
  -	case 3:
  -	    outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3))
);
  -	    outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf))
);
  -	    outStream.write( (byte) (((c << 6) & 0xc0) | (d  & 0x3f)) );
  -	    break;
  -	}
  -	return;
  +        return decoder.decode(buffer);
       }
   
  -    /**
  -     * decodeLineSuffix in this decoder simply finds the [newLine] and
  -     * positions us past it.
  -     */
  -    void decodeLineSuffix(InputStream inStream, OutputStream outStream)
  -	throws java.io.IOException
  +    public byte[] process(String text)
  +        throws IOException 
       {
  -	int c;
  +        return process(text.getBytes());
  +    }
   
  -	while (true) {
  -	    c = inStream.read();
  -	    if (c == -1) {
  -		throw new CEStreamExhausted();
  -	    }
  -	    if ((c == '\n') || (c == '\r')) {
  -		break;
  -	    }
  -	}
  +    public String processToText(String text)
  +        throws IOException 
  +    {
  +        byte[] buffer = process(text);
  +        return new String(buffer);
  +    }
  +        
  +    public byte[] process(InputStream input)
  +        throws IOException 
  +    {
  +        byte[] buffer;
  +        
  +        buffer = StreamUtil.readStream(input);
  +        return process(buffer);
       }
   }
  
  
  
  1.2       +98 -116   xml-xang/java/src/org/apache/xang/util/encoding/BASE64Encoder.java
  
  Index: BASE64Encoder.java
  ===================================================================
  RCS file: /home/cvs/xml-xang/java/src/org/apache/xang/util/encoding/BASE64Encoder.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BASE64Encoder.java	2000/03/13 22:55:43	1.1
  +++ BASE64Encoder.java	2000/04/14 21:38:36	1.2
  @@ -1,142 +1,124 @@
   /*
  - * @(#)BASE64Encoder.java	1.8 95/10/08 Chuck McManis
  + * The Apache Software License, Version 1.1
    *
    *
  - * Copyright (c) 1996 Chuck McManis, All Rights Reserved.
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights
  + * reserved.
    *
  - * Permission to use, copy, modify, and distribute this software
  - * and its documentation for NON-COMMERCIAL purposes and without
  - * fee is hereby granted provided that this copyright notice
  - * appears in all copies.
  - *
  - * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  - * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
  - * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  - * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS
  - * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT
  - * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  + * Redistribution and use in source and binary forms, with or without
  + * modification, are permitted provided that the following conditions
  + * are met:
  + *
  + * 1. Redistributions of source code must retain the above copyright
  + *    notice, this list of conditions and the following disclaimer.
  + *
  + * 2. 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.
  + *
  + * 3. The end-user documentation included with the redistribution,
  + *    if any, must include the following acknowledgment:
  + *       "This product includes software developed by the
  + *        Apache Software Foundation (http://www.apache.org/)."
  + *    Alternately, this acknowledgment may appear in the software itself,
  + *    if and wherever such third-party acknowledgments normally appear.
  + *
  + * 4. The names "Xerces" and "Apache Software Foundation" must
  + *    not be used to endorse or promote products derived from this
  + *    software without prior written permission. For written
  + *    permission, please contact apache@apache.org.
  + *
  + * 5. Products derived from this software may not be called "Apache",
  + *    nor may "Apache" appear in their name, without prior written
  + *    permission of the Apache Software Foundation.
  + *
  + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 THE APACHE SOFTWARE FOUNDATION OR
  + * ITS CONTRIBUTORS 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.
  + * ====================================================================
  + *
  + * This software consists of voluntary contributions made by many
  + * individuals on behalf of the Apache Software Foundation and was
  + * originally based on software copyright (c) 1999, International
  + * Business Machines, Inc., http://www.apache.org.  For more
  + * information on the Apache Software Foundation, please see
  + * <http://www.apache.org/>.
    */
   
   package org.apache.xang.util.encoding;
  +import java.io.*;
   
  -import java.io.OutputStream;
  -import java.io.InputStream;
  -import java.io.PrintStream;
  -import java.io.IOException;
  +import org.apache.xang.util.StreamUtil;
  +import org.apache.xang.util.base64.Base64;
   
  +
   /**
  - * This class implements a BASE64 Character encoder as specified in RFC1521.
  - * This RFC is part of the MIME specification as published by the Internet
  - * Engineering Task Force (IETF). Unlike some other encoding schemes there
  - * is nothing in this encoding that indicates
  - * where a buffer starts or ends.
  - *
  - * This means that the encoded text will simply start with the first line
  - * of encoded text and end with the last line of encoded text.
  - *
  - * @version	1.8, 08 Oct 1995
  - * @author	Chuck McManis
  - * @see		CharacterEncoder
  - * @see		BASE64Decoder
  + * This class wraps the Base64 implementation borrowed from Xerces.
  + *
  + * @see		org.apache.xang.util.base64.Base64
    */
   
  -public class BASE64Encoder extends CharacterEncoder {
  +public class BASE64Encoder
  +{
  +    private static Base64 encoder = new Base64();
   
  -    /** this class encodes three bytes per atom. */
  -    int bytesPerAtom() {
  -	return (3);
  +    /**
  +     * Encode from input stream to output stream
  +     */
  +    public void process(InputStream input, OutputStream output) 
  +        throws IOException
  +    {
  +        output.write(process(input));
       }
   
       /**
  -     * this class encodes 57 bytes per line. This results in a maximum
  -     * of 57/3 * 4 or 76 characters per output line. Not counting the
  -     * line termination.
  +     * Encode from text string to byte array
        */
  -    int bytesPerLine() {
  -	return (4096);
  +    public byte[] process(byte[] buffer)
  +        throws IOException 
  +    {
  +        return encoder.encode(buffer);
       }
   
  -    /** This array maps the characters to their 6 bit values */
  -    private final static char pem_array[] = {
  -	//       0   1   2   3   4   5   6   7
  -		'A','B','C','D','E','F','G','H', // 0
  -		'I','J','K','L','M','N','O','P', // 1
  -		'Q','R','S','T','U','V','W','X', // 2
  -		'Y','Z','a','b','c','d','e','f', // 3
  -		'g','h','i','j','k','l','m','n', // 4
  -		'o','p','q','r','s','t','u','v', // 5
  -		'w','x','y','z','0','1','2','3', // 6
  -		'4','5','6','7','8','9','+','/'  // 7
  -	};
  -
       /**
  -     * enocodeAtom - Take three bytes of input and encode it as 4
  -     * printable characters. Note that if the length in len is less
  -     * than three is encodes either one or two '=' signs to indicate
  -     * padding characters.
  +     * Encode from text string to byte array
        */
  -    void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
  -	throws IOException {
  -	byte a, b, c;
  -
  -	if (len == 1) {
  -	    a = data[offset];
  -	    b = 0;
  -	    c = 0;
  -	    outStream.write(pem_array[(a >>> 2) & 0x3F]);
  -	    outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) &
0xf)]);
  -	    outStream.write('=');
  -	    outStream.write('=');
  -	} else if (len == 2) {
  -	    a = data[offset];
  -	    b = data[offset+1];
  -	    c = 0;
  -	    outStream.write(pem_array[(a >>> 2) & 0x3F]);
  -	    outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) &
0xf)]);
  -	    outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) &
0x3)]);
  -	    outStream.write('=');
  -	} else {
  -	    a = data[offset];
  -	    b = data[offset+1];
  -	    c = data[offset+2];
  -	    outStream.write(pem_array[(a >>> 2) & 0x3F]);
  -	    outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) &
0xf)]);
  -	    outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) &
0x3)]);
  -	    outStream.write(pem_array[c & 0x3F]);
  -	}
  +    public byte[] process(String text)
  +        throws IOException 
  +    {
  +        return process(text.getBytes());
       }
   
  -	/* commented out, this breaks Fargo inproc mode.
  -    void encodeLineSuffix(OutputStream aStream) throws IOException {
  -	//pStream.println();
  +    /**
  +     * Encode from text string to a string object
  +     */
  +    public String processToText(String text)
  +        throws IOException 
  +    {
  +        byte[] buffer = process(text);
  +        return new String(buffer);
       }
  -	*/
  -
  -	public static void main(String args[])	{
  -		CharacterEncoder encoder = new BASE64Encoder();
  -		CharacterDecoder decoder = new BASE64Decoder();
  -
  -		try
  -		{
  -			String s;
  -			String sOut;
  -
  -			s = encoder.encodeBuffer("HelloWorld".getBytes());
  -			sOut = new String(decoder.decodeBuffer(s));
  -			System.out.println(s + "=[" + sOut + "]");
  -			
  -			s = encoder.encodeBuffer("HelloWorld1".getBytes());
  -			sOut = new String(decoder.decodeBuffer(s));
  -			System.out.println(s + "=[" + sOut + "]");
  -
  -			s = encoder.encodeBuffer("HelloWorld12".getBytes());
  -			sOut = new String(decoder.decodeBuffer(s));
  -			System.out.println(s + "=[" + sOut + "]");
  -		} 
  -		catch(IOException e)
  -		{
  -		}
  -		System.out.println("done");
  -	}
  +        
  +    /**
  +     * Encode from input stream to byte array
  +     */
  +    public byte[] process(InputStream input)
  +        throws IOException 
  +    {
  +        byte[] buffer;
  +        
  +        buffer = StreamUtil.readStream(input);
  +        return encoder.encode(buffer);
  +    }
   
   }
  
  
  

Mime
View raw message