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: }
|