java.net.Socket.setSocketImplFactory()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(2.9k)|赞(0)|评价(0)|浏览(198)

本文整理了Java中java.net.Socket.setSocketImplFactory()方法的一些代码示例,展示了Socket.setSocketImplFactory()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Socket.setSocketImplFactory()方法的具体详情如下:
包路径:java.net.Socket
类名称:Socket
方法名:setSocketImplFactory

Socket.setSocketImplFactory介绍

[英]Sets the internal factory for creating socket implementations. This may only be executed once during the lifetime of the application.
[中]

代码示例

代码示例来源:origin: worstcase/gumshoe

public void initializeProbe() throws IOException {
  Socket.setSocketImplFactory(this);
}

代码示例来源:origin: sniffy/sniffy

/**
 * Backups the existing {@link SocketImplFactory} and sets the {@link SnifferSocketImplFactory} as a default
 * @see #uninstall()
 * @throws IOException if failed to install {@link SnifferSocketImplFactory}
 * @since 3.1
 */
public static void install() throws IOException {
  
  try {
    Field factoryField = Socket.class.getDeclaredField("factory");
    factoryField.setAccessible(true);
    SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null);
    if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) {
      factoryField.set(null, null);
      Socket.setSocketImplFactory(new SnifferSocketImplFactory());
      previousSocketImplFactory = currentSocketImplFactory;
    }
  } catch (IllegalAccessException e) {
    throw new IOException("Failed to initialize SnifferSocketImplFactory", e);
  } catch (NoSuchFieldException e) {
    throw new IOException("Failed to initialize SnifferSocketImplFactory", e);
  }
}

代码示例来源:origin: sniffy/sniffy

/**
 * Backups the existing {@link SocketImplFactory} and sets the {@link SnifferSocketImplFactory} as a default
 * @see #uninstall()
 * @throws IOException if failed to install {@link SnifferSocketImplFactory}
 * @since 3.1
 */
public static void install() throws IOException {
  
  try {
    Field factoryField = Socket.class.getDeclaredField("factory");
    factoryField.setAccessible(true);
    SocketImplFactory currentSocketImplFactory = (SocketImplFactory) factoryField.get(null);
    if (null == currentSocketImplFactory || !SnifferSocketImplFactory.class.equals(currentSocketImplFactory.getClass())) {
      factoryField.set(null, null);
      Socket.setSocketImplFactory(new SnifferSocketImplFactory());
      previousSocketImplFactory = currentSocketImplFactory;
    }
  } catch (IllegalAccessException e) {
    throw new IOException("Failed to initialize SnifferSocketImplFactory", e);
  } catch (NoSuchFieldException e) {
    throw new IOException("Failed to initialize SnifferSocketImplFactory", e);
  }
}

代码示例来源:origin: sniffy/sniffy

@Test
public void testExistingFactory() throws IOException {
  TestSocketImplFactory testSocketImplFactory = new TestSocketImplFactory();
  SnifferSocketImplFactory.uninstall();
  Socket.setSocketImplFactory(testSocketImplFactory);
  SnifferSocketImplFactory.install();
  performSocketOperation();
  assertEquals(1, testSocketImplFactory.invocationCounter.intValue());
  SnifferSocketImplFactory.uninstall();
  performSocketOperation();
  assertEquals(2, testSocketImplFactory.invocationCounter.intValue());
  SnifferSocketImplFactory.install();
}

相关文章