Пример #1
0
  private static void scScatter() throws Exception {
    Pipe p = Pipe.open();
    Pipe.SinkChannel sink = p.sink();
    Pipe.SourceChannel source = p.source();
    sink.configureBlocking(false);

    ByteBuffer outgoingdata = ByteBuffer.allocateDirect(30);
    byte[] someBytes = new byte[30];
    generator.nextBytes(someBytes);
    outgoingdata.put(someBytes);
    outgoingdata.flip();

    int totalWritten = 0;
    while (totalWritten < 30) {
      int written = sink.write(outgoingdata);
      if (written < 0) throw new Exception("Write failed");
      totalWritten += written;
    }

    ByteBuffer[] bufs = new ByteBuffer[3];
    for (int i = 0; i < 3; i++) bufs[i] = ByteBuffer.allocateDirect(10);
    long numBytesRead = source.read(bufs);
    if (numBytesRead < 30) throw new Exception("Pipe test failed");
    sink.close();
    source.close();
  }
Пример #2
0
  public static void main(String[] args) throws Exception {

    File blah = File.createTempFile("blah", null);
    blah.deleteOnExit();
    ByteBuffer[] dstBuffers = new ByteBuffer[10];
    for (int i = 0; i < 10; i++) {
      dstBuffers[i] = ByteBuffer.allocateDirect(10);
      dstBuffers[i].position(10);
    }
    FileInputStream fis = new FileInputStream(blah);
    FileChannel fc = fis.getChannel();

    // No space left in buffers, this should return 0
    long bytesRead = fc.read(dstBuffers);
    if (bytesRead != 0) throw new RuntimeException("Nonzero return from read");

    fc.close();
    fis.close();
  }
Пример #3
0
  private static WAVData readFromStream(AudioInputStream aIn)
      throws UnsupportedAudioFileException, IOException {
    ReadableByteChannel aChannel = Channels.newChannel(aIn);
    AudioFormat fmt = aIn.getFormat();
    int numChannels = fmt.getChannels();
    int bits = fmt.getSampleSizeInBits();
    int format = AL_FORMAT_MONO8;

    if ((bits == 8) && (numChannels == 1)) {
      format = AL_FORMAT_MONO8;
    } else if ((bits == 16) && (numChannels == 1)) {
      format = AL_FORMAT_MONO16;
    } else if ((bits == 8) && (numChannels == 2)) {
      format = AL_FORMAT_STEREO8;
    } else if ((bits == 16) && (numChannels == 2)) {
      format = AL_FORMAT_STEREO16;
    }

    int freq = Math.round(fmt.getSampleRate());
    int size = aIn.available();
    ByteBuffer buffer = ByteBuffer.allocateDirect(size);
    while (buffer.remaining() > 0) {
      aChannel.read(buffer);
    }
    buffer.rewind();

    // Must byte swap on big endian platforms
    // Thanks to swpalmer on javagaming.org forums for hint at fix
    if ((bits == 16) && (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)) {
      int len = buffer.remaining();
      for (int i = 0; i < len; i += 2) {
        byte a = buffer.get(i);
        byte b = buffer.get(i + 1);
        buffer.put(i, b);
        buffer.put(i + 1, a);
      }
    }

    WAVData result = new WAVData(buffer, format, size, freq, false);
    aIn.close();

    return result;
  }
Пример #4
0
 static class ClientInfo {
   ByteBuffer inBuf = ByteBuffer.allocateDirect(512);
   ByteBuffer outBuf = ByteBuffer.allocateDirect(512);
   boolean outputPending = false;
   SocketChannel channel;
 }
Пример #5
0
public class Refused {

  static ByteBuffer outBuf = ByteBuffer.allocateDirect(100);
  static ByteBuffer inBuf = ByteBuffer.allocateDirect(100);
  static DatagramChannel client;
  static DatagramChannel server;
  static InetSocketAddress isa;

  public static void main(String[] args) throws Exception {
    outBuf.put("Blah Blah".getBytes());
    outBuf.flip();
    test1();

    // This test has been disabled because there are many circumstances
    // under which no ICMP port unreachable packets are received
    // See http://java.sun.com/j2se/1.4/networking-relnotes.html
    if ((args.length > 0) && (args[0].equals("test2"))) {
      outBuf.rewind();
      test2();
    }
  }

  public static void setup() throws Exception {
    client = DatagramChannel.open();
    server = DatagramChannel.open();

    client.socket().bind((SocketAddress) null);
    server.socket().bind((SocketAddress) null);

    client.configureBlocking(false);
    server.configureBlocking(false);

    InetAddress address = InetAddress.getLocalHost();
    int port = client.socket().getLocalPort();
    isa = new InetSocketAddress(address, port);
  }

  // Since this is not connected no PortUnreachableException should be thrown
  public static void test1() throws Exception {
    setup();

    server.send(outBuf, isa);
    server.receive(inBuf);

    client.close();

    outBuf.rewind();
    server.send(outBuf, isa);
    server.receive(inBuf);

    server.close();
  }

  // Test the connected case to see if PUE is thrown
  public static void test2() throws Exception {

    setup();
    server.configureBlocking(true);
    server.connect(isa);
    server.configureBlocking(false);
    outBuf.rewind();
    server.write(outBuf);
    server.receive(inBuf);

    client.close();
    Thread.sleep(2000);
    outBuf.rewind();

    try {
      server.write(outBuf);
      Thread.sleep(2000);
      inBuf.clear();
      server.read(inBuf);
      if (onSolarisOrLinux()) throw new Exception("Expected PUE not thrown");
    } catch (PortUnreachableException pue) {
      System.err.println("received PUE");
    }
    server.close();
  }

  static boolean onSolarisOrLinux() {
    String osName = System.getProperty("os.name");
    return osName.startsWith("SunOS") || osName.startsWith("Linux");
  }
}