Package java.nio

Examples of java.nio.ByteBuffer


    boolean shrink_remaining_buffers = false;
    int start_buff = reading_length_mode ? 1 : 0;
    boolean marked = false;   
   
    for( int i = start_buff; i < 2; i++ ) {  //set buffer limits according to bytes allowed
      ByteBuffer bb = decode_array[ i ];
     
      if( bb == null ) {
        Debug.out( "preReadProcess:: bb["+i+"] == null, decoder destroyed=" +destroyed );
      }
     
      if( shrink_remaining_buffers ) {
        bb.limit( 0 )//ensure no read into this next buffer is possible
      }
      else {
        int remaining = bb.remaining();
       
        if( remaining < 1 continue//skip full buffer

        if( !marked ) {
          pre_read_start_buffer = i;
          pre_read_start_position = bb.position();
          marked = true;
        }

        if( remaining > allowed ) {  //read only part of this buffer
          bb.limit( bb.position() + allowed )//limit current buffer
          bytes_available += bb.remaining();
          shrink_remaining_buffers = true//shrink any tail buffers
        }
        else //full buffer is allowed to be read
          bytes_available += remaining;
          allowed -= remaining;  //count this buffer toward allowed and move on to the next
View Full Code Here


   
    long  total_written = 0;
   
    if ( write_buffer_pending_db != null ){
     
      ByteBuffer  write_buffer_pending = write_buffer_pending_db.getBuffer( DirectByteBuffer.SS_NET );
     
      int  max_writable = 0;
     
      for (int i=array_offset;i<array_offset+length;i++){
       
        ByteBuffer  source_buffer = buffers[i];
       
        int  position   = source_buffer.position();
        int  limit    = source_buffer.limit();
       
        int  size = limit - position;
       
        max_writable  += size;
      }
     
      int  pending_position   = write_buffer_pending.position();
      int pending_limit    = write_buffer_pending.limit();
     
      int  pending_writable = pending_limit - pending_position;
     
      if ( pending_writable > max_writable ){
               
        pending_writable = max_writable;
       
        write_buffer_pending.limit( pending_position + pending_writable );
      }
     
      int  written = transport.write( write_buffer_pending, false );
     
      write_buffer_pending.limit( pending_limit );
     
      if ( written > 0 ){
       
        total_written = written;
       
        if ( write_buffer_pending.remaining() == 0 ){
         
          write_buffer_pending_db.returnToPool();
         
          write_buffer_pending_db  = null;
        }
       
          // skip "written" bytes in the source
       
        int skip = written;
       
        for (int i=array_offset;i<array_offset+length;i++){
         
          ByteBuffer  source_buffer = buffers[i];
         
          int  position   = source_buffer.position();
          int  limit    = source_buffer.limit();
         
          int  size = limit - position;
         
          if ( size <= skip ){
           
            source_buffer.position( limit );
           
            skip  -= size;
           
          }else{
           
            source_buffer.position( position + skip );
           
            skip  = 0;
           
            break;
          }
        }
       
        if ( skip != 0 ){
         
          throw( new IOException( "skip inconsistent - " + skip ));
        }
      }
     
        // if write came up short or we've filled the source buffer then we can't do
        // any more
     
      if ( total_written < pending_writable || total_written == max_writable ){
       
        return( total_written );
      }
    }
   
      // problem - we must only crypt stuff once and when crypted it *has*
      // to be sent (else the stream will get out of sync).
      // so we have to turn this into single buffer operations
   
    for (int i=array_offset;i<array_offset+length;i++){
     
      ByteBuffer  source_buffer = buffers[i];
     
      int  position   = source_buffer.position();
      int  limit    = source_buffer.limit();
     
      int  size = limit - position;
     
      if ( size == 0 ){
       
        continue;
      }
     
      DirectByteBuffer  target_buffer_db = DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_NET_CRYPT,  size );
     
      try{
        ByteBuffer  target_buffer = target_buffer_db.getBuffer( DirectByteBuffer.SS_NET );
     
        cryptoOut( source_buffer, target_buffer );
       
        target_buffer.position( 0 );
       
        boolean  partial_write = false;
       
        for (int j=i+1;j<array_offset+length;j++){

          if ( buffers[j].hasRemaining()){
           
            partial_write = true;
          }
        }
       
        int  written = transport.write( target_buffer, partial_write );
       
        total_written += written;
       
        source_buffer.position( position + written );
       
        if ( written < size ){
         
          write_buffer_pending_db  = target_buffer_db;
         
          target_buffer_db  = null;
         
          if ( written == 0 ){
           
              // we gotta pretend at least 1 byte was written to
              // guarantee that the caller writes the rest
                     
            write_buffer_pending_byte = ByteBuffer.wrap(new byte[]{target_buffer.get()});
                         
            source_buffer.get();
           
            total_written++;
          }
View Full Code Here

    ByteBuffer[]    copy  = new ByteBuffer[buffers.length];
   
    try{
      for (int i=array_offset;i<array_offset+length;i++){
       
        ByteBuffer  buffer = buffers[i];
       
        int  size = buffer.remaining();
       
        if ( size > 0 ){
         
          copy_db[i= DirectByteBufferPool.getBuffer( DirectByteBuffer.AL_NET_CRYPT, size );
         
          copy[i= copy_db[i].getBuffer( DirectByteBuffer.SS_NET );
        }else{
         
          copy[i] = ByteBuffer.allocate(0);
        }
      }
     
      total_read += transport.read( copy, array_offset, length );
     
      for (int i=array_offset;i<array_offset+length;i++){
 
        ByteBuffer  source_buffer  = copy[i];

        if ( source_buffer != null ){
         
          ByteBuffer  target_buffer   = buffers[i];
         
          int  source_position  = source_buffer.position();
               
          if ( source_position > 0 ){
           
View Full Code Here

    synchronized( this ){
     
      total_packets_received++;
    }
   
    ByteBuffer  initial_buffer = ByteBuffer.wrap( initial_data );
   
    initial_buffer.limit( initial_data_length );
   
    if ( !crypto_done ){
     
        // first packet - connection setup and crypto handshake
     
        // derive the sequence number in the normal way so that if a retranmission occurs
        // after crypto has been setup then it'll get handled correctly as a dupliate packet
        // below
     
      initial_buffer.position( 4 );
     
      Integer  pseudo_seq = new Integer( initial_buffer.getInt());
     
      initial_buffer.position( 0 );
     
      if ( !receive_done_sequences.contains( pseudo_seq )){
       
        receive_done_sequences.addFirst( pseudo_seq );
     
        if ( receive_done_sequences.size() > RECEIVE_DONE_SEQ_MAX ){
         
          receive_done_sequences.removeLast();
        }
      }
     
      if ( outgoing ){
       
          // a reply received by the initiator acknowledges that the initial message sent has
          // been received
       
        remoteLastInSequence( -1 );
      }
     
      receiveCrypto( initial_buffer );
     
    }else{
     
        // pull out the alternative last-in-order seq
     
      byte[]  alt_seq = new byte[4];
     
      alt_seq[0= initial_data[0];
      alt_seq[1= initial_data[1];
      alt_seq[2= initial_data[8];
      alt_seq[3= initial_data[9];
     
      int  alt = bytesToInt( alt_seq, 0 );
           
      boolean  write_select = remoteLastInSequence( alt );
     
      boolean  lazy_ack_found  = false;

      try{
        initial_buffer.getInt()// seq1
       
        Integer  seq2 = new Integer( initial_buffer.getInt());
       
        initial_buffer.getInt()// seq3
         
          // first see if we know about this sequence number already
       
        if ( receive_done_sequences.contains( seq2 )){
         
          if ( manager.trace() ){
            trace( "Duplicate processed packet: " + seq2 );
          }
         
            // if we're gone quiescent and our lazy-ack packet failed to be delivered we end up here with the other end
            // resending their last message. We pick up this delivery failure and resend the packet
         
          UDPPacket  packet_to_send = null;
         
          synchronized( this ){
           
            stats_packets_duplicates++;
            total_packets_duplicates++;
           
            if ( transmit_unack_packets.size() == 1 ){
             
              UDPPacket  packet = (UDPPacket)transmit_unack_packets.get(0);
             
              if ( !packet.isAutoRetransmit()){
               
                if ( total_tick_count - packet.getSendTickCount() >= MIN_RETRANSMIT_TICKS ){
               
                  if ( manager.trace() ){
                    trace( "Retrans non-auto-retrans packet" );
                  }
                 
                  packet_to_send = packet;
                }
              }
            }
          }
         
          if ( packet_to_send != null ){
           
            send( packet_to_send );
          }
         
          return;
        }
       
        if ( !out_seq_generator.isValidAlterativeSequence( alt )){
           
          if ( manager.trace() ){
            trace( "Received invalid alternative sequence " + alt + " - dropping packet" );
          }
         
          return;
        }

        boolean  oop = false;
       
        for (int i=0;i<receive_out_of_order_packets.size();i++){
         
          Object[]  entry = (Object[])receive_out_of_order_packets.get(i);
         
          Integer  oop_seq = (Integer)entry[0];
         
          ByteBuffer  oop_buffer = (ByteBuffer)entry[2];
         
          if ( oop_seq.equals( seq2 )){
           
            synchronized( this ){

              if ( oop_buffer != null ){
           
                stats_packets_duplicates++;
                total_packets_duplicates++;
               
                if ( manager.trace() ){
                  trace( "Duplicate out-of-order packet: " + seq2 );
                }
               
                return;
              }
             
              stats_packets_unique_received++;
              total_packets_unique_received++;
             
              if ( manager.trace() ){
                trace( "Out-of-order packet entry data matched for seq " + seq2 );
              }
             
                // got data matching out-of-order-entry, add it in!
                         
              entry[2] = initial_buffer;
             
              oop = true;
             
              break;
            }
          }
        }
       
        if ( !oop ){
         
            // not a known out-of-order packet. If our oop list is full then all we can do is drop
            // the packet
         
          boolean  added = false;
         
          while ( receive_out_of_order_packets.size() < RECEIVE_OUT_OF_ORDER_PACKETS_MAX ){
                 
            int[]  seq_in = in_seq_generator.getNextSequenceNumber();
               
            if ( seq2.intValue() == seq_in[1] ){
               
              synchronized( this ){
               
                stats_packets_unique_received++;
                total_packets_unique_received++;
              }
             
              if ( receive_out_of_order_packets.size() == 0 ){

                // this is an in-order packet :)

              }else{
               
                if ( manager.trace() ){
                  trace( "Out-of-order packet entry adding for seq " + seq_in[1] );
                }
              }
                           
              receive_out_of_order_packets.add( new Object[]{ seq2, new Integer( seq_in[3]), initial_buffer } );
             
              added = true;
             
              break;
             
            }else{
             
              if ( manager.trace() ){
                trace( "Out-of-order packet: adding spacer for seq " + seq_in[1] );
              }
             
              receive_out_of_order_packets.add( new Object[]{ new Integer( seq_in[1]), new Integer( seq_in[3]), null } );
            }
          }
         
          if ( !added ){
         
              // drop the packet, we have no room to store it
           
            if ( manager.trace() ){
              trace( "Out-of-order packet dropped as too many pending" );
            }
           
            return;
          }
        }
       
        boolean  this_is_oop   = true;
       
          // process any ready packets
       
        Iterator  it = receive_out_of_order_packets.iterator();
       
        while( it.hasNext()){
         
          Object[]  entry = (Object[])it.next();
         
          ByteBuffer  buffer = (ByteBuffer)entry[2];
         
          if ( buffer == null ){
           
            break;
          }
       
          it.remove();
       
          byte[]  data = buffer.array();
         
          if ( buffer == initial_buffer ){
           
            this_is_oop = false;
          }
         
          synchronized( this ){
           
            current_receive_unack_in_sequence_count++;
          }
         
          Integer  seq = (Integer)entry[0];
         
          receive_last_inorder_sequence    = seq.intValue();
          receive_last_inorder_alt_sequence  = ((Integer)entry[1]).intValue();
         
          if ( !receive_done_sequences.contains( seq )){
           
            receive_done_sequences.addFirst( seq );
         
            if ( receive_done_sequences.size() > RECEIVE_DONE_SEQ_MAX ){
             
              receive_done_sequences.removeLast();
            }
          }
         
          header_cipher_in.processBytes( data, 12, 2, data, 12 );
   
          int  header_len = buffer.getShort()&0xffff;
         
          if ( header_len > data.length ){
           
            if ( manager.trace() ){
              trace( "Header length too large" );
            }
           
            return;
          }
         
          header_cipher_in.processBytes( data, 14, header_len-14, data, 14 );
         
          SHA1Hasher  hasher = new SHA1Hasher();
         
          hasher.update( data, 4, 4 );
          hasher.update( data, 12, header_len - 4 - 12 );
                 
          byte[]  hash = hasher.getDigest();
             
          for (int i=0;i<4;i++){
           
            if ( hash[i] != data[header_len-4+i] ){ 
           
              if ( manager.trace() ){
                trace( "hash incorrect" );
              }
             
              return;
            }
          }
         
          byte  version = buffer.get();
         
          if ( version != UDPPacket.PROTOCOL_VERSION ){
           
            // continue, assumption is that version changes are backward compatible
           
            // throw( new IOException( "Invalid protocol version '" + version + "'" ));
          }
         
          byte  flags = buffer.get();
               
          if ( ( flags & UDPPacket.FLAG_LAZY_ACK ) != 0 ){
           
            lazy_ack_found  = true;
          }
         
          int  their_timer_base = (buffer.getShort()&0xffff)*10;
         
          receiveTimerBase( their_timer_base );
         
          byte  command = buffer.get();
         
          if ( command == UDPPacket.COMMAND_DATA ){       
 
            receiveDataCommand( seq.intValue(), buffer, header_len );
             
View Full Code Here

   
    // first packet, cram it all in
   
    byte[]  packet_bytes = new byte[ payload_to_send ];
   
    ByteBuffer packet_buffer = ByteBuffer.wrap( packet_bytes );
   
    for (int i=offset;i<offset+length;i++){
     
      packet_buffer.put( buffers[i] );
    }
     
    UDPPacket packet_to_send = new UDPPacket( lead_connection, new int[]{ -1, -1, -1, -1 }, UDPPacket.COMMAND_CRYPTO, packet_bytes, 0 )
   
    synchronized( this ){
View Full Code Here

      payload_to_send += buffers[i].remaining();
    }
   
    byte[]  header = new byte[256];
   
    ByteBuffer  header_buffer = ByteBuffer.wrap( header );
   
    UDPPacket  packet_to_send;
   
    synchronized( this ){
     
      long  unack_in_sequence_count  = current_receive_unack_in_sequence_count;
     
      int[]  sequence_numbers = writeHeaderStart( header_buffer, UDPPacket.COMMAND_DATA, UDPPacket.FLAG_NONE );
     
      header_buffer.putInt( connection.getID());
     
      int header_size = writeHeaderEnd( header_buffer, false );
                 
      int  mss = connection.getTransport().getMss();
 
        // just in case we have some crazy limit set
     
      if ( mss < MIN_MSS ){
       
        mss = MIN_MSS;
      }
     
      if ( payload_to_send > mss - header_size ){
       
        payload_to_send  = mss - header_size;
      }
     
      if ( payload_to_send < 0 ){
       
        payload_to_send  = 0;
      }
         
      byte[]  packet_bytes = new byte[ header_size + payload_to_send ];
     
      ByteBuffer packet_buffer = ByteBuffer.wrap( packet_bytes );
     
      packet_buffer.put( header, 0, header_size );
     
      int  rem = payload_to_send;
     
      for (int i=offset;i<offset+length;i++){
       
        ByteBuffer  buffer = buffers[i];
       
        int  limit = buffer.limit();
       
        try{
          if ( buffer.remaining() > rem ){
           
            buffer.limit( buffer.position() + rem );
          }
         
          rem -= buffer.remaining();
         
          packet_buffer.put( buffer );
         
        }finally{
         
          buffer.limit( limit );
        }
       
        if ( rem == 0 ){
         
          break;
View Full Code Here

     
      if ( packet_to_send == null ){
     
        byte[]  header_bytes = new byte[256 + (RECEIVE_OUT_OF_ORDER_PACKETS_MAX+1)*4];
       
        ByteBuffer  header = ByteBuffer.wrap( header_bytes );
       
        long  unack_in_sequence_count  = current_receive_unack_in_sequence_count;
 
          // if this is the only packet, contains nothing out-of-sequence and timer invoked then the connection has
          // gone idle - don't auto-retransmit to allow things to quiesce
     
        boolean  no_retrans = transmit_unack_packets.size() == 0 && timer_expired && receive_out_of_order_packets.size() == 0;
       
        int[]  sequences = writeHeaderStart( header, UDPPacket.COMMAND_ACK, no_retrans?UDPPacket.FLAG_LAZY_ACK:UDPPacket.FLAG_NONE );
       
        it = receive_out_of_order_packets.iterator();
       
        String  oos_str = "";
       
        int  count = 0;
       
        while( it.hasNext() && count < MAX_CONTIGUOUS_RETRANS_FOR_ACK ){
         
          Object[]  entry = (Object[])it.next();
       
          if ( entry[2] != null ){
         
            int  out_of_order_seq = ((Integer)entry[0]).intValue();
            int  out_of_rep_seq = ((Integer)entry[1]).intValue();
                 
            oos_str += (oos_str.length()==0?"":",") + out_of_order_seq + "/" + out_of_rep_seq;
           
            header.putInt(out_of_order_seq);
           
            count++;
          }
        }
       
        header.putInt( -1 );
       
        if ( count == 0 ){
         
          sent_receive_out_of_order_count    = current_receive_out_of_order_count;
         
View Full Code Here

     
      if ( packet_to_send == null ){
             
        byte[]  header_bytes = new byte[256];
       
        ByteBuffer  header = ByteBuffer.wrap( header_bytes );
       
        long  unack_in_sequence_count  = current_receive_unack_in_sequence_count;
 
        int[]  sequences = writeHeaderStart( header, UDPPacket.COMMAND_STAT_REQUEST, UDPPacket.FLAG_NONE );
       
View Full Code Here

     
      if ( packet_to_send == null ){
             
        byte[]  header_bytes = new byte[256];
       
        ByteBuffer  header = ByteBuffer.wrap( header_bytes );
       
        long  unack_in_sequence_count  = current_receive_unack_in_sequence_count;
 
        boolean  no_retrans = transmit_unack_packets.size() == 0 && receive_out_of_order_packets.size() == 0;
       
View Full Code Here

     
      synchronized( this ){
       
        byte[]  header_bytes = new byte[256];
       
        ByteBuffer  header = ByteBuffer.wrap( header_bytes );
       
        long  unack_in_sequence_count  = current_receive_unack_in_sequence_count;
   
        int[]  sequences = writeHeaderStart( header, UDPPacket.COMMAND_CLOSE, UDPPacket.FLAG_NONE );
       
        header.putInt( connection.getID());
       
        int  size = writeHeaderEnd( header, true );
       
        byte[]  packet_bytes = new byte[size];
       
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.