Package java.nio.channels

Examples of java.nio.channels.ByteChannel


        session.setSelectionKey(ch.register(selector, SelectionKey.OP_READ, session));
    }

    @Override
    protected void destroy(NioSession session) throws Exception {
        ByteChannel ch = session.getChannel();
        SelectionKey key = session.getSelectionKey();
        if (key != null) {
            key.cancel();
        }
        ch.close();
    }
View Full Code Here


        super(runtime, runtime.getModule("FFI").getClass(CLASS_NAME));
        MakeOpenFile();
        ModeFlags modes = newModeFlags(runtime, ModeFlags.RDWR);
        int fileno = RubyNumeric.fix2int(fd);
        FileStat stat = runtime.getPosix().fstat(fileno);
        ByteChannel channel;

        if (stat.isSocket()) {
            channel = new jnr.enxio.channels.NativeSocketChannel(fileno);
        } else if (stat.isBlockDev() || stat.isCharDev()) {
            channel = new jnr.enxio.channels.NativeDeviceChannel(fileno);
View Full Code Here

      server_port.intValue(), null, null );
    assertNotNull( server_vmid );

    double total_bps = 0;
    for( int i = 0; i < RUNS; i++ ) {
      ByteChannel channel = client_instance.createChannel( server_vmid, null );

      ByteBuffer write_buffer = ByteBuffer.allocate( 1 << 18 ); // 256KB
      long start = System.currentTimeMillis();
      int pos = 0;
      while( pos < data.length ) {
        write_buffer.put( data, pos,
          Math.min( write_buffer.remaining(), data.length - pos ) );
        pos += write_buffer.position();
        write_buffer.flip();

        channel.write( write_buffer );
        write_buffer.clear();
      }
      channel.close();
      long time = System.currentTimeMillis() - start;
      double bps = ( ( data.length * 2 ) / ( double ) time ) * 1000.0;
      total_bps += bps;
      System.out.println( "Client done writing in " + time + " ms: " +
        NumberFormat.getNumberInstance().format( bps ) + " Bps" );
View Full Code Here

      service.execute( new Runnable() {
        @Override
        public void run() {
          try {
            System.out.println( "Starting thread " + thread_number );
            ByteChannel channel =
              client_instance.createChannel( server_vmid, null );

            ByteBuffer write_buffer = ByteBuffer.allocate( 1 << 18 ); // 256KB
            long start = System.currentTimeMillis();
            int pos = 0;
            while( pos < data.length ) {
              write_buffer.put( data, pos,
                Math.min( write_buffer.remaining(), data.length - pos ) );
              pos += write_buffer.position();
              write_buffer.flip();

              channel.write( write_buffer );
              write_buffer.clear();
            }
            channel.close();
            long time = System.currentTimeMillis() - start;
            double bps = ( ( data.length * 2 ) / ( double ) time ) * 1000.0;
            System.out.println( "Client done writing in " + time + " ms: " +
              NumberFormat.getNumberInstance().format( bps ) + " Bps" );

View Full Code Here


    //////////////////////////////////////////////////////////////////////////////////
    // Data channel

    ByteChannel channel = client_instance.createChannel( server_vmid, null );

    // CLIENT - channel opened
    pair = getNextMessage( client_listener, 2000 );
    assertNotNull( pair );
    assertEquals( CallType.VIRTUAL_CHANNEL_OPENED, pair.getOne() );
    assertEquals( client_instance.getLocalVMID(), pair.getTwo().get( 0 ) );
    assertEquals( server_instance.getLocalVMID(), pair.getTwo().get( 1 ) );
    short client_channel_id = ( ( Short ) pair.getTwo().get( 2 ) ).shortValue();

    // SERVER - channel opened
    pair = getNextMessage( server_listener, 2000 );
    assertNotNull( pair );
    assertEquals( CallType.VIRTUAL_CHANNEL_OPENED, pair.getOne() );
    assertEquals( server_instance.getLocalVMID(), pair.getTwo().get( 0 ) );
    assertEquals( client_instance.getLocalVMID(), pair.getTwo().get( 1 ) );
    assertEquals( Short.valueOf( client_channel_id ), pair.getTwo().get( 2 ) );

    ByteBuffer buffer = ByteBuffer.allocate( 1024 );
    byte[] to_write = new byte[ buffer.remaining() ];
    new Random().nextBytes( to_write );
    buffer.put( to_write );
    buffer.flip();
    channel.write( buffer );

    // CLIENT - channel data sent
    pair = getNextMessage( client_listener, 2000 );
    assertNotNull( pair );
    assertEquals( CallType.VIRTUAL_CHANNEL_DATA_SENT, pair.getOne() );
    assertEquals( client_instance.getLocalVMID(), pair.getTwo().get( 0 ) );
    assertEquals( server_instance.getLocalVMID(), pair.getTwo().get( 1 ) );
    assertEquals( Short.valueOf( client_channel_id ), pair.getTwo().get( 2 ) );
    assertEquals( Integer.valueOf( to_write.length ), pair.getTwo().get( 3 ) );

    // SERVER - channel data received
    pair = getNextMessage( server_listener, 2000 );
    assertNotNull( pair );
    assertEquals( CallType.VIRTUAL_CHANNEL_DATA_RECEIVED, pair.getOne() );
    assertEquals( server_instance.getLocalVMID(), pair.getTwo().get( 0 ) );
    assertEquals( client_instance.getLocalVMID(), pair.getTwo().get( 1 ) );
    assertEquals( Short.valueOf( client_channel_id ), pair.getTwo().get( 2 ) );
    assertEquals( Integer.valueOf( to_write.length ), pair.getTwo().get( 3 ) );

    channel.close();

    // CLIENT - channel close
    pair = getNextMessage( client_listener, 2000 );
    assertNotNull( pair );
    assertEquals( CallType.VIRTUAL_CHANNEL_CLOSED, pair.getOne() );
View Full Code Here

    client = Intrepid.create( new IntrepidSetup().vmidHint( "client" ) );

    VMID server_vmid = client.connect( InetAddress.getLocalHost(),
      server.getServerPort().intValue(), null, null );

    ByteChannel client_channel = client.createChannel( server_vmid, "testing" );

    // Client write
    ByteBuffer buffer = ByteBuffer.allocate( 10 );
    buffer.put( ( byte ) 0xCA );
    buffer.put( ( byte ) 0xFE );
    buffer.flip();

    int written = client_channel.write( buffer );
    assertEquals( 2, written );
    buffer.clear();

    buffer.put( ( byte ) 0xBA );
    buffer.put( ( byte ) 0xBE );
    buffer.flip();

    written = client_channel.write( buffer );
    assertEquals( 2, written );
    buffer.clear();

    // Server read
    Triple<ByteChannel,VMID,Serializable> channel_info =
      acceptor.queue.poll( 2, TimeUnit.SECONDS );
    assertNotNull( channel_info );

    assertEquals( "testing", channel_info.getThree() );
    assertEquals( client.getLocalVMID(), channel_info.getTwo() );

    ByteChannel server_channel = channel_info.getOne();

    buffer.limit( 4 );
    int read = 0;
    while( read < 4 ) {
      int sweep_read = server_channel.read( buffer );
      assertTrue( String.valueOf( read ), sweep_read > 1 );
      read += sweep_read;
    }
    buffer.flip();

    assertEquals( ( byte ) 0xCA, buffer.get() );
    assertEquals( ( byte ) 0xFE, buffer.get() );
    assertEquals( ( byte ) 0xBA, buffer.get() );
    assertEquals( ( byte ) 0xBE, buffer.get() );

    buffer.clear();

    // Server write
    buffer.put( ( byte ) 0xDE );
    buffer.put( ( byte ) 0xAD );
    buffer.put( ( byte ) 0xBE );
    buffer.put( ( byte ) 0xEF );
    buffer.flip();

    written = server_channel.write( buffer );
    buffer.clear();
    assertEquals( 4, written );

    // Server close
    server_channel.close();

    // Client read
    buffer.limit( 1 );
    read = client_channel.read( buffer );
    assertEquals( 1, read );
View Full Code Here


    client.connect( InetAddress.getLocalHost(),
      server.getServerPort().intValue(), null, null );

    ByteChannel client_channel = client.createChannel( server_vmid, "testing" );


    // Client write
    ByteBuffer buffer = ByteBuffer.allocate( 1 << 20 ); // 1M
    Random rand = new Random();
    while( buffer.hasRemaining() ) {
      buffer.putLong( rand.nextLong() );
    }
    buffer.flip();
    client_channel.write( buffer );

    client_channel.close();

    ThreadKit.sleep( 1000 );

    client.disconnect( server_vmid );
View Full Code Here

    client = Intrepid.create( new IntrepidSetup().vmidHint( "client" ) );

    VMID server_vmid = client.connect( InetAddress.getLocalHost(),
      server.getServerPort().intValue(), null, null );

    ByteChannel client_channel = client.createChannel( server_vmid, "testing" );

    // NOTE: this would block forever before svn change 367
    ByteBuffer read_buffer = ByteBuffer.allocate( 4 );
    while( read_buffer.hasRemaining() ) {
      client_channel.read( read_buffer );
    }

    read_buffer.flip();

    assertEquals( ( byte ) 0xDE, read_buffer.get() );
View Full Code Here

      server_port.intValue(), null, null );
    assertNotNull( server_vmid );

    double total_bps = 0;
    for( int i = 0; i < RUNS; i++ ) {
      ByteChannel channel = null;
      InputStream in = null;
      OutputStream out = null;
      BufferedOutputStream bout = null;
     
View Full Code Here

      else slot.set( object );

      return;
    }

    ByteChannel channel = null;
    OutputStream out = null;
    ObjectOutputStream oout = null;
    try {
      channel = instance.createChannel( destination, id );
      out = new BufferedOutputStream( Channels.newOutputStream( channel ),
View Full Code Here

TOP

Related Classes of java.nio.channels.ByteChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.