]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/UrlEncoded.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / util / UrlEncoded.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.util;
20
21 import static org.eclipse.jetty.util.TypeUtil.convertHexDigit;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.io.StringWriter;
27 import java.nio.charset.Charset;
28 import java.nio.charset.StandardCharsets;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.eclipse.jetty.util.Utf8Appendable.NotUtf8Exception;
33 import org.eclipse.jetty.util.log.Log;
34 import org.eclipse.jetty.util.log.Logger;
35
36 /* ------------------------------------------------------------ */
37 /** Handles coding of MIME  "x-www-form-urlencoded".
38  * <p>
39  * This class handles the encoding and decoding for either
40  * the query string of a URL or the _content of a POST HTTP request.
41  *
42  * <h4>Notes</h4>
43  * The UTF-8 charset is assumed, unless otherwise defined by either
44  * passing a parameter or setting the "org.eclipse.jetty.util.UrlEncoding.charset"
45  * System property.
46  * <p>
47  * The hashtable either contains String single values, vectors
48  * of String or arrays of Strings.
49  * <p>
50  * This class is only partially synchronised.  In particular, simple
51  * get operations are not protected from concurrent updates.
52  *
53  * @see java.net.URLEncoder
54  */
55 @SuppressWarnings("serial")
56 public class UrlEncoded extends MultiMap<String> implements Cloneable
57 {
58     static final Logger LOG = Log.getLogger(UrlEncoded.class);
59
60     public static final Charset ENCODING;
61     static
62     {
63         Charset encoding;
64         try
65         {
66             String charset = System.getProperty("org.eclipse.jetty.util.UrlEncoding.charset");
67             encoding = charset == null ? StandardCharsets.UTF_8 : Charset.forName(charset);
68         }
69         catch(Exception e)
70         {
71             LOG.warn(e);
72             encoding=StandardCharsets.UTF_8;
73         }
74         ENCODING=encoding;
75     }
76     
77     /* ----------------------------------------------------------------- */
78     public UrlEncoded(UrlEncoded url)
79     {
80         super(url);
81     }
82     
83     /* ----------------------------------------------------------------- */
84     public UrlEncoded()
85     {
86     }
87     
88     public UrlEncoded(String query)
89     {
90         decodeTo(query,this,ENCODING,-1);
91     }
92
93     /* ----------------------------------------------------------------- */
94     public void decode(String query)
95     {
96         decodeTo(query,this,ENCODING,-1);
97     }
98     
99     /* ----------------------------------------------------------------- */
100     public void decode(String query,Charset charset)
101     {
102         decodeTo(query,this,charset,-1);
103     }
104     
105     /* -------------------------------------------------------------- */
106     /** Encode Hashtable with % encoding.
107      */
108     public String encode()
109     {
110         return encode(ENCODING,false);
111     }
112     
113     /* -------------------------------------------------------------- */
114     /** Encode Hashtable with % encoding.
115      */
116     public String encode(Charset charset)
117     {
118         return encode(charset,false);
119     }
120     
121     /* -------------------------------------------------------------- */
122     /** Encode Hashtable with % encoding.
123      * @param equalsForNullValue if True, then an '=' is always used, even
124      * for parameters without a value. e.g. "blah?a=&b=&c=".
125      */
126     public synchronized String encode(Charset charset, boolean equalsForNullValue)
127     {
128         return encode(this,charset,equalsForNullValue);
129     }
130     
131     /* -------------------------------------------------------------- */
132     /** Encode Hashtable with % encoding.
133      * @param equalsForNullValue if True, then an '=' is always used, even
134      * for parameters without a value. e.g. "blah?a=&b=&c=".
135      */
136     public static String encode(MultiMap<String> map, Charset charset, boolean equalsForNullValue)
137     {
138         if (charset==null)
139             charset=ENCODING;
140
141         StringBuilder result = new StringBuilder(128);
142
143         boolean delim = false;
144         for(Map.Entry<String, List<String>> entry: map.entrySet())
145         {
146             String key = entry.getKey().toString();
147             List<String> list = entry.getValue();
148             int s=list.size();
149             
150             if (delim)
151             {
152                 result.append('&');
153             }
154
155             if (s==0)
156             {
157                 result.append(encodeString(key,charset));
158                 if(equalsForNullValue)
159                     result.append('=');
160             }
161             else
162             {
163                 for (int i=0;i<s;i++)
164                 {
165                     if (i>0)
166                         result.append('&');
167                     String val=list.get(i);
168                     result.append(encodeString(key,charset));
169
170                     if (val!=null)
171                     {
172                         String str=val.toString();
173                         if (str.length()>0)
174                         {
175                             result.append('=');
176                             result.append(encodeString(str,charset));
177                         }
178                         else if (equalsForNullValue)
179                             result.append('=');
180                     }
181                     else if (equalsForNullValue)
182                         result.append('=');
183                 }
184             }
185             delim = true;
186         }
187         return result.toString();
188     }
189
190     /* -------------------------------------------------------------- */
191     /** Decoded parameters to Map.
192      * @param content the string containing the encoded parameters
193      */
194     public static void decodeTo(String content, MultiMap<String> map, String charset, int maxKeys)
195     {
196         decodeTo(content,map,charset==null?null:Charset.forName(charset),maxKeys);
197     }
198     
199     /* -------------------------------------------------------------- */
200     /** Decoded parameters to Map.
201      * @param content the string containing the encoded parameters
202      */
203     public static void decodeTo(String content, MultiMap<String> map, Charset charset, int maxKeys)
204     {
205         if (charset==null)
206             charset=ENCODING;
207
208         synchronized(map)
209         {
210             String key = null;
211             String value = null;
212             int mark=-1;
213             boolean encoded=false;
214             for (int i=0;i<content.length();i++)
215             {
216                 char c = content.charAt(i);
217                 switch (c)
218                 {
219                   case '&':
220                       int l=i-mark-1;
221                       value = l==0?"":
222                           (encoded?decodeString(content,mark+1,l,charset):content.substring(mark+1,i));
223                       mark=i;
224                       encoded=false;
225                       if (key != null)
226                       {
227                           map.add(key,value);
228                       }
229                       else if (value!=null&&value.length()>0)
230                       {
231                           map.add(value,"");
232                       }
233                       key = null;
234                       value=null;
235                       if (maxKeys>0 && map.size()>maxKeys)
236                           throw new IllegalStateException("Form too many keys");
237                       break;
238                   case '=':
239                       if (key!=null)
240                           break;
241                       key = encoded?decodeString(content,mark+1,i-mark-1,charset):content.substring(mark+1,i);
242                       mark=i;
243                       encoded=false;
244                       break;
245                   case '+':
246                       encoded=true;
247                       break;
248                   case '%':
249                       encoded=true;
250                       break;
251                 }                
252             }
253             
254             if (key != null)
255             {
256                 int l=content.length()-mark-1;
257                 value = l==0?"":(encoded?decodeString(content,mark+1,l,charset):content.substring(mark+1));
258                 map.add(key,value);
259             }
260             else if (mark<content.length())
261             {
262                 key = encoded
263                     ?decodeString(content,mark+1,content.length()-mark-1,charset)
264                     :content.substring(mark+1);
265                 if (key != null && key.length() > 0)
266                 {
267                     map.add(key,"");
268                 }
269             }
270         }
271     }
272
273     /* -------------------------------------------------------------- */
274     /** Decoded parameters to Map.
275      * @param raw the byte[] containing the encoded parameters
276      * @param offset the offset within raw to decode from
277      * @param length the length of the section to decode
278      * @param map the {@link MultiMap} to populate
279      */
280     public static void decodeUtf8To(byte[] raw,int offset, int length, MultiMap<String> map)
281     {
282         Utf8StringBuilder buffer = new Utf8StringBuilder();
283         synchronized(map)
284         {
285             String key = null;
286             String value = null;
287
288             int end=offset+length;
289             for (int i=offset;i<end;i++)
290             {
291                 byte b=raw[i];
292                 try
293                 {
294                     switch ((char)(0xff&b))
295                     {
296                         case '&':
297                             value = buffer.toReplacedString();
298                             buffer.reset();
299                             if (key != null)
300                             {
301                                 map.add(key,value);
302                             }
303                             else if (value!=null&&value.length()>0)
304                             {
305                                 map.add(value,"");
306                             }
307                             key = null;
308                             value=null;
309                             break;
310
311                         case '=':
312                             if (key!=null)
313                             {
314                                 buffer.append(b);
315                                 break;
316                             }
317                             key = buffer.toReplacedString();
318                             buffer.reset();
319                             break;
320
321                         case '+':
322                             buffer.append((byte)' ');
323                             break;
324
325                         case '%':
326                             if (i+2<end)
327                             {
328                                 if ('u'==raw[i+1])
329                                 {
330                                     i++;
331                                     if (i+4<end)
332                                     {
333                                         byte top=raw[++i];
334                                         byte hi=raw[++i];
335                                         byte lo=raw[++i];
336                                         byte bot=raw[++i];
337                                         buffer.getStringBuilder().append(Character.toChars((convertHexDigit(top)<<12) +(convertHexDigit(hi)<<8) + (convertHexDigit(lo)<<4) +convertHexDigit(bot)));
338                                     }
339                                     else
340                                     {
341                                         buffer.getStringBuilder().append(Utf8Appendable.REPLACEMENT);
342                                         i=end;
343                                     }
344                                 }
345                                 else
346                                 {
347                                     byte hi=raw[++i];
348                                     byte lo=raw[++i];
349                                     buffer.append((byte)((convertHexDigit(hi)<<4) + convertHexDigit(lo)));
350                                 }
351                             }
352                             else
353                             {
354                                 buffer.getStringBuilder().append(Utf8Appendable.REPLACEMENT);
355                                 i=end;
356                             }
357                             break;
358                             
359                         default:
360                             buffer.append(b);
361                             break;
362                     }
363                 }
364                 catch(NotUtf8Exception e)
365                 {
366                     LOG.warn(e.toString());
367                     LOG.debug(e);
368                 }
369                 catch(NumberFormatException e)
370                 {
371                     buffer.append(Utf8Appendable.REPLACEMENT_UTF8,0,3);
372                     LOG.warn(e.toString());
373                     LOG.debug(e);
374                 }
375             }
376             
377             if (key != null)
378             {
379                 value = buffer.toReplacedString();
380                 buffer.reset();
381                 map.add(key,value);
382             }
383             else if (buffer.length()>0)
384             {
385                 map.add(buffer.toReplacedString(),"");
386             }
387         }
388     }
389
390     /* -------------------------------------------------------------- */
391     /** Decoded parameters to Map.
392      * @param in InputSteam to read
393      * @param map MultiMap to add parameters to
394      * @param maxLength maximum number of keys to read or -1 for no limit
395      */
396     public static void decode88591To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys)
397     throws IOException
398     {
399         synchronized(map)
400         {
401             StringBuffer buffer = new StringBuffer();
402             String key = null;
403             String value = null;
404             
405             int b;
406
407             int totalLength=0;
408             while ((b=in.read())>=0)
409             {
410                 switch ((char) b)
411                 {
412                     case '&':
413                         value = buffer.length()==0?"":buffer.toString();
414                         buffer.setLength(0);
415                         if (key != null)
416                         {
417                             map.add(key,value);
418                         }
419                         else if (value!=null&&value.length()>0)
420                         {
421                             map.add(value,"");
422                         }
423                         key = null;
424                         value=null;
425                         if (maxKeys>0 && map.size()>maxKeys)
426                             throw new IllegalStateException("Form too many keys");
427                         break;
428                         
429                     case '=':
430                         if (key!=null)
431                         {
432                             buffer.append((char)b);
433                             break;
434                         }
435                         key = buffer.toString();
436                         buffer.setLength(0);
437                         break;
438                         
439                     case '+':
440                         buffer.append(' ');
441                         break;
442                         
443                     case '%':
444                         int code0=in.read();
445                         if ('u'==code0)
446                         {
447                             int code1=in.read();
448                             if (code1>=0)
449                             {
450                                 int code2=in.read();
451                                 if (code2>=0)
452                                 {
453                                     int code3=in.read();
454                                     if (code3>=0)
455                                         buffer.append(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3)));
456                                 }
457                             }
458                         }
459                         else if (code0>=0)
460                         {
461                             int code1=in.read();
462                             if (code1>=0)
463                                 buffer.append((char)((convertHexDigit(code0)<<4)+convertHexDigit(code1)));
464                         }
465                         break;
466                      
467                     default:
468                         buffer.append((char)b);
469                     break;
470                 }
471                 if (maxLength>=0 && (++totalLength > maxLength))
472                     throw new IllegalStateException("Form too large");
473             }
474             
475             if (key != null)
476             {
477                 value = buffer.length()==0?"":buffer.toString();
478                 buffer.setLength(0);
479                 map.add(key,value);
480             }
481             else if (buffer.length()>0)
482             {
483                 map.add(buffer.toString(), "");
484             }
485         }
486     }
487     
488     /* -------------------------------------------------------------- */
489     /** Decoded parameters to Map.
490      * @param in InputSteam to read
491      * @param map MultiMap to add parameters to
492      * @param maxLength maximum number of keys to read or -1 for no limit
493      */
494     public static void decodeUtf8To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys)
495     throws IOException
496     {
497         synchronized(map)
498         {
499             Utf8StringBuilder buffer = new Utf8StringBuilder();
500             String key = null;
501             String value = null;
502             
503             int b;
504             
505             int totalLength=0;
506             while ((b=in.read())>=0)
507             {
508                 try
509                 {
510                     switch ((char) b)
511                     {
512                         case '&':
513                             value = buffer.toReplacedString();
514                             buffer.reset();
515                             if (key != null)
516                             {
517                                 map.add(key,value);
518                             }
519                             else if (value!=null&&value.length()>0)
520                             {
521                                 map.add(value,"");
522                             }
523                             key = null;
524                             value=null;
525                             if (maxKeys>0 && map.size()>maxKeys)
526                                 throw new IllegalStateException("Form too many keys");
527                             break;
528
529                         case '=':
530                             if (key!=null)
531                             {
532                                 buffer.append((byte)b);
533                                 break;
534                             }
535                             key = buffer.toReplacedString(); 
536                             buffer.reset();
537                             break;
538
539                         case '+':
540                             buffer.append((byte)' ');
541                             break;
542
543                         case '%':
544                             int code0=in.read();
545                             boolean decoded=false;
546                             if ('u'==code0)
547                             {
548                                 code0=in.read(); // XXX: we have to read the next byte, otherwise code0 is always 'u'
549                                 if (code0>=0)
550                                 {
551                                     int code1=in.read();
552                                     if (code1>=0)
553                                     {
554                                         int code2=in.read();
555                                         if (code2>=0)
556                                         {
557                                             int code3=in.read();
558                                             if (code3>=0)
559                                             {
560                                                 buffer.getStringBuilder().append(Character.toChars
561                                                     ((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3)));
562                                                 decoded=true;
563                                             }
564                                         }
565                                     }
566                                 }
567                             }
568                             else if (code0>=0)
569                             {
570                                 int code1=in.read();
571                                 if (code1>=0)
572                                 {
573                                     buffer.append((byte)((convertHexDigit(code0)<<4)+convertHexDigit(code1)));
574                                     decoded=true;
575                                 }
576                             }
577                             
578                             if (!decoded)
579                                 buffer.getStringBuilder().append(Utf8Appendable.REPLACEMENT);
580
581                             break;
582                           
583                         default:
584                             buffer.append((byte)b);
585                             break;
586                     }
587                 }
588                 catch(NotUtf8Exception e)
589                 {
590                     LOG.warn(e.toString());
591                     LOG.debug(e);
592                 }
593                 catch(NumberFormatException e)
594                 {
595                     buffer.append(Utf8Appendable.REPLACEMENT_UTF8,0,3);
596                     LOG.warn(e.toString());
597                     LOG.debug(e);
598                 }
599                 if (maxLength>=0 && (++totalLength > maxLength))
600                     throw new IllegalStateException("Form too large");
601             }
602             
603             if (key != null)
604             {
605                 value = buffer.toReplacedString();
606                 buffer.reset();
607                 map.add(key,value);
608             }
609             else if (buffer.length()>0)
610             {
611                 map.add(buffer.toReplacedString(), "");
612             }
613         }
614     }
615     
616     /* -------------------------------------------------------------- */
617     public static void decodeUtf16To(InputStream in, MultiMap<String> map, int maxLength, int maxKeys) throws IOException
618     {
619         InputStreamReader input = new InputStreamReader(in,StandardCharsets.UTF_16);
620         StringWriter buf = new StringWriter(8192);
621         IO.copy(input,buf,maxLength);
622         
623         decodeTo(buf.getBuffer().toString(),map,StandardCharsets.UTF_16,maxKeys);
624     }
625
626     /* -------------------------------------------------------------- */
627     /** Decoded parameters to Map.
628      * @param in the stream containing the encoded parameters
629      */
630     public static void decodeTo(InputStream in, MultiMap<String> map, String charset, int maxLength, int maxKeys)
631     throws IOException
632     {
633         if (charset==null)
634         {
635             if (ENCODING.equals(StandardCharsets.UTF_8))
636                 decodeUtf8To(in,map,maxLength,maxKeys);
637             else
638                 decodeTo(in,map,ENCODING,maxLength,maxKeys);
639         }
640         else if (StringUtil.__UTF8.equalsIgnoreCase(charset))
641             decodeUtf8To(in,map,maxLength,maxKeys);
642         else if (StringUtil.__ISO_8859_1.equalsIgnoreCase(charset))
643             decode88591To(in,map,maxLength,maxKeys);
644         else if (StringUtil.__UTF16.equalsIgnoreCase(charset))
645             decodeUtf16To(in,map,maxLength,maxKeys);
646         else
647             decodeTo(in,map,Charset.forName(charset),maxLength,maxKeys);
648     }
649     
650     /* -------------------------------------------------------------- */
651     /** Decoded parameters to Map.
652      * @param in the stream containing the encoded parameters
653      */
654     public static void decodeTo(InputStream in, MultiMap<String> map, Charset charset, int maxLength, int maxKeys)
655     throws IOException
656     {
657         //no charset present, use the configured default
658         if (charset==null) 
659            charset=ENCODING;
660             
661         if (StandardCharsets.UTF_8.equals(charset))
662         {
663             decodeUtf8To(in,map,maxLength,maxKeys);
664             return;
665         }
666         
667         if (StandardCharsets.ISO_8859_1.equals(charset))
668         {
669             decode88591To(in,map,maxLength,maxKeys);
670             return;
671         }
672
673         if (StandardCharsets.UTF_16.equals(charset)) // Should be all 2 byte encodings
674         {
675             decodeUtf16To(in,map,maxLength,maxKeys);
676             return;
677         }
678
679         synchronized(map)
680         {
681             String key = null;
682             String value = null;
683             
684             int c;
685             
686             int totalLength = 0;
687             ByteArrayOutputStream2 output = new ByteArrayOutputStream2();
688             
689             int size=0;
690             
691             while ((c=in.read())>0)
692             {
693                 switch ((char) c)
694                 {
695                     case '&':
696                         size=output.size();
697                         value = size==0?"":output.toString(charset);
698                         output.setCount(0);
699                         if (key != null)
700                         {
701                             map.add(key,value);
702                         }
703                         else if (value!=null&&value.length()>0)
704                         {
705                             map.add(value,"");
706                         }
707                         key = null;
708                         value=null;
709                         if (maxKeys>0 && map.size()>maxKeys)
710                             throw new IllegalStateException("Form too many keys");
711                         break;
712                     case '=':
713                         if (key!=null)
714                         {
715                             output.write(c);
716                             break;
717                         }
718                         size=output.size();
719                         key = size==0?"":output.toString(charset);
720                         output.setCount(0);
721                         break;
722                     case '+':
723                         output.write(' ');
724                         break;
725                     case '%':
726                         int code0=in.read();
727                         if ('u'==code0)
728                         {
729                             int code1=in.read();
730                             if (code1>=0)
731                             {
732                                 int code2=in.read();
733                                 if (code2>=0)
734                                 {
735                                     int code3=in.read();
736                                     if (code3>=0)
737                                         output.write(new String(Character.toChars((convertHexDigit(code0)<<12)+(convertHexDigit(code1)<<8)+(convertHexDigit(code2)<<4)+convertHexDigit(code3))).getBytes(charset));
738                                 }
739                             }
740                             
741                         }
742                         else if (code0>=0)
743                         {
744                             int code1=in.read();
745                             if (code1>=0)
746                                 output.write((convertHexDigit(code0)<<4)+convertHexDigit(code1));
747                         }
748                         break;
749                     default:
750                         output.write(c);
751                     break;
752                 }
753                 
754                 totalLength++;
755                 if (maxLength>=0 && totalLength > maxLength)
756                     throw new IllegalStateException("Form too large");
757             }
758
759             size=output.size();
760             if (key != null)
761             {
762                 value = size==0?"":output.toString(charset);
763                 output.setCount(0);
764                 map.add(key,value);
765             }
766             else if (size>0)
767                 map.add(output.toString(charset),"");
768         }
769     }
770     
771     /* -------------------------------------------------------------- */
772     /** Decode String with % encoding.
773      * This method makes the assumption that the majority of calls
774      * will need no decoding.
775      */
776     public static String decodeString(String encoded,int offset,int length,Charset charset)
777     {
778         if (charset==null || StandardCharsets.UTF_8.equals(charset))
779         {
780             Utf8StringBuffer buffer=null;
781
782             for (int i=0;i<length;i++)
783             {
784                 char c = encoded.charAt(offset+i);
785                 if (c<0||c>0xff)
786                 {
787                     if (buffer==null)
788                     {
789                         buffer=new Utf8StringBuffer(length);
790                         buffer.getStringBuffer().append(encoded,offset,offset+i+1);
791                     }
792                     else
793                         buffer.getStringBuffer().append(c);
794                 }
795                 else if (c=='+')
796                 {
797                     if (buffer==null)
798                     {
799                         buffer=new Utf8StringBuffer(length);
800                         buffer.getStringBuffer().append(encoded,offset,offset+i);
801                     }
802                     
803                     buffer.getStringBuffer().append(' ');
804                 }
805                 else if (c=='%')
806                 {
807                     if (buffer==null)
808                     {
809                         buffer=new Utf8StringBuffer(length);
810                         buffer.getStringBuffer().append(encoded,offset,offset+i);
811                     }
812                     
813                     if ((i+2)<length)
814                     {
815                         try
816                         {
817                             if ('u'==encoded.charAt(offset+i+1))
818                             {
819                                 if((i+5)<length)
820                                 {
821                                     int o=offset+i+2;
822                                     i+=5;
823                                     String unicode = new String(Character.toChars(TypeUtil.parseInt(encoded,o,4,16)));
824                                     buffer.getStringBuffer().append(unicode); 
825                                 }
826                                 else
827                                 {
828                                     i=length;
829                                     buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT); 
830                                 }
831                             }
832                             else
833                             {
834                                 int o=offset+i+1;
835                                 i+=2;
836                                 byte b=(byte)TypeUtil.parseInt(encoded,o,2,16);
837                                 buffer.append(b);
838                             }
839                         }
840                         catch(NotUtf8Exception e)
841                         {
842                             LOG.warn(e.toString());
843                             LOG.debug(e);
844                         }
845                         catch(NumberFormatException e)
846                         {
847                             LOG.warn(e.toString());
848                             LOG.debug(e);
849                             buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT);  
850                         }
851                     }
852                     else
853                     {
854                         buffer.getStringBuffer().append(Utf8Appendable.REPLACEMENT); 
855                         i=length;
856                     }
857                 }
858                 else if (buffer!=null)
859                     buffer.getStringBuffer().append(c);
860             }
861
862             if (buffer==null)
863             {
864                 if (offset==0 && encoded.length()==length)
865                     return encoded;
866                 return encoded.substring(offset,offset+length);
867             }
868
869             return buffer.toReplacedString();
870         }
871         else
872         {
873             StringBuffer buffer=null;
874
875             for (int i=0;i<length;i++)
876             {
877                 char c = encoded.charAt(offset+i);
878                 if (c<0||c>0xff)
879                 {
880                     if (buffer==null)
881                     {
882                         buffer=new StringBuffer(length);
883                         buffer.append(encoded,offset,offset+i+1);
884                     }
885                     else
886                         buffer.append(c);
887                 }
888                 else if (c=='+')
889                 {
890                     if (buffer==null)
891                     {
892                         buffer=new StringBuffer(length);
893                         buffer.append(encoded,offset,offset+i);
894                     }
895
896                     buffer.append(' ');
897                 }
898                 else if (c=='%')
899                 {
900                     if (buffer==null)
901                     {
902                         buffer=new StringBuffer(length);
903                         buffer.append(encoded,offset,offset+i);
904                     }
905
906                     byte[] ba=new byte[length];
907                     int n=0;
908                     while(c>=0 && c<=0xff)
909                     {
910                         if (c=='%')
911                         {   
912                             if(i+2<length)
913                             {
914                                 try
915                                 {
916                                     if ('u'==encoded.charAt(offset+i+1))
917                                     {
918                                         if (i+6<length)
919                                         {
920                                             int o=offset+i+2;
921                                             i+=6;
922                                             String unicode = new String(Character.toChars(TypeUtil.parseInt(encoded,o,4,16)));
923                                             byte[] reencoded = unicode.getBytes(charset);
924                                             System.arraycopy(reencoded,0,ba,n,reencoded.length);
925                                             n+=reencoded.length;
926                                         }
927                                         else
928                                         {
929                                             ba[n++] = (byte)'?';
930                                             i=length;
931                                         }
932                                     }
933                                     else
934                                     {
935                                         int o=offset+i+1;
936                                         i+=3;
937                                         ba[n]=(byte)TypeUtil.parseInt(encoded,o,2,16);
938                                         n++;
939                                     }
940                                 }
941                                 catch(Exception e)
942                                 {   
943                                     LOG.warn(e.toString());
944                                     LOG.debug(e);
945                                     ba[n++] = (byte)'?';
946                                 }
947                             }
948                             else
949                             {
950                                     ba[n++] = (byte)'?';
951                                     i=length;
952                             }
953                         }
954                         else if (c=='+')
955                         {
956                             ba[n++]=(byte)' ';
957                             i++;
958                         }
959                         else
960                         {
961                             ba[n++]=(byte)c;
962                             i++;
963                         }
964
965                         if (i>=length)
966                             break;
967                         c = encoded.charAt(offset+i);
968                     }
969
970                     i--;
971                     buffer.append(new String(ba,0,n,charset));
972
973                 }
974                 else if (buffer!=null)
975                     buffer.append(c);
976             }
977
978             if (buffer==null)
979             {
980                 if (offset==0 && encoded.length()==length)
981                     return encoded;
982                 return encoded.substring(offset,offset+length);
983             }
984
985             return buffer.toString();
986         }
987
988     }
989     
990     /* ------------------------------------------------------------ */
991     /** Perform URL encoding.
992      * @param string 
993      * @return encoded string.
994      */
995     public static String encodeString(String string)
996     {
997         return encodeString(string,ENCODING);
998     }
999     
1000     /* ------------------------------------------------------------ */
1001     /** Perform URL encoding.
1002      * @param string 
1003      * @return encoded string.
1004      */
1005     public static String encodeString(String string,Charset charset)
1006     {
1007         if (charset==null)
1008             charset=ENCODING;
1009         byte[] bytes=null;
1010         bytes=string.getBytes(charset);
1011         
1012         int len=bytes.length;
1013         byte[] encoded= new byte[bytes.length*3];
1014         int n=0;
1015         boolean noEncode=true;
1016         
1017         for (int i=0;i<len;i++)
1018         {
1019             byte b = bytes[i];
1020             
1021             if (b==' ')
1022             {
1023                 noEncode=false;
1024                 encoded[n++]=(byte)'+';
1025             }
1026             else if (b>='a' && b<='z' ||
1027                      b>='A' && b<='Z' ||
1028                      b>='0' && b<='9')
1029             {
1030                 encoded[n++]=b;
1031             }
1032             else
1033             {
1034                 noEncode=false;
1035                 encoded[n++]=(byte)'%';
1036                 byte nibble= (byte) ((b&0xf0)>>4);
1037                 if (nibble>=10)
1038                     encoded[n++]=(byte)('A'+nibble-10);
1039                 else
1040                     encoded[n++]=(byte)('0'+nibble);
1041                 nibble= (byte) (b&0xf);
1042                 if (nibble>=10)
1043                     encoded[n++]=(byte)('A'+nibble-10);
1044                 else
1045                     encoded[n++]=(byte)('0'+nibble);
1046             }
1047         }
1048
1049         if (noEncode)
1050             return string;
1051         
1052         return new String(encoded,0,n,charset);
1053     }
1054
1055
1056     /* ------------------------------------------------------------ */
1057     /** 
1058      */
1059     @Override
1060     public Object clone()
1061     {
1062         return new UrlEncoded(this);
1063     }
1064 }