KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > HTUU


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util;
25
26 /*
27 ** HTUU.CLASS**** ACKNOWLEDGEMENT:
28 ** Main code is taken from the HTUU.C distribution, and was originally
29 ** written by Mark Riordan (riordanmr@clvax1.cl.msu.edu)
30 ** and Ari Luotonen (luotonen@dxcern.cern.ch).**** AUTHORS:
31 ** IG Ian Goh ian.goh@jhu.edu**** HISTORY:
32 ** Converted HTUU.C "HTUU_encode" function into Java (1.0.2): IG 13 July 1996
33 ** -------------------------------------------------------------
34 ** File contains a routine to convert a buffer
35 ** of bytes to RFC 1113 printable encoding format.**
36 ** This technique is similar to the familiar Unix uuencode
37 ** format in that it maps 6 binary bits to one ASCII
38 ** character (or more aptly, 3 binary bytes to 4 ASCII
39 ** characters). However, RFC 1113 does not use the same
40 ** mapping to printable characters as uuencode.**
41 ** Mark Riordan 12 August 1990 and 17 Feb 1991.
42 ** This code is hereby placed in the public domain.
43 ** -------------------------------------------------------------*****/

44
45 public class HTUU
46 {
47     static String JavaDoc version = "HTUU Class v1.0 7/13/96";
48     
49     // the Base64 printable encoding characters
50
static char[] ENC = {
51         'A','B','C','D','E','F','G','H','I','J','K','L','M',
52         'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
53         'a','b','c','d','e','f','g','h','i','j','k','l','m',
54         'n','o','p','q','r','s','t','u','v','w','x','y','z',
55         '0','1','2','3','4','5','6','7','8','9','+','/' };
56     
57     // function encode takes the "username:password" string and
58
// converts it into the printable encoding format
59

60     public static String JavaDoc encode(String JavaDoc string)
61     {
62         int i, j;
63         byte[] byte_array = new byte[3];
64         StringBuffer JavaDoc buf_coded = new StringBuffer JavaDoc();
65         
66         // get length of input string
67
int nbytes = string.length();
68         for (i = 0; i < nbytes; i+= 3)
69         {
70             // check to make sure we don't run off the end of input string
71
if (i + 3 < nbytes)
72                 j = i + 3;
73             else
74                 j = nbytes;
75             
76             string.getBytes(i, j, byte_array, 0); // get bytes i..j
77

78             if (j - i == 1)
79             { // missing last two bytes
80
byte_array[1] = 0;
81                 byte_array[2] = 0;
82             }
83             if (j - i == 2)
84             { // missing last byte
85
byte_array[2] = 0;
86             }
87             // convert the three bytes into four Base64 characters
88
// and append to the buf_coded string buffer
89
buf_coded.append(ENC[byte_array[0] >> 2]);
90             buf_coded.append(ENC[((byte_array[0] << 4) & 060) | ((byte_array[1] >> 4) & 017)]);
91             buf_coded.append(ENC[((byte_array[1] << 2) & 074) | ((byte_array[2] >> 6) & 03)]);
92             buf_coded.append(ENC[byte_array[2] & 077]); } // end for loop
93
// If nbytes was not a multiple of 3, then we have encoded too
94
// many characters. Adjust appropriately.
95
int buf_length = buf_coded.length();
96         if (i == nbytes+1)
97         {
98             /* There were only 2 bytes in that last group */
99             buf_coded.setCharAt(buf_length - 1, '=');
100         }
101         else if (i == nbytes+2)
102         {
103             /* There was only 1 byte in that last group */
104             buf_coded.setCharAt(buf_length - 1, '=');
105             buf_coded.setCharAt(buf_length - 2, '=');
106         }
107         // return the Base64 encoded string
108
return buf_coded.toString();
109     }
110 }
Popular Tags