/src/java/org/infoglue/deliver/util/HTUU.java

http://github.com/bogeblad/infoglue · Java · 110 lines · 49 code · 9 blank · 52 comment · 11 complexity · 94da63792d509935f37f76c6d992df53 MD5 · raw file

  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. package org.infoglue.deliver.util;
  24. /*
  25. ** HTUU.CLASS**** ACKNOWLEDGEMENT:
  26. ** Main code is taken from the HTUU.C distribution, and was originally
  27. ** written by Mark Riordan (riordanmr@clvax1.cl.msu.edu)
  28. ** and Ari Luotonen (luotonen@dxcern.cern.ch).**** AUTHORS:
  29. ** IG Ian Goh ian.goh@jhu.edu**** HISTORY:
  30. ** Converted HTUU.C "HTUU_encode" function into Java (1.0.2): IG 13 July 1996
  31. ** -------------------------------------------------------------
  32. ** File contains a routine to convert a buffer
  33. ** of bytes to RFC 1113 printable encoding format.**
  34. ** This technique is similar to the familiar Unix uuencode
  35. ** format in that it maps 6 binary bits to one ASCII
  36. ** character (or more aptly, 3 binary bytes to 4 ASCII
  37. ** characters). However, RFC 1113 does not use the same
  38. ** mapping to printable characters as uuencode.**
  39. ** Mark Riordan 12 August 1990 and 17 Feb 1991.
  40. ** This code is hereby placed in the public domain.
  41. ** -------------------------------------------------------------*****/
  42. public class HTUU
  43. {
  44. static String version = "HTUU Class v1.0 7/13/96";
  45. // the Base64 printable encoding characters
  46. static char[] ENC = {
  47. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  48. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  49. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  50. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  51. '0','1','2','3','4','5','6','7','8','9','+','/' };
  52. // function encode takes the "username:password" string and
  53. // converts it into the printable encoding format
  54. public static String encode(String string)
  55. {
  56. int i, j;
  57. byte[] byte_array = new byte[3];
  58. StringBuffer buf_coded = new StringBuffer();
  59. // get length of input string
  60. int nbytes = string.length();
  61. for (i = 0; i < nbytes; i+= 3)
  62. {
  63. // check to make sure we don't run off the end of input string
  64. if (i + 3 < nbytes)
  65. j = i + 3;
  66. else
  67. j = nbytes;
  68. string.getBytes(i, j, byte_array, 0); // get bytes i..j
  69. if (j - i == 1)
  70. { // missing last two bytes
  71. byte_array[1] = 0;
  72. byte_array[2] = 0;
  73. }
  74. if (j - i == 2)
  75. { // missing last byte
  76. byte_array[2] = 0;
  77. }
  78. // convert the three bytes into four Base64 characters
  79. // and append to the buf_coded string buffer
  80. buf_coded.append(ENC[byte_array[0] >> 2]);
  81. buf_coded.append(ENC[((byte_array[0] << 4) & 060) | ((byte_array[1] >> 4) & 017)]);
  82. buf_coded.append(ENC[((byte_array[1] << 2) & 074) | ((byte_array[2] >> 6) & 03)]);
  83. buf_coded.append(ENC[byte_array[2] & 077]); } // end for loop
  84. // If nbytes was not a multiple of 3, then we have encoded too
  85. // many characters. Adjust appropriately.
  86. int buf_length = buf_coded.length();
  87. if (i == nbytes+1)
  88. {
  89. /* There were only 2 bytes in that last group */
  90. buf_coded.setCharAt(buf_length - 1, '=');
  91. }
  92. else if (i == nbytes+2)
  93. {
  94. /* There was only 1 byte in that last group */
  95. buf_coded.setCharAt(buf_length - 1, '=');
  96. buf_coded.setCharAt(buf_length - 2, '=');
  97. }
  98. // return the Base64 encoded string
  99. return buf_coded.toString();
  100. }
  101. }