< prev index next >

test/lib/testlibrary/jdk/testlibrary/Utils.java

Print this page
rev 13311 : 8193879: Java debugger hangs on method invocation
Reviewed-by: sspitsyn
rev 13428 : jfr backport


  25 
  26 import static jdk.testlibrary.Asserts.assertTrue;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileReader;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.net.InetAddress;
  35 import java.net.ServerSocket;
  36 import java.net.UnknownHostException;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.Arrays;
  40 import java.util.Collections;
  41 import java.util.Objects;
  42 import java.util.regex.Pattern;
  43 import java.util.regex.Matcher;
  44 import java.util.concurrent.TimeUnit;

  45 import java.util.function.Function;
  46 
  47 /**
  48  * Common library for various test helper functions.
  49  */
  50 public final class Utils {
  51 
  52     /**
  53      * Returns the sequence used by operating system to separate lines.
  54      */
  55     public static final String NEW_LINE = System.getProperty("line.separator");
  56 
  57     /**
  58      * Returns the value of 'test.vm.opts'system property.
  59      */
  60     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  61 
  62     /**
  63      * Returns the value of 'test.java.opts'system property.
  64      */


 393      * one, or both, streams may be in an inconsistent state. It is strongly
 394      * recommended that both streams be promptly closed if an I/O error occurs.
 395      *
 396      * @param  in the input stream, non-null
 397      * @param  out the output stream, non-null
 398      * @return the number of bytes transferred
 399      * @throws IOException if an I/O error occurs when reading or writing
 400      * @throws NullPointerException if {@code in} or {@code out} is {@code null}
 401      *
 402      */
 403     public static long transferTo(InputStream in, OutputStream out)
 404             throws IOException  {
 405         long transferred = 0;
 406         byte[] buffer = new byte[BUFFER_SIZE];
 407         int read;
 408         while ((read = in.read(buffer, 0, BUFFER_SIZE)) >= 0) {
 409             out.write(buffer, 0, read);
 410             transferred += read;
 411         }
 412         return transferred;














































 413     }
 414 }


  25 
  26 import static jdk.testlibrary.Asserts.assertTrue;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileReader;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.net.InetAddress;
  35 import java.net.ServerSocket;
  36 import java.net.UnknownHostException;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.Arrays;
  40 import java.util.Collections;
  41 import java.util.Objects;
  42 import java.util.regex.Pattern;
  43 import java.util.regex.Matcher;
  44 import java.util.concurrent.TimeUnit;
  45 import java.util.function.BooleanSupplier;
  46 import java.util.function.Function;
  47 
  48 /**
  49  * Common library for various test helper functions.
  50  */
  51 public final class Utils {
  52 
  53     /**
  54      * Returns the sequence used by operating system to separate lines.
  55      */
  56     public static final String NEW_LINE = System.getProperty("line.separator");
  57 
  58     /**
  59      * Returns the value of 'test.vm.opts'system property.
  60      */
  61     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  62 
  63     /**
  64      * Returns the value of 'test.java.opts'system property.
  65      */


 394      * one, or both, streams may be in an inconsistent state. It is strongly
 395      * recommended that both streams be promptly closed if an I/O error occurs.
 396      *
 397      * @param  in the input stream, non-null
 398      * @param  out the output stream, non-null
 399      * @return the number of bytes transferred
 400      * @throws IOException if an I/O error occurs when reading or writing
 401      * @throws NullPointerException if {@code in} or {@code out} is {@code null}
 402      *
 403      */
 404     public static long transferTo(InputStream in, OutputStream out)
 405             throws IOException  {
 406         long transferred = 0;
 407         byte[] buffer = new byte[BUFFER_SIZE];
 408         int read;
 409         while ((read = in.read(buffer, 0, BUFFER_SIZE)) >= 0) {
 410             out.write(buffer, 0, read);
 411             transferred += read;
 412         }
 413         return transferred;
 414     }
 415 
 416     /**
 417      * Wait for condition to be true
 418      *
 419      * @param condition, a condition to wait for
 420      */
 421     public static final void waitForCondition(BooleanSupplier condition) {
 422         waitForCondition(condition, -1L, 100L);
 423     }
 424 
 425     /**
 426      * Wait until timeout for condition to be true
 427      *
 428      * @param condition, a condition to wait for
 429      * @param timeout a time in milliseconds to wait for condition to be true
 430      * specifying -1 will wait forever
 431      * @return condition value, to determine if wait was successful
 432      */
 433     public static final boolean waitForCondition(BooleanSupplier condition,
 434             long timeout) {
 435         return waitForCondition(condition, timeout, 100L);
 436     }
 437 
 438     /**
 439      * Wait until timeout for condition to be true for specified time
 440      *
 441      * @param condition, a condition to wait for
 442      * @param timeout a time in milliseconds to wait for condition to be true,
 443      * specifying -1 will wait forever
 444      * @param sleepTime a time to sleep value in milliseconds
 445      * @return condition value, to determine if wait was successful
 446      */
 447     public static final boolean waitForCondition(BooleanSupplier condition,
 448             long timeout, long sleepTime) {
 449         long startTime = System.currentTimeMillis();
 450         while (!(condition.getAsBoolean() || (timeout != -1L
 451                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 452             try {
 453                 Thread.sleep(sleepTime);
 454             } catch (InterruptedException e) {
 455                 Thread.currentThread().interrupt();
 456                 throw new Error(e);
 457             }
 458         }
 459         return condition.getAsBoolean();
 460     }
 461 }
< prev index next >