Source Code Cross Referenced for Utils.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » jndi » provider » ldap » asn1 » 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 » Apache Harmony Java SE » org package » org.apache.harmony.jndi.provider.ldap.asn1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more 
003:         *  contributor license agreements.  See the NOTICE file distributed with 
004:         *  this work for additional information regarding copyright ownership. 
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0 
006:         *  (the "License"); you may not use this file except in compliance with 
007:         *  the License.  You may obtain a copy of the License at 
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0 
010:         * 
011:         *  Unless required by applicable law or agreed to in writing, software 
012:         *  distributed under the License is distributed on an "AS IS" BASIS, 
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014:         *  See the License for the specific language governing permissions and 
015:         *  limitations under the License. 
016:         */
017:
018:        package org.apache.harmony.jndi.provider.ldap.asn1;
019:
020:        import java.io.UnsupportedEncodingException;
021:
022:        import org.apache.harmony.security.asn1.ASN1Sequence;
023:        import org.apache.harmony.security.asn1.ASN1Type;
024:
025:        public class Utils {
026:
027:            public static final String CODING_CHARSET = "UTF-8"; //$NON-NLS-1$
028:
029:            /**
030:             * conjoin two ASN1Sequence type as new one
031:             * 
032:             * @param first
033:             *            first ASN1Sequence type
034:             * @param second
035:             *            secod ASN1Sequence type
036:             * @return a new joined ASN1Sequence type
037:             */
038:            public static ASN1Sequence conjoinSequence(ASN1Sequence first,
039:                    ASN1Sequence second) {
040:                if (first == null) {
041:                    return second;
042:                }
043:
044:                if (second == null) {
045:                    return first;
046:                }
047:
048:                ASN1Type[] result = new ASN1Type[first.type.length
049:                        + second.type.length];
050:                System.arraycopy(first.type, 0, result, 0, first.type.length);
051:                System.arraycopy(second.type, 0, result, first.type.length,
052:                        second.type.length);
053:
054:                ASN1Sequence sequence = new ASN1SequenceWrap(result);
055:
056:                System.arraycopy(first.OPTIONAL, 0, sequence.OPTIONAL, 0,
057:                        first.OPTIONAL.length);
058:                System.arraycopy(second.OPTIONAL, 0, sequence.OPTIONAL,
059:                        first.OPTIONAL.length, second.OPTIONAL.length);
060:
061:                System.arraycopy(first.DEFAULT, 0, sequence.DEFAULT, 0,
062:                        first.DEFAULT.length);
063:                System.arraycopy(second.DEFAULT, 0, sequence.DEFAULT,
064:                        first.DEFAULT.length, second.DEFAULT.length);
065:                return sequence;
066:            }
067:
068:            /**
069:             * Convert <code>obj</code> to <code>String</code>. If obj is byte[],
070:             * then using UTF-8 charset. when <code>obj</code> is <code>null</code>,
071:             * empty String would be returned.
072:             * 
073:             * @param obj
074:             *            object to be covert
075:             * @return UTF-8 String
076:             */
077:            public static String getString(Object obj) {
078:                if (obj == null) {
079:                    return ""; //$NON-NLS-1$
080:                }
081:
082:                if (obj instanceof  byte[]) {
083:                    try {
084:                        return new String((byte[]) obj, CODING_CHARSET);
085:                    } catch (UnsupportedEncodingException e) {
086:                        // never reached, because UTF-8 is supported by all java
087:                        // platform
088:                        return ""; //$NON-NLS-1$
089:                    }
090:                } else if (obj instanceof  char[]) {
091:                    return new String((char[]) obj);
092:                } else {
093:                    return (String) obj;
094:                }
095:
096:            }
097:
098:            /**
099:             * Encodes <code>obj</code> into a sequence of bytes using UTF-8 charset.
100:             * 
101:             * @param obj
102:             *            object to be encoded
103:             * @return UTF-8 byte[]
104:             */
105:            public static byte[] getBytes(Object obj) {
106:                if (obj == null) {
107:                    return new byte[0];
108:                }
109:
110:                if (obj instanceof  String) {
111:                    try {
112:                        return ((String) obj).getBytes(CODING_CHARSET);
113:                    } catch (UnsupportedEncodingException e) {
114:                        // never reached, because UTF-8 is supported by all java platform
115:                        return new byte[0];
116:                    }
117:                } else if (obj instanceof  char[]) {
118:                    try {
119:                        return new String((char[]) obj)
120:                                .getBytes(CODING_CHARSET);
121:                    } catch (UnsupportedEncodingException e) {
122:                        // never reached, because UTF-8 is supported by all java platform
123:                        return new byte[0];
124:                    }
125:                } else {
126:                    // no other types, ignore class cast expection
127:                    return (byte[]) obj;
128:                }
129:            }
130:
131:            /**
132:             * Convert <code>obj</code> to <code>char[]</code>. If obj is byte[],
133:             * then using UTF-8 charset. when <code>obj</code> is <code>null</code>,
134:             * zero length char array would be returned.
135:             * 
136:             * @param obj
137:             *            object to be covert
138:             * @return UTF-8 char[]
139:             */
140:            public static char[] getCharArray(Object obj) {
141:                if (obj == null) {
142:                    return new char[0];
143:                }
144:
145:                if (obj instanceof  String) {
146:                    return ((String) obj).toCharArray();
147:                } else if (obj instanceof  byte[]) {
148:                    try {
149:                        return new String((byte[]) obj, CODING_CHARSET)
150:                                .toCharArray();
151:                    } catch (UnsupportedEncodingException e) {
152:                        // never reached, because UTF-8 is supported by all java
153:                        // platform
154:                        return new char[0];
155:                    }
156:                } else {
157:                    // no other types, ignore class cast expection
158:                    return (char[]) obj;
159:                }
160:            }
161:        }
ww___w_.__j___a__v_a___2___s._c__o_m___ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.