예제 #1
0
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
예제 #2
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();
  }
 /*
  * @see java.nio.channels.Selector#wakeup()
  */
 public Selector wakeup() {
   try {
     sink.write(ByteBuffer.allocate(MOCK_WRITEBUF_SIZE));
   } catch (IOException e) {
     // do nothing
   }
   return this;
 }
예제 #4
0
  public void send() {
    int nbytes = 0;
    ByteBuffer dummy = ByteBuffer.allocate(1);

    while (true) {
      try {
        Thread.interrupted();
        nbytes = w.write(dummy);
      } catch (IOException e) {
        throw new ZError.IOException(e);
      }
      if (nbytes == 0) {
        continue;
      }
      assert (nbytes == 1);
      wcursor.incrementAndGet();
      break;
    }
  }
 public int write(final ByteBuffer src) throws IOException {
   return wrapped.write(src);
 }
 public long write(final ByteBuffer[] srcs, final int offset, final int length)
     throws IOException {
   return wrapped.write(srcs, offset, length);
 }
 public long write(final ByteBuffer[] srcs) throws IOException {
   return wrapped.write(srcs);
 }