001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.mime4j.util;
021
022 import java.nio.charset.Charset;
023 import java.nio.charset.IllegalCharsetNameException;
024 import java.nio.charset.UnsupportedCharsetException;
025
026 /**
027 * Utility class for working with character sets.
028 */
029 public class CharsetUtil {
030
031 /** carriage return - line feed sequence */
032 public static final String CRLF = "\r\n";
033
034 /** US-ASCII CR, carriage return (13) */
035 public static final int CR = '\r';
036
037 /** US-ASCII LF, line feed (10) */
038 public static final int LF = '\n';
039
040 /** US-ASCII SP, space (32) */
041 public static final int SP = ' ';
042
043 /** US-ASCII HT, horizontal-tab (9) */
044 public static final int HT = '\t';
045
046 public static final Charset US_ASCII = Charset.forName("US-ASCII");
047
048 public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
049
050 public static final Charset UTF_8 = Charset.forName("UTF-8");
051
052 public static final Charset DEFAULT_CHARSET = US_ASCII;
053
054 /**
055 * Returns <code>true</code> if the specified character falls into the US
056 * ASCII character set (Unicode range 0000 to 007f).
057 *
058 * @param ch
059 * character to test.
060 * @return <code>true</code> if the specified character falls into the US
061 * ASCII character set, <code>false</code> otherwise.
062 */
063 public static boolean isASCII(char ch) {
064 return (0xFF80 & ch) == 0;
065 }
066
067 /**
068 * Returns <code>true</code> if the specified string consists entirely of
069 * US ASCII characters.
070 *
071 * @param s
072 * string to test.
073 * @return <code>true</code> if the specified string consists entirely of
074 * US ASCII characters, <code>false</code> otherwise.
075 */
076 public static boolean isASCII(final String s) {
077 if (s == null) {
078 throw new IllegalArgumentException("String may not be null");
079 }
080 final int len = s.length();
081 for (int i = 0; i < len; i++) {
082 if (!isASCII(s.charAt(i))) {
083 return false;
084 }
085 }
086 return true;
087 }
088
089 /**
090 * Returns <code>true</code> if the specified character is a whitespace
091 * character (CR, LF, SP or HT).
092 *
093 * @param ch
094 * character to test.
095 * @return <code>true</code> if the specified character is a whitespace
096 * character, <code>false</code> otherwise.
097 */
098 public static boolean isWhitespace(char ch) {
099 return ch == SP || ch == HT || ch == CR || ch == LF;
100 }
101
102 /**
103 * Returns <code>true</code> if the specified string consists entirely of
104 * whitespace characters.
105 *
106 * @param s
107 * string to test.
108 * @return <code>true</code> if the specified string consists entirely of
109 * whitespace characters, <code>false</code> otherwise.
110 */
111 public static boolean isWhitespace(final String s) {
112 if (s == null) {
113 throw new IllegalArgumentException("String may not be null");
114 }
115 final int len = s.length();
116 for (int i = 0; i < len; i++) {
117 if (!isWhitespace(s.charAt(i))) {
118 return false;
119 }
120 }
121 return true;
122 }
123
124 /**
125 * Returns a {@link Charset} instance if character set with the given name
126 * is recognized and supported by Java runtime. Returns <code>null</code>
127 * otherwise.
128 * <p/>
129 * This method is a wrapper around {@link Charset#forName(String)} method
130 * that catches {@link IllegalCharsetNameException} and
131 * {@link UnsupportedCharsetException} and returns <code>null</code>.
132 */
133 public static Charset lookup(final String name) {
134 if (name == null) {
135 return null;
136 }
137 try {
138 return Charset.forName(name);
139 } catch (IllegalCharsetNameException ex) {
140 return null;
141 } catch (UnsupportedCharsetException ex) {
142 return null;
143 }
144 }
145
146 }