Source for gnu.gcj.convert.Output_JavaSrc

   1: /* Copyright (C) 1999  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package gnu.gcj.convert; 
  10:  
  11: /**
  12:  * Convert Unicode to Ascii with \ u XXXX-escapes.
  13:  * @author Per Bothner <bothner@cygnus.com>
  14:  * @date April 1999.
  15:  */
  16: 
  17: public class Output_JavaSrc extends UnicodeToBytes
  18: {
  19:   public String getName() { return "JavaSrc"; }
  20: 
  21:   // Number of bytes remaining before pending_char has been written.
  22:   int todo;
  23:  int pending_char;
  24: 
  25:   public int write (char[] inbuffer, int inpos, int inlength)
  26:   {
  27:     int start_pos = inpos;
  28:     int avail = buf.length - count;
  29:     for (;;)
  30:       {
  31:     if (avail == 0)
  32:       break;
  33:     switch (todo)
  34:       {
  35:       case 1:
  36:         if (pending_char == '\\')
  37:           {
  38:         buf[count++] = (byte) '\\';
  39:         avail--;
  40:         todo = 0;
  41:         continue;
  42:           }
  43:         /* ... else fall through ... */
  44:       case 2:
  45:       case 3:
  46:       case 4:
  47:         todo--;
  48:         int digit = ((int) pending_char >> (todo * 4)) & 0xF;
  49:         buf[count++] = (byte) Character.forDigit(digit, 16);
  50:         avail--;
  51:         continue;
  52:       case 5:
  53:         buf[count++] = (byte) 'u';
  54:         avail--;
  55:         todo = 4;
  56:         continue;
  57:       default:
  58:         ;
  59:       }
  60:     if (inlength == 0)
  61:       break;
  62:     char ch = inbuffer[inpos++];
  63:     inlength--;
  64:     if (ch == '\\')
  65:       {
  66:         buf[count++] = (byte) '\\';
  67:         pending_char = ch;
  68:         todo = 1;
  69:         avail--;
  70:       }
  71:     else if (ch < 127)
  72:       {
  73:         buf[count++] = (byte) ch;
  74:         avail--;
  75:       }
  76:     else
  77:       {
  78:         buf[count++] = (byte) '\\';
  79:         pending_char = ch;
  80:         todo = 5;
  81:         avail--;
  82:       }
  83:       }
  84:     return inpos - start_pos;
  85:   }
  86: }