Files
javademos/other-changes/src/Api27.java
T

43 lines
2.2 KiB
Java

import java.math.BigDecimal;
import java.math.MathContext;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.time.Instant;
import java.util.Set;
import javax.crypto.spec.SecretKeySpec;
/**
* Small additions to the JDK 27 public API found by diffing javap dumps of 26 and 27
* (docs/output/51-api-diff-26-to-27.txt), each exercised once. Compile with --release 27.
*/
public class Api27 {
public static void main(String[] args) throws Exception {
System.out.printf("Math.acosh(2) = %.6f%n", Math.acosh(2));
System.out.printf("Math.asinh(1) = %.6f%n", Math.asinh(1));
System.out.printf("Math.atanh(.5) = %.6f%n", Math.atanh(0.5));
Check.that(Math.abs(Math.acosh(Math.cosh(1.5)) - 1.5) < 1e-12, "acosh inverts cosh");
String s = "na\u00efve caf\u00e9";
int utf8 = s.encodedLength(StandardCharsets.UTF_8);
System.out.println("\"" + s + "\".length() = " + s.length() + ", encodedLength(UTF_8) = " + utf8);
Check.that(utf8 == s.getBytes(StandardCharsets.UTF_8).length, "encodedLength matches getBytes().length without allocating the array");
BigDecimal cube = new BigDecimal("27").rootn(3, MathContext.DECIMAL64);
System.out.println("BigDecimal(27).rootn(3) = " + cube);
Check.that(cube.compareTo(new BigDecimal("3")) == 0, "rootn(3) of 27 is 3");
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(null, null);
char[] pw = "changeit".toCharArray();
ks.setEntry("aes-key", new KeyStore.SecretKeyEntry(new SecretKeySpec(new byte[16], "AES")),
new KeyStore.PasswordProtection(pw));
Instant created = ks.getCreationInstant("aes-key");
System.out.println("KeyStore.getCreationInstant(\"aes-key\") returns an Instant: " + (created != null));
Check.that(created != null && Math.abs(created.toEpochMilli() - System.currentTimeMillis()) < 60_000,
"KeyStore.getCreationInstant returns the entry's creation time as a java.time.Instant");
Set<Integer> evens = Set.ofLazy(Set.of(1, 2, 3, 4), n -> n % 2 == 0);
Check.that(evens.contains(2) && !evens.contains(3), "Set.ofLazy(Set, Predicate) exists (preview API)");
}
}