PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/MRI-J/jdk/test/java/nio/channels/TestUtil.java

https://github.com/GregBowyer/ManagedRuntimeInitiative
Java | 99 lines | 44 code | 12 blank | 43 comment | 4 complexity | 03223de86ca347a095d932f403fea90a MD5 | raw file
  1. /*
  2. * Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20. * CA 95054 USA or visit www.sun.com if you need additional information or
  21. * have any 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. private TestUtil() { }
  33. // Repeatedly try random ports until we bind to one. You might be tempted
  34. // to do this:
  35. //
  36. // ServerSocketChannel ssc = ServerSocketChannel.open();
  37. // ssc.socket().bind(new InetSocketAddress(0));
  38. // SocketAddress sa = ssc.socket().getLocalSocketAddress();
  39. //
  40. // but unfortunately it doesn't work on NT 4.0.
  41. //
  42. // Returns the bound port.
  43. //
  44. static int bind(ServerSocketChannel ssc) throws IOException {
  45. InetAddress lh = InetAddress.getLocalHost();
  46. Random r = new Random();
  47. for (;;) {
  48. int p = r.nextInt((1 << 16) - 1024) + 1024;
  49. InetSocketAddress isa = new InetSocketAddress(lh, p);
  50. try {
  51. ssc.socket().bind(isa);
  52. } catch (IOException x) {
  53. continue;
  54. }
  55. return p;
  56. }
  57. }
  58. // A more convenient form of bind(ServerSocketChannel) that returns a full
  59. // socket address.
  60. //
  61. static InetSocketAddress bindToRandomPort(ServerSocketChannel ssc)
  62. throws IOException
  63. {
  64. int p = bind(ssc);
  65. return new InetSocketAddress(InetAddress.getLocalHost(), p);
  66. }
  67. private static String osName = System.getProperty("os.name");
  68. // Examines os.name property to determine if running on 95/98/ME.
  69. //
  70. // Returns true if running on windows95/98/ME.
  71. //
  72. static boolean onME() {
  73. if (osName.startsWith("Windows")) {
  74. if (osName.indexOf("9") > 0)
  75. return true;
  76. if (osName.indexOf("M") > 0)
  77. return true;
  78. }
  79. return false;
  80. }
  81. static boolean onSolaris() {
  82. return osName.startsWith("SunOS");
  83. }
  84. static boolean onWindows() {
  85. return osName.startsWith("Windows");
  86. }
  87. }