Source Code Cross Referenced for EncodingUtils.java in  » Net » httpcomponents-core-4.0-beta1 » org » apache » http » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » httpcomponents core 4.0 beta1 » org.apache.http.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/EncodingUtils.java $
003:         * $Revision: 503413 $
004:         * $Date: 2007-02-04 15:22:14 +0100 (Sun, 04 Feb 2007) $
005:         *
006:         * ====================================================================
007:         * Licensed to the Apache Software Foundation (ASF) under one
008:         * or more contributor license agreements.  See the NOTICE file
009:         * distributed with this work for additional information
010:         * regarding copyright ownership.  The ASF licenses this file
011:         * to you under the Apache License, Version 2.0 (the
012:         * "License"); you may not use this file except in compliance
013:         * with the License.  You may obtain a copy of the License at
014:         *
015:         *   http://www.apache.org/licenses/LICENSE-2.0
016:         *
017:         * Unless required by applicable law or agreed to in writing,
018:         * software distributed under the License is distributed on an
019:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020:         * KIND, either express or implied.  See the License for the
021:         * specific language governing permissions and limitations
022:         * under the License.
023:         * ====================================================================
024:         *
025:         * This software consists of voluntary contributions made by many
026:         * individuals on behalf of the Apache Software Foundation.  For more
027:         * information on the Apache Software Foundation, please see
028:         * <http://www.apache.org/>.
029:         *
030:         */
031:        package org.apache.http.util;
032:
033:        import java.io.UnsupportedEncodingException;
034:
035:        import org.apache.http.protocol.HTTP;
036:
037:        /**
038:         * The home for utility methods that handle various encoding tasks.
039:         * 
040:         * @author Michael Becke
041:         * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
042:         * 
043:         * @since 4.0
044:         */
045:        public final class EncodingUtils {
046:
047:            /**
048:             * Converts the byte array of HTTP content characters to a string. If
049:             * the specified charset is not supported, default system encoding
050:             * is used.
051:             *
052:             * @param data the byte array to be encoded
053:             * @param offset the index of the first byte to encode
054:             * @param length the number of bytes to encode 
055:             * @param charset the desired character encoding
056:             * @return The result of the conversion.
057:             */
058:            public static String getString(final byte[] data, int offset,
059:                    int length, String charset) {
060:
061:                if (data == null) {
062:                    throw new IllegalArgumentException(
063:                            "Parameter may not be null");
064:                }
065:
066:                if (charset == null || charset.length() == 0) {
067:                    throw new IllegalArgumentException(
068:                            "charset may not be null or empty");
069:                }
070:
071:                try {
072:                    return new String(data, offset, length, charset);
073:                } catch (UnsupportedEncodingException e) {
074:                    return new String(data, offset, length);
075:                }
076:            }
077:
078:            /**
079:             * Converts the byte array of HTTP content characters to a string. If
080:             * the specified charset is not supported, default system encoding
081:             * is used.
082:             *
083:             * @param data the byte array to be encoded
084:             * @param charset the desired character encoding
085:             * @return The result of the conversion.
086:             */
087:            public static String getString(final byte[] data,
088:                    final String charset) {
089:                if (data == null) {
090:                    throw new IllegalArgumentException(
091:                            "Parameter may not be null");
092:                }
093:                return getString(data, 0, data.length, charset);
094:            }
095:
096:            /**
097:             * Converts the specified string to a byte array.  If the charset is not supported the
098:             * default system charset is used.
099:             *
100:             * @param data the string to be encoded
101:             * @param charset the desired character encoding
102:             * @return The resulting byte array.
103:             */
104:            public static byte[] getBytes(final String data,
105:                    final String charset) {
106:
107:                if (data == null) {
108:                    throw new IllegalArgumentException("data may not be null");
109:                }
110:
111:                if (charset == null || charset.length() == 0) {
112:                    throw new IllegalArgumentException(
113:                            "charset may not be null or empty");
114:                }
115:
116:                try {
117:                    return data.getBytes(charset);
118:                } catch (UnsupportedEncodingException e) {
119:                    return data.getBytes();
120:                }
121:            }
122:
123:            /**
124:             * Converts the specified string to byte array of ASCII characters.
125:             *
126:             * @param data the string to be encoded
127:             * @return The string as a byte array.
128:             */
129:            public static byte[] getAsciiBytes(final String data) {
130:
131:                if (data == null) {
132:                    throw new IllegalArgumentException(
133:                            "Parameter may not be null");
134:                }
135:
136:                try {
137:                    return data.getBytes(HTTP.US_ASCII);
138:                } catch (UnsupportedEncodingException e) {
139:                    throw new Error("HttpClient requires ASCII support");
140:                }
141:            }
142:
143:            /**
144:             * Converts the byte array of ASCII characters to a string. This method is
145:             * to be used when decoding content of HTTP elements (such as response
146:             * headers)
147:             *
148:             * @param data the byte array to be encoded
149:             * @param offset the index of the first byte to encode
150:             * @param length the number of bytes to encode 
151:             * @return The string representation of the byte array
152:             */
153:            public static String getAsciiString(final byte[] data, int offset,
154:                    int length) {
155:
156:                if (data == null) {
157:                    throw new IllegalArgumentException(
158:                            "Parameter may not be null");
159:                }
160:
161:                try {
162:                    return new String(data, offset, length, HTTP.US_ASCII);
163:                } catch (UnsupportedEncodingException e) {
164:                    throw new Error("HttpClient requires ASCII support");
165:                }
166:            }
167:
168:            /**
169:             * Converts the byte array of ASCII characters to a string. This method is
170:             * to be used when decoding content of HTTP elements (such as response
171:             * headers)
172:             *
173:             * @param data the byte array to be encoded
174:             * @return The string representation of the byte array
175:             */
176:            public static String getAsciiString(final byte[] data) {
177:                if (data == null) {
178:                    throw new IllegalArgumentException(
179:                            "Parameter may not be null");
180:                }
181:                return getAsciiString(data, 0, data.length);
182:            }
183:
184:            /**
185:             * This class should not be instantiated.
186:             */
187:            private EncodingUtils() {
188:            }
189:
190:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.