View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import static java.net.URLDecoder.decode;
19  import static java.net.URLEncoder.encode;
20  import static org.kuali.common.util.base.Exceptions.illegalState;
21  import static org.kuali.common.util.base.Precondition.checkNotNull;
22  
23  import java.io.UnsupportedEncodingException;
24  
25  public final class Encodings {
26  
27  	private Encodings() {
28  	}
29  
30  	public static final String UTF8 = "UTF-8";
31  	public static final String ASCII = "US-ASCII";
32  
33  	public static final String encodeUTF8(String text) {
34  		try {
35  			return encode(checkNotNull(text, "text"), UTF8);
36  		} catch (UnsupportedEncodingException e) {
37  			throw illegalState(e); // Shouldn't happen. Spec requires JVM's to support UTF-8
38  		}
39  	}
40  
41  	public static final String decodeUTF8(String text) {
42  		try {
43  			return decode(checkNotNull(text, "text"), UTF8);
44  		} catch (UnsupportedEncodingException e) {
45  			throw illegalState(e); // Shouldn't happen. Spec requires JVM's to support UTF-8
46  		}
47  	}
48  
49  }