PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/openjdk7/jdk/test/java/nio/channels/TestUtil.java

https://github.com/Helbrass/openjdk-fontfix
Java | 105 lines | 48 code | 12 blank | 45 comment | 4 complexity | 0ae67a2a5c33a3d8f3f1744a67b0acdf MD5 | raw file
  1. /*
  2. * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /* Test utilities
  24. *
  25. */
  26. import java.io.*;
  27. import java.net.*;
  28. import java.nio.*;
  29. import java.nio.channels.*;
  30. import java.util.Random;
  31. public class TestUtil {
  32. // Test hosts used by the channels tests - change these when
  33. // executing in a different network.
  34. public static final String HOST = "javaweb.sfbay.sun.com";
  35. public static final String REFUSING_HOST = "jano1.sfbay.sun.com";
  36. public static final String FAR_HOST = "irejano.ireland.sun.com";
  37. public static final String UNRESOLVABLE_HOST = "blah-blah.blah-blah.blah";
  38. private TestUtil() { }
  39. // Repeatedly try random ports until we bind to one. You might be tempted
  40. // to do this:
  41. //
  42. // ServerSocketChannel ssc = ServerSocketChannel.open();
  43. // ssc.socket().bind(new InetSocketAddress(0));
  44. // SocketAddress sa = ssc.socket().getLocalSocketAddress();
  45. //
  46. // but unfortunately it doesn't work on NT 4.0.
  47. //
  48. // Returns the bound port.
  49. //
  50. static int bind(ServerSocketChannel ssc) throws IOException {
  51. InetAddress lh = InetAddress.getLocalHost();
  52. Random r = new Random();
  53. for (;;) {
  54. int p = r.nextInt((1 << 16) - 1024) + 1024;
  55. InetSocketAddress isa = new InetSocketAddress(lh, p);
  56. try {
  57. ssc.socket().bind(isa);
  58. } catch (IOException x) {
  59. continue;
  60. }
  61. return p;
  62. }
  63. }
  64. // A more convenient form of bind(ServerSocketChannel) that returns a full
  65. // socket address.
  66. //
  67. static InetSocketAddress bindToRandomPort(ServerSocketChannel ssc)
  68. throws IOException
  69. {
  70. int p = bind(ssc);
  71. return new InetSocketAddress(InetAddress.getLocalHost(), p);
  72. }
  73. private static String osName = System.getProperty("os.name");
  74. // Examines os.name property to determine if running on 95/98/ME.
  75. //
  76. // Returns true if running on windows95/98/ME.
  77. //
  78. static boolean onME() {
  79. if (osName.startsWith("Windows")) {
  80. if (osName.indexOf("9") > 0)
  81. return true;
  82. if (osName.indexOf("M") > 0)
  83. return true;
  84. }
  85. return false;
  86. }
  87. static boolean onSolaris() {
  88. return osName.startsWith("SunOS");
  89. }
  90. static boolean onWindows() {
  91. return osName.startsWith("Windows");
  92. }
  93. }