27 import java.io.IOException;
28 import java.lang.annotation.Annotation;
29 import java.lang.reflect.Method;
30 import java.net.Inet6Address;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.MalformedURLException;
34 import java.net.ServerSocket;
35 import java.net.URL;
36 import java.net.URLClassLoader;
37 import java.net.UnknownHostException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.nio.file.attribute.FileAttribute;
42 import java.nio.channels.SocketChannel;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.Map;
49 import java.util.HashMap;
50 import java.util.LinkedList;
51 import java.util.List;
52 import java.util.Objects;
53 import java.util.Random;
54 import java.util.function.BooleanSupplier;
55 import java.util.concurrent.TimeUnit;
56 import java.util.function.Consumer;
57 import java.util.function.Function;
58 import java.util.regex.Matcher;
59 import java.util.regex.Pattern;
60
61 import static jdk.test.lib.Asserts.assertTrue;
62 import jdk.test.lib.process.ProcessTools;
63 import jdk.test.lib.process.OutputAnalyzer;
64
65 /**
66 * Common library for various test helper functions.
471 */
472 public static long adjustTimeout(long tOut) {
473 return Math.round(tOut * Utils.TIMEOUT_FACTOR);
474 }
475
476 /**
477 * Return the contents of the named file as a single String,
478 * or null if not found.
479 * @param filename name of the file to read
480 * @return String contents of file, or null if file not found.
481 * @throws IOException
482 * if an I/O error occurs reading from the file or a malformed or
483 * unmappable byte sequence is read
484 */
485 public static String fileAsString(String filename) throws IOException {
486 Path filePath = Paths.get(filename);
487 if (!Files.exists(filePath)) return null;
488 return new String(Files.readAllBytes(filePath));
489 }
490
491 private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
492
493 /**
494 * Returns hex view of byte array
495 *
496 * @param bytes byte array to process
497 * @return space separated hexadecimal string representation of bytes
498 */
499 public static String toHexString(byte[] bytes) {
500 char[] hexView = new char[bytes.length * 3 - 1];
501 for (int i = 0; i < bytes.length - 1; i++) {
502 hexView[i * 3] = hexArray[(bytes[i] >> 4) & 0x0F];
503 hexView[i * 3 + 1] = hexArray[bytes[i] & 0x0F];
504 hexView[i * 3 + 2] = ' ';
505 }
506 hexView[hexView.length - 2] = hexArray[(bytes[bytes.length - 1] >> 4) & 0x0F];
507 hexView[hexView.length - 1] = hexArray[bytes[bytes.length - 1] & 0x0F];
508 return new String(hexView);
509 }
510
511 /**
512 * Returns byte array of hex view
513 *
514 * @param hex hexadecimal string representation
515 * @return byte array
516 */
517 public static byte[] toByteArray(String hex) {
518 int length = hex.length();
519 byte[] bytes = new byte[length / 2];
520 for (int i = 0; i < length; i += 2) {
521 bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
522 + Character.digit(hex.charAt(i + 1), 16));
523 }
524 return bytes;
525 }
526
527 /**
528 * Returns {@link java.util.Random} generator initialized with particular seed.
529 * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
530 * In case no seed is provided, the method uses a random number.
531 * The used seed printed to stdout.
532 * @return {@link java.util.Random} generator with particular seed.
533 */
534 public static Random getRandomInstance() {
535 if (RANDOM_GENERATOR == null) {
536 synchronized (Utils.class) {
537 if (RANDOM_GENERATOR == null) {
538 RANDOM_GENERATOR = new Random(SEED);
539 System.out.printf("For random generator using seed: %d%n", SEED);
540 System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
541 }
542 }
543 }
544 return RANDOM_GENERATOR;
|
27 import java.io.IOException;
28 import java.lang.annotation.Annotation;
29 import java.lang.reflect.Method;
30 import java.net.Inet6Address;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.MalformedURLException;
34 import java.net.ServerSocket;
35 import java.net.URL;
36 import java.net.URLClassLoader;
37 import java.net.UnknownHostException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.nio.file.attribute.FileAttribute;
42 import java.nio.channels.SocketChannel;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.Hex;
48 import java.util.Iterator;
49 import java.util.Map;
50 import java.util.HashMap;
51 import java.util.LinkedList;
52 import java.util.List;
53 import java.util.Objects;
54 import java.util.Random;
55 import java.util.function.BooleanSupplier;
56 import java.util.concurrent.TimeUnit;
57 import java.util.function.Consumer;
58 import java.util.function.Function;
59 import java.util.regex.Matcher;
60 import java.util.regex.Pattern;
61
62 import static jdk.test.lib.Asserts.assertTrue;
63 import jdk.test.lib.process.ProcessTools;
64 import jdk.test.lib.process.OutputAnalyzer;
65
66 /**
67 * Common library for various test helper functions.
472 */
473 public static long adjustTimeout(long tOut) {
474 return Math.round(tOut * Utils.TIMEOUT_FACTOR);
475 }
476
477 /**
478 * Return the contents of the named file as a single String,
479 * or null if not found.
480 * @param filename name of the file to read
481 * @return String contents of file, or null if file not found.
482 * @throws IOException
483 * if an I/O error occurs reading from the file or a malformed or
484 * unmappable byte sequence is read
485 */
486 public static String fileAsString(String filename) throws IOException {
487 Path filePath = Paths.get(filename);
488 if (!Files.exists(filePath)) return null;
489 return new String(Files.readAllBytes(filePath));
490 }
491
492 /**
493 * Returns hex view of byte array
494 *
495 * @param bytes byte array to process
496 * @return space separated hexadecimal string representation of bytes
497 * @deprecated replaced by {@link java.util.Hex#encoder(CharSequence)} (String) Hex.encoder(" ").encode
498 * (byte[], char)}.
499 */
500 @Deprecated
501 public static String toHexString(byte[] bytes) {
502 return Hex.encoder(" ", "", "", true).encode(bytes);
503 }
504
505 /**
506 * Returns byte array of hex view
507 *
508 * @param hex hexadecimal string representation
509 * @return byte array
510 */
511 public static byte[] toByteArray(String hex) {
512 return Hex.decoder().decode(hex);
513 }
514
515 /**
516 * Returns {@link java.util.Random} generator initialized with particular seed.
517 * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
518 * In case no seed is provided, the method uses a random number.
519 * The used seed printed to stdout.
520 * @return {@link java.util.Random} generator with particular seed.
521 */
522 public static Random getRandomInstance() {
523 if (RANDOM_GENERATOR == null) {
524 synchronized (Utils.class) {
525 if (RANDOM_GENERATOR == null) {
526 RANDOM_GENERATOR = new Random(SEED);
527 System.out.printf("For random generator using seed: %d%n", SEED);
528 System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
529 }
530 }
531 }
532 return RANDOM_GENERATOR;
|