27
27
import java .io .IOException ;
28
28
import java .security .cert .*;
29
29
import java .util .Date ;
30
+ import java .util .Objects ;
30
31
31
32
import sun .security .util .*;
32
33
@@ -46,8 +47,8 @@ public class CertificateValidity implements DerEncoder {
46
47
static final long YR_2050 = 2524608000000L ;
47
48
48
49
// Private data members
49
- private Date notBefore ;
50
- private Date notAfter ;
50
+ private final Date notBefore ;
51
+ private final Date notAfter ;
51
52
52
53
// Returns the first time the certificate is valid.
53
54
public Date getNotBefore () {
@@ -59,8 +60,27 @@ public Date getNotAfter() {
59
60
return new Date (notAfter .getTime ());
60
61
}
61
62
62
- // Construct the class from the DerValue
63
- private void construct (DerValue derVal ) throws IOException {
63
+ /**
64
+ * The constructor for this class for the specified interval.
65
+ *
66
+ * @param notBefore the date and time before which the certificate
67
+ * is not valid
68
+ * @param notAfter the date and time after which the certificate is
69
+ * not valid
70
+ */
71
+ public CertificateValidity (Date notBefore , Date notAfter ) {
72
+ this .notBefore = Objects .requireNonNull (notBefore );
73
+ this .notAfter = Objects .requireNonNull (notAfter );
74
+ }
75
+
76
+ /**
77
+ * Create the object, decoding the values from the passed DER stream.
78
+ *
79
+ * @param in the DerInputStream to read the CertificateValidity from
80
+ * @exception IOException on decoding errors.
81
+ */
82
+ public CertificateValidity (DerInputStream in ) throws IOException {
83
+ DerValue derVal = in .getDerValue ();
64
84
if (derVal .tag != DerValue .tag_Sequence ) {
65
85
throw new IOException ("Invalid encoded CertificateValidity, " +
66
86
"starting sequence tag missing." );
@@ -91,41 +111,10 @@ private void construct(DerValue derVal) throws IOException {
91
111
}
92
112
}
93
113
94
- /**
95
- * Default constructor for the class.
96
- */
97
- public CertificateValidity () { }
98
-
99
- /**
100
- * The default constructor for this class for the specified interval.
101
- *
102
- * @param notBefore the date and time before which the certificate
103
- * is not valid.
104
- * @param notAfter the date and time after which the certificate is
105
- * not valid.
106
- */
107
- public CertificateValidity (Date notBefore , Date notAfter ) {
108
- this .notBefore = notBefore ;
109
- this .notAfter = notAfter ;
110
- }
111
-
112
- /**
113
- * Create the object, decoding the values from the passed DER stream.
114
- *
115
- * @param in the DerInputStream to read the CertificateValidity from.
116
- * @exception IOException on decoding errors.
117
- */
118
- public CertificateValidity (DerInputStream in ) throws IOException {
119
- DerValue derVal = in .getDerValue ();
120
- construct (derVal );
121
- }
122
-
123
114
/**
124
115
* Return the validity period as user readable string.
125
116
*/
126
117
public String toString () {
127
- if (notBefore == null || notAfter == null )
128
- return "" ;
129
118
return "Validity: [From: " + notBefore +
130
119
",\n To: " + notAfter + ']' ;
131
120
}
@@ -139,12 +128,6 @@ public String toString() {
139
128
@ Override
140
129
public void encode (DerOutputStream out ) throws IOException {
141
130
142
- // in cases where default constructor is used check for
143
- // null values
144
- if (notBefore == null || notAfter == null ) {
145
- throw new IOException ("CertificateValidity:" +
146
- " null values to encode.\n " );
147
- }
148
131
DerOutputStream pair = new DerOutputStream ();
149
132
150
133
if (notBefore .getTime () < YR_2050 ) {
0 commit comments