source: contrib/MailArchiver/sources/vendor/mime4j/custom/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java @ 6785

Revision 6785, 5.1 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
1/****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one   *
3 * or more contributor license agreements.  See the NOTICE file *
4 * distributed with this work for additional information        *
5 * regarding copyright ownership.  The ASF licenses this file   *
6 * to you under the Apache License, Version 2.0 (the            *
7 * "License"); you may not use this file except in compliance   *
8 * with the License.  You may obtain a copy of the License at   *
9 *                                                              *
10 *   http://www.apache.org/licenses/LICENSE-2.0                 *
11 *                                                              *
12 * Unless required by applicable law or agreed to in writing,   *
13 * software distributed under the License is distributed on an  *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15 * KIND, either express or implied.  See the License for the    *
16 * specific language governing permissions and limitations      *
17 * under the License.                                           *
18 ****************************************************************/
19
20package org.apache.james.mime4j.util;
21
22import java.nio.charset.Charset;
23import java.nio.charset.IllegalCharsetNameException;
24import java.nio.charset.UnsupportedCharsetException;
25
26/**
27 * Utility class for working with character sets.
28 */
29public class CharsetUtil {
30   
31    /** carriage return - line feed sequence */
32    public static final String CRLF = "\r\n";
33
34    /** US-ASCII CR, carriage return (13) */
35    public static final int CR = '\r';
36
37    /** US-ASCII LF, line feed (10) */
38    public static final int LF = '\n';
39
40    /** US-ASCII SP, space (32) */
41    public static final int SP = ' ';
42
43    /** US-ASCII HT, horizontal-tab (9) */
44    public static final int HT = '\t';
45
46    public static final Charset US_ASCII = Charset.forName("US-ASCII");
47
48    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
49
50    public static final Charset UTF_8 = Charset.forName("UTF-8");
51
52    public static final Charset DEFAULT_CHARSET = US_ASCII;
53
54    /**
55     * Returns <code>true</code> if the specified character falls into the US
56     * ASCII character set (Unicode range 0000 to 007f).
57     *
58     * @param ch
59     *            character to test.
60     * @return <code>true</code> if the specified character falls into the US
61     *         ASCII character set, <code>false</code> otherwise.
62     */
63    public static boolean isASCII(char ch) {
64        return (0xFF80 & ch) == 0;
65    }
66
67    /**
68     * Returns <code>true</code> if the specified string consists entirely of
69     * US ASCII characters.
70     *
71     * @param s
72     *            string to test.
73     * @return <code>true</code> if the specified string consists entirely of
74     *         US ASCII characters, <code>false</code> otherwise.
75     */
76    public static boolean isASCII(final String s) {
77        if (s == null) {
78            throw new IllegalArgumentException("String may not be null");
79        }
80        final int len = s.length();
81        for (int i = 0; i < len; i++) {
82            if (!isASCII(s.charAt(i))) {
83                return false;
84            }
85        }
86        return true;
87    }
88
89    /**
90     * Returns <code>true</code> if the specified character is a whitespace
91     * character (CR, LF, SP or HT).
92     *
93     * @param ch
94     *            character to test.
95     * @return <code>true</code> if the specified character is a whitespace
96     *         character, <code>false</code> otherwise.
97     */
98    public static boolean isWhitespace(char ch) {
99        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 }
Note: See TracBrowser for help on using the repository browser.