1: 
   8: 
   9: package ;
  10: 
  11: import ;
  12: 
  13: public abstract class BytesToUnicode extends IOConverter
  14: {
  15:   
  17:   public byte[] inbuffer;
  18:   
  19:   public int inpos;
  20:   
  21:   public int inlength;
  22: 
  23:   
  24:   static String defaultEncoding;
  25: 
  26:   
  33:   private static final int CACHE_SIZE = 4;  
  34:   private static BytesToUnicode[] decoderCache
  35:     = new BytesToUnicode[CACHE_SIZE];
  36:   private static int currCachePos = 0;
  37: 
  38:   public abstract String getName();
  39: 
  40:   public static BytesToUnicode getDefaultDecoder()
  41:   {
  42:     try
  43:       {
  44:     synchronized (BytesToUnicode.class)
  45:       {
  46:         if (defaultEncoding == null)
  47:           {
  48:         String encoding
  49:           = canonicalize (System.getProperty("file.encoding",
  50:                              "8859_1"));
  51:         String className = "gnu.gcj.convert.Input_" + encoding;
  52:         try
  53:           {
  54:             Class defaultDecodingClass = Class.forName(className);
  55:             defaultEncoding = encoding;
  56:           }
  57:         catch (ClassNotFoundException ex)
  58:           {
  59:             throw new NoClassDefFoundError("missing default encoding "
  60:                            + encoding + " (class "
  61:                            + className
  62:                            + " not found)");
  63:           }
  64:           }
  65:       }
  66:     return getDecoder (defaultEncoding);
  67:       }
  68:     catch (Throwable ex)
  69:       {
  70:     return new Input_8859_1();
  71:       }
  72:   }
  73: 
  74:   
  75:   public static BytesToUnicode getDecoder (String encoding)
  76:     throws java.io.UnsupportedEncodingException
  77:   {
  78:     
  80:     String canonicalEncoding = canonicalize(encoding);
  81:     synchronized (BytesToUnicode.class)
  82:       {
  83:     int i;
  84:     for (i = 0; i < decoderCache.length; ++i)
  85:       {
  86:         if (decoderCache[i] != null
  87:         && canonicalEncoding.equals(decoderCache[i].getName ()))
  88:           {
  89:         BytesToUnicode rv = decoderCache[i];
  90:         decoderCache[i] = null;
  91:         return rv;
  92:         }
  93:       }
  94:       }
  95: 
  96:     
  97:     String className = "gnu.gcj.convert.Input_" + canonicalEncoding;
  98:     Class decodingClass;
  99:     try 
 100:       { 
 101:     decodingClass = Class.forName(className); 
 102:     return (BytesToUnicode) decodingClass.newInstance();
 103:       } 
 104:     catch (Throwable ex) 
 105:       { 
 106:     try
 107:       {
 108:         
 109:         
 110:         
 111:         return new Input_iconv (encoding);
 112:       }
 113:     catch (Throwable _)
 114:       {
 115:         
 116:       }
 117:     try
 118:       {
 119:         return new BytesToCharsetAdaptor(Charset.forName(encoding));
 120:       }
 121:     catch (Throwable _)
 122:       {
 123:         throw new java.io.UnsupportedEncodingException(encoding
 124:                                + " (" + ex + ')');
 125:       }
 126:       }
 127:   }
 128: 
 129:   
 134:   public final void setInput(byte[] buffer, int pos, int length)
 135:   {
 136:     inbuffer = buffer;
 137:     inpos = pos;
 138:     inlength = length;
 139:   }
 140: 
 141:   
 158:   public abstract int read (char[] outbuffer, int outpos, int count);
 159: 
 160:   
 165:   public void done ()
 166:   {
 167:     synchronized (BytesToUnicode.class)
 168:       {
 169:     this.inbuffer = null;
 170:     this.inpos = 0;
 171:     this.inlength = 0;
 172: 
 173:     decoderCache[currCachePos] = this;
 174:     currCachePos = (currCachePos + 1) % CACHE_SIZE;
 175:       }
 176:   }
 177: }