1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Vladimir N. Molotkov, Stepan M. Mishura 20 * @version $Revision$ 21 */ 22 23 package org.apache.harmony.security.asn1; 24 25 import java.io.IOException; 26 import java.nio.charset.Charsets; 27 import java.text.SimpleDateFormat; 28 import java.util.TimeZone; 29 30 /** 31 * This class represents ASN.1 GeneralizedTime type. 32 * 33 * @see http://asn1.elibel.tm.fr/en/standards/index.htm 34 */ 35 public final class ASN1GeneralizedTime extends ASN1Time { 36 37 // default implementation 38 private static final ASN1GeneralizedTime ASN1 = new ASN1GeneralizedTime(); 39 40 /** 41 * Constructs ASN.1 GeneralizedTime type 42 * 43 * The constructor is provided for inheritance purposes 44 * when there is a need to create a custom ASN.1 GeneralizedTime type. 45 * To get a default implementation it is recommended to use 46 * getInstance() method. 47 */ 48 public ASN1GeneralizedTime() { 49 super(TAG_GENERALIZEDTIME); 50 } 51 52 /** 53 * Returns ASN.1 GeneralizedTime type default implementation 54 * 55 * The default implementation works with encoding 56 * that is represented as Date object. 57 * 58 * @return ASN.1 GeneralizedTime type default implementation 59 */ 60 public static ASN1GeneralizedTime getInstance() { 61 return ASN1; 62 } 63 64 public Object decode(BerInputStream in) throws IOException { 65 in.readGeneralizedTime(); 66 67 if (in.isVerify) { 68 return null; 69 } 70 return getDecodedObject(in); 71 } 72 73 public void encodeContent(BerOutputStream out) { 74 out.encodeGeneralizedTime(); 75 } 76 77 // FIXME support only one format for encoding, do we need others? 78 // 79 // According to X.680: 80 // four digit year, seconds always presented 81 // and fractional-seconds elements without 82 // trailing 0's (must be cut later from content) 83 private static final String GEN_PATTERN = "yyyyMMddHHmmss.SSS"; 84 85 public void setEncodingContent(BerOutputStream out) { 86 SimpleDateFormat sdf = new SimpleDateFormat(GEN_PATTERN); 87 sdf.setTimeZone(TimeZone.getTimeZone("UTC")); 88 String temp = sdf.format(out.content); 89 // cut off trailing 0s 90 int nullId; 91 int currLength; 92 while (((nullId = temp.lastIndexOf('0', currLength = temp.length() - 1)) != -1) 93 & (nullId == currLength)) { 94 temp = temp.substring(0, nullId); 95 } 96 // deal with point (cut off if it is last char) 97 if (temp.charAt(currLength) == '.') { 98 temp = temp.substring(0, currLength); 99 } 100 101 out.content = (temp + "Z").getBytes(Charsets.UTF_8); 102 out.length = ((byte[]) out.content).length; 103 } 104 } 105