8234032: Improve basic calendar services

Reviewed-by: weijun, rhalade, mschoene
This commit is contained in:
Sean Mullan 2020-01-14 14:51:57 -05:00
parent 653af300cc
commit 1bfcf768f5

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -312,15 +312,15 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
if (generalized) { if (generalized) {
type = "Generalized"; type = "Generalized";
year = 1000 * Character.digit((char)buf[pos++], 10); year = 1000 * toDigit(buf[pos++], type);
year += 100 * Character.digit((char)buf[pos++], 10); year += 100 * toDigit(buf[pos++], type);
year += 10 * Character.digit((char)buf[pos++], 10); year += 10 * toDigit(buf[pos++], type);
year += Character.digit((char)buf[pos++], 10); year += toDigit(buf[pos++], type);
len -= 2; // For the two extra YY len -= 2; // For the two extra YY
} else { } else {
type = "UTC"; type = "UTC";
year = 10 * Character.digit((char)buf[pos++], 10); year = 10 * toDigit(buf[pos++], type);
year += Character.digit((char)buf[pos++], 10); year += toDigit(buf[pos++], type);
if (year < 50) // origin 2000 if (year < 50) // origin 2000
year += 2000; year += 2000;
@ -328,17 +328,17 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
year += 1900; // origin 1900 year += 1900; // origin 1900
} }
month = 10 * Character.digit((char)buf[pos++], 10); month = 10 * toDigit(buf[pos++], type);
month += Character.digit((char)buf[pos++], 10); month += toDigit(buf[pos++], type);
day = 10 * Character.digit((char)buf[pos++], 10); day = 10 * toDigit(buf[pos++], type);
day += Character.digit((char)buf[pos++], 10); day += toDigit(buf[pos++], type);
hour = 10 * Character.digit((char)buf[pos++], 10); hour = 10 * toDigit(buf[pos++], type);
hour += Character.digit((char)buf[pos++], 10); hour += toDigit(buf[pos++], type);
minute = 10 * Character.digit((char)buf[pos++], 10); minute = 10 * toDigit(buf[pos++], type);
minute += Character.digit((char)buf[pos++], 10); minute += toDigit(buf[pos++], type);
len -= 10; // YYMMDDhhmm len -= 10; // YYMMDDhhmm
@ -350,8 +350,8 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
millis = 0; millis = 0;
if (len > 2) { if (len > 2) {
second = 10 * Character.digit((char)buf[pos++], 10); second = 10 * toDigit(buf[pos++], type);
second += Character.digit((char)buf[pos++], 10); second += toDigit(buf[pos++], type);
len -= 2; len -= 2;
// handle fractional seconds (if present) // handle fractional seconds (if present)
if (buf[pos] == '.' || buf[pos] == ',') { if (buf[pos] == '.' || buf[pos] == ',') {
@ -363,7 +363,7 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
buf[pos] != '-') { buf[pos] != '-') {
// Validate all digits in the fractional part but // Validate all digits in the fractional part but
// store millisecond precision only // store millisecond precision only
int thisDigit = Character.digit((char)buf[pos], 10); int thisDigit = toDigit(buf[pos], type);
precision++; precision++;
pos++; pos++;
switch (precision) { switch (precision) {
@ -412,10 +412,10 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
switch (buf[pos++]) { switch (buf[pos++]) {
case '+': case '+':
hr = 10 * Character.digit((char)buf[pos++], 10); hr = 10 * toDigit(buf[pos++], type);
hr += Character.digit((char)buf[pos++], 10); hr += toDigit(buf[pos++], type);
min = 10 * Character.digit((char)buf[pos++], 10); min = 10 * toDigit(buf[pos++], type);
min += Character.digit((char)buf[pos++], 10); min += toDigit(buf[pos++], type);
if (hr >= 24 || min >= 60) if (hr >= 24 || min >= 60)
throw new IOException("Parse " + type + " time, +hhmm"); throw new IOException("Parse " + type + " time, +hhmm");
@ -424,10 +424,10 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
break; break;
case '-': case '-':
hr = 10 * Character.digit((char)buf[pos++], 10); hr = 10 * toDigit(buf[pos++], type);
hr += Character.digit((char)buf[pos++], 10); hr += toDigit(buf[pos++], type);
min = 10 * Character.digit((char)buf[pos++], 10); min = 10 * toDigit(buf[pos++], type);
min += Character.digit((char)buf[pos++], 10); min += toDigit(buf[pos++], type);
if (hr >= 24 || min >= 60) if (hr >= 24 || min >= 60)
throw new IOException("Parse " + type + " time, -hhmm"); throw new IOException("Parse " + type + " time, -hhmm");
@ -443,4 +443,16 @@ class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
} }
return new Date(time); return new Date(time);
} }
/**
* Converts byte (represented as a char) to int.
* @throws IOException if integer is not a valid digit in the specified
* radix (10)
*/
private static int toDigit(byte b, String type) throws IOException {
if (b < '0' || b > '9') {
throw new IOException("Parse " + type + " time, invalid format");
}
return b - '0';
}
} }