| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.ber.stream; |
| 39 | |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.io.OutputStream; |
| 43 | |
| 44 | /** |
| 45 | * This class is for the UTCTime object. |
| 46 | * <P>See CCITT X.209. |
| 47 | * |
| 48 | * <pre> |
| 49 | * ENCODING RULE: |
| 50 | * Primitive Definite length. |
| 51 | * tag = 0x17 |
| 52 | * length = (short or long form) |
| 53 | * one or more contents octets |
| 54 | * </pre> |
| 55 | * |
| 56 | * @version 1.0 |
| 57 | */ |
| 58 | public class BERUTCTime extends BERElement { |
| 59 | |
| 60 | /** |
| 61 | * Internal variables |
| 62 | */ |
| 63 | private String m_value = null; |
| 64 | |
| 65 | /** |
| 66 | * Constructs a UTC time element containing the specified string. |
| 67 | * @param utc_string string in UTC time format |
| 68 | */ |
| 69 | public BERUTCTime(String utc_string) { |
| 70 | m_value = utc_string; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Constructs a UTCTime element from an input stream |
| 75 | * (for constructed encoding) |
| 76 | * @param stream source |
| 77 | * @param bytes_read array of 1 int, incremented by number of bytes read |
| 78 | * @exception IOException failed to construct |
| 79 | */ |
| 80 | public BERUTCTime(BERTagDecoder decoder, InputStream stream, |
| 81 | int[] bytes_read) throws IOException { |
| 82 | int octet; |
| 83 | int contents_length = super.readLengthOctets(stream, bytes_read); |
| 84 | int[] component_length = new int[1]; |
| 85 | BERElement element = null; |
| 86 | |
| 87 | m_value = ""; |
| 88 | |
| 89 | if (contents_length == -1) { |
| 90 | /* Constructed - indefinite length content octets. */ |
| 91 | { |
| 92 | component_length[0] = 0; |
| 93 | element = getElement(decoder,stream,component_length); |
| 94 | if (element != null) { |
| 95 | /* element is an octetstring - add it to the existing buffer */ |
| 96 | BERUTCTime utc_element = (BERUTCTime)element; |
| 97 | m_value += utc_element.getValue(); |
| 98 | } |
| 99 | } while (element != null); |
| 100 | } else { |
| 101 | /* Definite length content octets string. */ |
| 102 | bytes_read[0] += contents_length; |
| 103 | while (contents_length > 0) { |
| 104 | component_length[0] = 0; |
| 105 | element = getElement(decoder,stream,component_length); |
| 106 | if (element != null) { |
| 107 | /* element is an octetstring - add it to the existing buffer */ |
| 108 | BERUTCTime utc_element = (BERUTCTime)element; |
| 109 | m_value += utc_element.getValue(); |
| 110 | } |
| 111 | contents_length -= component_length[0]; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Constructs a UTC time element from an input stream |
| 118 | * (for primitive encoding) |
| 119 | * @param stream source |
| 120 | * @param bytes_read array of 1 int, incremented by number of bytes read |
| 121 | * @exception IOException failed to construct |
| 122 | */ |
| 123 | public BERUTCTime(InputStream stream, int[] bytes_read) throws IOException { |
| 124 | int contents_length = super.readLengthOctets(stream, bytes_read); |
| 125 | |
| 126 | /* Definite length content octets string. */ |
| 127 | if (contents_length > 0) { |
| 128 | byte[] byte_buf = new byte[contents_length]; |
| 129 | for (int i = 0; i < contents_length; i++) { |
| 130 | byte_buf[i] = (byte) stream.read(); |
| 131 | } |
| 132 | bytes_read[0] += contents_length; |
| 133 | try{ |
| 134 | m_value = new String(byte_buf, "UTF8"); |
| 135 | } catch(Throwable x) |
| 136 | {} |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Writes BER to a stream. |
| 142 | * @return number of bytes written to stream. |
| 143 | * @exception IOException failed to write |
| 144 | */ |
| 145 | private byte[] byte_buf; |
| 146 | |
| 147 | public void write(OutputStream stream) throws IOException { |
| 148 | stream.write((byte)getType()); |
| 149 | if (m_value == null) { |
| 150 | sendDefiniteLength(stream, 0); |
| 151 | } else { |
| 152 | |
| 153 | try{ |
| 154 | byte_buf = m_value.getBytes("UTF8"); |
| 155 | sendDefiniteLength(stream, byte_buf.length); /* length */ |
| 156 | } catch(Throwable x) |
| 157 | {} |
| 158 | stream.write(byte_buf,0,byte_buf.length); /* contents */ |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Gets the element value. |
| 164 | * @param element value |
| 165 | */ |
| 166 | public String getValue() { |
| 167 | return m_value; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Gets the element type. |
| 172 | * @param element type |
| 173 | */ |
| 174 | public int getType() { |
| 175 | return BERElement.UTCTIME; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets the string representation. |
| 180 | * NOTE: currently prints out values in decimal form. |
| 181 | * @return string representation of tag. |
| 182 | */ |
| 183 | public String toString() { |
| 184 | if (m_value == null) |
| 185 | return "UTCTime (null)"; |
| 186 | else |
| 187 | return "UTCTime {" + m_value + "}"; |
| 188 | } |
| 189 | } |