001: /*
002: * @(#)SerialNumber.java 1.16 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.security.x509;
028:
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.math.BigInteger;
032:
033: import sun.security.util.*;
034:
035: /**
036: * This class defines the SerialNumber class used by certificates.
037: *
038: * @author Amit Kapoor
039: * @author Hemma Prafullchandra
040: * @version 1.9
041: */
042: public class SerialNumber {
043: private BigInteger serialNum;
044:
045: // Construct the class from the DerValue
046: private void construct(DerValue derVal) throws IOException {
047: serialNum = derVal.getBigInteger();
048: if (derVal.data.available() != 0) {
049: throw new IOException("Excess SerialNumber data");
050: }
051: }
052:
053: /**
054: * The default constructor for this class using BigInteger.
055: *
056: * @param num the BigInteger number used to create the serial number.
057: */
058: public SerialNumber(BigInteger num) {
059: serialNum = num;
060: }
061:
062: /**
063: * The default constructor for this class using int.
064: *
065: * @param num the BigInteger number used to create the serial number.
066: */
067: public SerialNumber(int num) {
068: serialNum = BigInteger.valueOf(num);
069: }
070:
071: /**
072: * Create the object, decoding the values from the passed DER stream.
073: *
074: * @param in the DerInputStream to read the SerialNumber from.
075: * @exception IOException on decoding errors.
076: */
077: public SerialNumber(DerInputStream in) throws IOException {
078: DerValue derVal = in.getDerValue();
079: construct(derVal);
080: }
081:
082: /**
083: * Create the object, decoding the values from the passed DerValue.
084: *
085: * @param val the DerValue to read the SerialNumber from.
086: * @exception IOException on decoding errors.
087: */
088: public SerialNumber(DerValue val) throws IOException {
089: construct(val);
090: }
091:
092: /**
093: * Create the object, decoding the values from the passed stream.
094: *
095: * @param in the InputStream to read the SerialNumber from.
096: * @exception IOException on decoding errors.
097: */
098: public SerialNumber(InputStream in) throws IOException {
099: DerValue derVal = new DerValue(in);
100: construct(derVal);
101: }
102:
103: /**
104: * Return the SerialNumber as user readable string.
105: */
106: public String toString() {
107: return ("SerialNumber: [" + Debug.toHexString(serialNum) + "]");
108: }
109:
110: /**
111: * Encode the SerialNumber in DER form to the stream.
112: *
113: * @param out the DerOutputStream to marshal the contents to.
114: * @exception IOException on errors.
115: */
116: public void encode(DerOutputStream out) throws IOException {
117: out.putInteger(serialNum);
118: }
119:
120: /**
121: * Return the serial number.
122: */
123: public BigInteger getNumber() {
124: return serialNum;
125: }
126: }
|