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, Stepan M. Mishura
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 GeneralizedTime type.
029: *
030: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
031: */
032:
033: public class ASN1GeneralizedTime extends ASN1Time {
034:
035: // default implementation
036: private static final ASN1GeneralizedTime ASN1 = new ASN1GeneralizedTime();
037:
038: /**
039: * Constructs ASN.1 GeneralizedTime type
040: *
041: * The constructor is provided for inheritance purposes
042: * when there is a need to create a custom ASN.1 GeneralizedTime type.
043: * To get a default implementation it is recommended to use
044: * getInstance() method.
045: */
046: public ASN1GeneralizedTime() {
047: super (TAG_GENERALIZEDTIME);
048: }
049:
050: /**
051: * Returns ASN.1 GeneralizedTime type default implementation
052: *
053: * The default implementation works with encoding
054: * that is represented as Date object.
055: *
056: * @return ASN.1 GeneralizedTime type default implementation
057: */
058: public static ASN1GeneralizedTime getInstance() {
059: return ASN1;
060: }
061:
062: //
063: //
064: // Decode
065: //
066: //
067:
068: public Object decode(BerInputStream in) throws IOException {
069: in.readGeneralizedTime();
070:
071: if (in.isVerify) {
072: return null;
073: }
074: return getDecodedObject(in);
075: }
076:
077: //
078: //
079: // Encode
080: //
081: //
082:
083: public void encodeContent(BerOutputStream out) {
084: out.encodeGeneralizedTime();
085: }
086:
087: // FIXME support only one format for encoding, do we need others?
088: //
089: // According to X.680:
090: // four digit year, seconds always presented
091: // and fractional-seconds elements without
092: // trailing 0's (must be cut later from content)
093: private final static String GEN_PATTERN = "yyyyMMddHHmmss.SSS"; //$NON-NLS-1$
094:
095: public void setEncodingContent(BerOutputStream out) {
096:
097: SimpleDateFormat sdf = new SimpleDateFormat(GEN_PATTERN);
098: sdf.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
099: String temp = sdf.format(out.content);
100: // cut off trailing 0s
101: int nullId;
102: int currLength;
103: while (((nullId = temp.lastIndexOf('0', currLength = temp
104: .length() - 1)) != -1)
105: & (nullId == currLength)) {
106: temp = temp.substring(0, nullId);
107: }
108: // deal with point (cut off if it is last char)
109: if (temp.charAt(currLength) == '.') {
110: temp = temp.substring(0, currLength);
111: }
112: out.content = (temp + "Z").getBytes(); //$NON-NLS-1$
113: out.length = ((byte[]) out.content).length;
114: }
115: }
|