KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > util > EncoderCache


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/util/EncoderCache.java,v 1.4 2004/02/12 00:29:50 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.util;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22
23 import org.apache.jorphan.util.JOrphanUtils;
24 import org.apache.oro.util.Cache;
25 import org.apache.oro.util.CacheLRU;
26
27 /**
28  * @author Administrator
29  *
30  * @version $Revision: 1.4 $ last updated $Date: 2004/02/12 00:29:50 $
31  */

32 public class EncoderCache
33 {
34     Cache cache;
35
36     public EncoderCache(int cacheSize)
37     {
38        cache = new CacheLRU(cacheSize);
39     }
40     
41     public String JavaDoc getEncoded(String JavaDoc k)
42     {
43         Object JavaDoc encodedValue = cache.getElement(k);
44         if(encodedValue != null)
45         {
46             return (String JavaDoc)encodedValue;
47         }
48         try
49         {
50             encodedValue = JOrphanUtils.encode(k, "utf8");
51         }
52         catch (UnsupportedEncodingException JavaDoc e)
53         {
54             // This can't happen (how should utf8 not be supported!?!),
55
// so just throw an Error:
56
throw new Error JavaDoc("Should not happen: "+e.toString());
57         }
58         cache.addElement(k,encodedValue);
59         return (String JavaDoc)encodedValue;
60     }
61 }
62
Popular Tags