Package java.nio

Examples of java.nio.ByteBuffer


    return updateThread;
  }

  private void broadcast (int udpPort, DatagramSocket socket) throws IOException {
    int classID = kryo.getRegisteredClass(DiscoverHost.class).getID();
    ByteBuffer dataBuffer = ByteBuffer.allocate(4);
    IntSerializer.put(dataBuffer, classID, true);
    dataBuffer.flip();
    byte[] data = new byte[dataBuffer.limit()];
    dataBuffer.get(data);
    for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
      for (InetAddress address : Collections.list(iface.getInetAddresses())) {
        if (!address.isSiteLocalAddress()) continue;
        // Java 1.5 doesn't support getting the subnet mask, so try the two most common.
        byte[] ip = address.getAddress();
View Full Code Here


       
        if ( expected_length == -1 && message.size() >= 4 ){
         
          byte[]  bytes = message.toByteArray();
         
          ByteBuffer  bb = ByteBuffer.wrap( bytes );
         
          expected_length = bb.getInt();
         
          message = new ByteArrayOutputStream();
         
          if ( bytes.length > 4 ){
           
View Full Code Here

                  if (Logger.isEnabled())
                    Logger.log(new LogEvent(LOGID, "Incoming connection from ["
                        + connection + "] successfully routed to NAT CHECKER"));
 
                  try{
                    ByteBuffer  msg = getMessage();
 
                    Transport transport = connection.getTransport();
 
                    long  start = SystemTime.getCurrentTime();
 
                    while( msg.hasRemaining()){
 
                      transport.write( new ByteBuffer[]{ msg }, 0, 1 );
 
                      if ( msg.hasRemaining()){
 
                        long now = SystemTime.getCurrentTime();
 
                        if ( now < start ){
 
View Full Code Here

      map.put( "check", check );

      byte[]  map_bytes = BEncoder.encode( map );

      ByteBuffer msg = ByteBuffer.allocate( 4 + map_bytes.length );

      msg.putInt( map_bytes.length );
      msg.put( map_bytes );

      msg.flip();
     
      return( msg );
    }
View Full Code Here

      connection.connect();

    }else{
     
      try{
        ByteBuffer  buffer = ByteBuffer.allocate( 32*1024 );
           
        sts_engine.getKeys( buffer );
           
        buffer.flip();
       
        sent_keys = true;
       
        connection.connect( buffer );
       
View Full Code Here

  {
    try{
      boolean  forward        = false;
      boolean  crypto_completed  = false;
     
      ByteBuffer  out_buffer = null;

      synchronized( this ){
   
        if ( crypto_complete.isReleasedForever()){
         
          forward  = true;
         
        }else{
         
            // basic sts flow:
            //   a -> puba -> b
            //   a <- pubb <- b
            //   a -> auta -> b
            //   a <- autb <- b
            //   a -> data -> b
         
            // optimised
         
            //  a -> puba      -> b
            //  a <- pubb + auta <- b
            //  a -> autb + data -> b
         
            // therefore can be one or two messages in the payload
            //     1 crypto
            //    2 crypto (pub + auth)
            //    crypto + data
         
            // initial a ->puba -> is done on first data send so data is ready for phase 3
         
          ByteBuffer  in_buffer = ByteBuffer.wrap( message.toByteArray());
         
          message.returnToPool();
           
            // piggyback pub key send
         
          if ( !sent_keys ){
           
              // we've received
              //    a -> puba -> b
              // reply with
              //    a <- puba + auta <- b
           
            out_buffer = ByteBuffer.allocate( 64*1024 );

              // write our keys
           
            sts_engine.getKeys( out_buffer );
         
            sent_keys  = true;
           
              // read their keys
           
            sts_engine.putKeys( in_buffer );
         
              // write our auth
           
            sts_engine.getAuth( out_buffer );
           
            sent_auth   = true;
           
          }else if ( !sent_auth ){
           
            out_buffer = ByteBuffer.allocate( 64*1024 );

            // we've received
            //    a <- puba + auta <- b
            // reply with
            //    a -> autb + data -> b

              // read their keys
           
            sts_engine.putKeys( in_buffer );

              // write our auth
           
            sts_engine.getAuth( out_buffer );

            sent_auth = true;
         
              // read their auth
           
            sts_engine.putAuth( in_buffer );

              // check we wanna talk to this person
           
            byte[]  rem_key = sts_engine.getRemotePublicKey();
           
            if ( !key_locator.accept(
                SESTSConnectionImpl.this,
                new SEPublicKeyImpl( my_public_key.getType(), rem_key ))){
             
              throw( new MessageException( "remote public key not accepted" ));
            }
             
            setupBlockCrypto();
           
            if ( pending_message != null ){
             
              byte[]  pending_bytes = pending_message.toByteArray();
             
              int  pending_size = pending_bytes.length;
             
              if ( outgoing_cipher != null ){
               
                pending_size =  (( pending_size + AES_KEY_SIZE_BYTES -1 )/AES_KEY_SIZE_BYTES)*AES_KEY_SIZE_BYTES;
               
                if ( pending_size == 0 ){
                 
                  pending_size = AES_KEY_SIZE_BYTES;
                }
              }
             
              if ( out_buffer.remaining() >= pending_size ){
               
                if ( outgoing_cipher != null ){
                 
                 
                  out_buffer.put( outgoing_cipher.doFinal( pending_bytes ));
                 
                }else{
               
                  out_buffer.put( pending_bytes );
                }
               
                  // don't deallocate the pending message, the original caller does this
                               
                pending_message  = null;
              }
            }
           
            crypto_completed  = true;
           
          }else{
              // we've received
              //    a -> autb + data -> b
           
              // read their auth
           
            sts_engine.putAuth( in_buffer );

              // check we wanna talk to this person
           
            byte[]  rem_key = sts_engine.getRemotePublicKey();
           
            if ( !key_locator.accept(
                SESTSConnectionImpl.this,
                new SEPublicKeyImpl( my_public_key.getType(), rem_key ))){
             
                // this is just here to prevent unwanted spew  during closedown process
             
              connection.closing();
             
              throw( new MessageException( "remote public key not accepted" ));
            }
           
            setupBlockCrypto();

            crypto_completed  = true;
           
              // pick up any remaining data for delivery
           
            if ( in_buffer.hasRemaining()){
             
              message = new PooledByteBufferImpl( new DirectByteBuffer( in_buffer.slice()));
             
              forward  = true;
            }
          }
        }
View Full Code Here

      InputStream stream;

      try {
        byte[] bytes;
        ByteBuffer byteBuffer= encoder.encode(CharBuffer.wrap(document.get()));
        if (byteBuffer.hasArray())
          bytes= byteBuffer.array();
        else {
          bytes= new byte[byteBuffer.limit()];
          byteBuffer.get(bytes);
        }
        stream= new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
      } catch (CharacterCodingException ex) {
        Assert.isTrue(ex instanceof UnmappableCharacterException);
        String message= "Charset mapping failed.";
        IStatus s= new Status(IStatus.ERROR, Plugin.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
        throw new CoreException(s);
View Full Code Here

      private ByteBuffer buffer = ByteBuffer.allocate(1 << 16);
     
      @Override
      public void writeDirect(byte[] bytes, int offset, int length) throws TeiidComponentException {
        if (getLength() + length > buffer.capacity()) {
          ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2 + length);
          buffer.position(0);
          newBuffer.put(buffer);
          buffer = newBuffer;
        }
        buffer.position((int)getLength());
        buffer.put(bytes, offset, length);
      }
View Full Code Here

                public void run() {
                    Selector sel=null;
                    SocketChannel tmp;
                    Set ready_keys;
                    SelectionKey key;
                    ByteBuffer transfer_buf=ByteBuffer.allocate(BUFSIZE);

                    try {
                        sel=Selector.open();
                        in_channel.configureBlocking(false);
                        out_channel.configureBlocking(false);
View Full Code Here

    public synchronized static ByteBuffer allocateDirectView(ByteBuffer byteBuffer, final int size) {
        if(byteBuffer == null || ((byteBuffer.capacity() - byteBuffer.limit()) < size)) {
            byteBuffer = ByteBuffer.allocateDirect(size);
        }
        byteBuffer.limit(byteBuffer.position() + size);
        ByteBuffer view = byteBuffer.slice();
        byteBuffer.position(byteBuffer.limit());
        return view;
    }
View Full Code Here

TOP

Related Classes of java.nio.ByteBuffer

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.