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: /**
019: * @author Vladimir N. Molotkov
020: * @version $Revision$
021: */package org.apache.harmony.security.asn1;
022:
023: import java.io.IOException;
024: import java.text.SimpleDateFormat;
025: import java.util.TimeZone;
026:
027: /**
028: * This class represents ASN.1 UTCTime type
029: *
030: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
031: */
032: public class ASN1UTCTime extends ASN1Time {
033:
034: /**
035: * Length for the pattern: YYMMDDhhmm'Z'
036: */
037: public static final int UTC_HM = 11;
038:
039: /**
040: * Length for the pattern: YYMMDDhhmmss'Z'
041: */
042: public static final int UTC_HMS = 13;
043:
044: /**
045: * Length for the pattern: YYMMDDhhmm('+'/'-')hhmm
046: */
047: public static final int UTC_LOCAL_HM = 15;
048:
049: /**
050: * Length for the pattern: YYMMDDhhmmss('+'/'-')hhmm
051: */
052: public static final int UTC_LOCAL_HMS = 17;
053:
054: // default implementation
055: private static final ASN1UTCTime ASN1 = new ASN1UTCTime();
056:
057: /**
058: * Constructs ASN.1 UTCTime type
059: *
060: * The constructor is provided for inheritance purposes
061: * when there is a need to create a custom ASN.1 UTCTime type.
062: * To get a default implementation it is recommended to use
063: * getInstance() method.
064: */
065: public ASN1UTCTime() {
066: super (TAG_UTCTIME);
067: }
068:
069: /**
070: * Returns ASN.1 UTCTime type default implementation
071: *
072: * The default implementation works with encoding
073: * that is represented as Date object.
074: *
075: * @return ASN.1 UTCTime type default implementation
076: */
077: public static ASN1UTCTime getInstance() {
078: return ASN1;
079: }
080:
081: //
082: //
083: // Decode
084: //
085: //
086:
087: public Object decode(BerInputStream in) throws IOException {
088: in.readUTCTime();
089:
090: if (in.isVerify) {
091: return null;
092: }
093: return getDecodedObject(in);
094: }
095:
096: //
097: //
098: // Encode
099: //
100: //
101: public void encodeContent(BerOutputStream out) {
102: out.encodeUTCTime();
103: }
104:
105: // FIXME support only one format for encoding, do we need others?
106: //
107: // According to X.680 coordinated universal time format:
108: // two digit year, seconds always presented,
109: // no fractional-seconds elements, 'Z' at the end
110: private final static String UTC_PATTERN = "yyMMddHHmmss'Z'"; //$NON-NLS-1$
111:
112: public void setEncodingContent(BerOutputStream out) {
113: SimpleDateFormat sdf = new SimpleDateFormat(UTC_PATTERN);
114: sdf.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
115: out.content = sdf.format(out.content).getBytes();
116: out.length = ((byte[]) out.content).length;
117: }
118: }
|