Package java.nio

Examples of java.nio.ByteBuffer


    }
   
    public byte[]
    getDigest()
    {
      ByteBuffer result_buffer = ByteBuffer.allocate(16);
     
      finalDigest(result_buffer);
     
      byte[] result = new byte[16];
     
      result_buffer.position(0);
     
      for(int i = 0 ; i < result.length ; i++) {
       
        result[i] = result_buffer.get();
      }  
     
      return result;  
    }
View Full Code Here


     
      long  bytes_read;
     
      int    read_lim = (int)( max_bytes - total_read );
     
      ByteBuffer  payload_buffer = buffers[1];
     
      if ( payload_buffer == null ){
       
        int  rem = length_buffer.remaining();
        int  lim  = length_buffer.limit();
       
        if ( rem > read_lim ){
         
          length_buffer.limit( length_buffer.position() + read_lim );
        }
       
        bytes_read = transport.read( buffers, 0, 1 );
       
        length_buffer.limit( lim );
       
        protocol_bytes_last_read += bytes_read;
       
        if ( length_buffer.hasRemaining()){
         
          total_read += bytes_read;
         
          break;
         
        }else{
         
          length_buffer.flip();
         
          int  size = length_buffer.getInt();
         
          if ( size > MAX_MESSAGE_LENGTH ){
           
            Debug.out( "Message too large for generic payload" );
           
            throw( new IOException( "message too large" ));
          }
         
          buffers[1] = ByteBuffer.allocate( size );
         
          length_buffer.flip();
        }
      }else{
       
        int  rem = payload_buffer.remaining();
        int  lim  = payload_buffer.limit();
       
        if ( rem > read_lim ){
         
          payload_buffer.limit( payload_buffer.position() + read_lim );
        }

        bytes_read = transport.read( buffers, 1, 1 );
       
        payload_buffer.limit( lim );

        data_bytes_last_read += bytes_read;
       
        if ( payload_buffer.hasRemaining()){
         
          total_read += bytes_read;
         
          break;
        }
       
        payload_buffer.flip();
       
        messages.add( new GenericMessage( msg_type, msg_desc, new DirectByteBuffer( payload_buffer ), false ));
       
        buffers[1= null;
      }
View Full Code Here

   * Calculate the SHA-1 hash for the given bytes.
   * @param bytes data to hash
   * @return 20-byte hash
   */
  public byte[] calculateHash( byte[] bytes ) {
    ByteBuffer buff = ByteBuffer.wrap( bytes );
    return calculateHash( buff );
  }
View Full Code Here

  public ReferenceCountedDirectByteBuffer
  duplicate(
    int    offset,
    int    length )
  {
    ByteBuffer  duplicate = getBufferInternal().duplicate();
   
    duplicate.position( duplicate.position() + offset );
   
    duplicate.limitduplicate.position() + length );
   
    ReferenceCountedDirectByteBuffer res = new ReferenceCountedDirectByteBufferDuplicate( duplicate );
   
    return( res );
  }
View Full Code Here

    md5.reset();
    final int position = buffer.position(DirectByteBuffer.SS_DW);
    md5.update(buffer.getBuffer(DirectByteBuffer.SS_DW));
    buffer.position(DirectByteBuffer.SS_DW, position);
    ByteBuffer md5Result  = ByteBuffer.allocate(16);
    md5Result.position(0);
    md5.finalDigest( md5Result );

    final byte[] result =new byte[16];
    md5Result.position(0);
    for (int i =0; i <result.length; i++ )
    {
      result[i] = md5Result.get();
    }  

    return result;   
  }
View Full Code Here

        new GenericMessageDecoder( msg_id, msg_desc ),
        stream_crypto != MessageManager.STREAM_ENCRYPTION_NONE,       // use crypto
        stream_crypto != MessageManager.STREAM_ENCRYPTION_RC4_REQUIRED,   // allow fallback
        shared_secrets );
   
    ByteBuffer  initial_data = ByteBuffer.wrap( msg_id.getBytes());
   
    if ( upper_initial_data != null ){
   
      GenericMessage  gm = new GenericMessage( msg_id, msg_desc, new DirectByteBuffer( upper_initial_data ), false );
     
      DirectByteBuffer[]  payload = new GenericMessageEncoder().encodeMessage( gm )[0].getRawData();
     
      int  size = initial_data.remaining();
     
      for (int i=0;i<payload.length;i++){
       
        size += payload[i].remaining( DirectByteBuffer.SS_MSG );
      }
     
      ByteBuffer  temp = ByteBuffer.allocate( size );
     
      temp.put( initial_data );
     
      for (int i=0;i<payload.length;i++){
     
        temp.put( payload[i].getBuffer( DirectByteBuffer.SS_MSG ));
      }
     
      temp.rewind();
     
      initial_data = temp;
    }
           
    connection.connect(
View Full Code Here

      res = getSliceBuffer( _allocator, _length );
     
    }else{
       
      ByteBuffer  buff = null;
     
      Integer reqVal = new Integer(_length);
     
        //loop through the buffer pools to find a buffer big enough
     
      Iterator it = buffersMap.keySet().iterator();
     
      while (it.hasNext()) {
       
        Integer keyVal = (Integer)it.next();
 
          //  check if the buffers in this pool are big enough
       
        if ( reqVal.compareTo(keyVal) <= 0 ){
         
          ArrayList bufferPool = (ArrayList)buffersMap.get(keyVal);
         
          while( true ){
           
            synchronized ( poolsLock ) {
           
              // make sure we don't remove a buffer when running compaction
              // if there are no free buffers in the pool, create a new one.
              // otherwise use one from the pool
             
              if ( bufferPool.isEmpty()){
               
                buff = allocateNewBuffer(keyVal.intValue());
               
                if ( buff == null ){
                 
                  Debug.out( "allocateNewBuffer for " + _length + " returned null" );
                }
               
                break;
               
              }else{
               
                synchronized ( bufferPool ) {
                 
                  buff = (ByteBuffer)bufferPool.remove(bufferPool.size() - 1);
                }
               
                if ( buff == null ){
                 
                  Debug.out( "buffer pool for " + _length + " contained null entry" );
                 
                }else{
                 
                  break;
                }
              }
            }
          }
         
          break;
        }
      }
   
      if ( buff == null ){
         
        String str = "Unable to find an appropriate buffer pool for " + _length;
       
          Debug.out( str );
         
          throw( new RuntimeException( str ));
      }
     
      res = new DirectByteBuffer( _allocator, buff, this );      
    }
     
          // clear doesn't actually zero the data, it just sets pos to 0 etc.
       
    ByteBuffer buff = res.getBufferInternal();
   
        buff.clear();   //scrub the buffer
       
    buff.limit( _length );
   
        bytesOut += buff.capacity();
                               
        if ( DEBUG_PRINT_MEM || DEBUG_TRACK_HANDEDOUT ){
         
          synchronized( handed_out ){
                   
View Full Code Here

 
  protected void
  returnBufferSupport(
    DirectByteBuffer ddb )
  {   
    ByteBuffer  buff = ddb.getBufferInternal();
   
    if ( buff == null ){
     
      Debug.out( "Returned dbb has null delegate" );
    }
   
    int  capacity = buff.capacity();

    bytesIn += capacity;
       
      if ( DEBUG_TRACK_HANDEDOUT ){
       
View Full Code Here

        if ( slot != -1 ){
         
          short  slice_entry_size   = SLICE_ENTRY_SIZES[slice_index];
          short  slice_entry_count  = SLICE_ENTRY_ALLOC_SIZES[slice_index];
         
          ByteBuffer  chunk = ByteBuffer.allocateDirectslice_entry_size*slice_entry_count  );
         
          my_allocs[slot] = true;
         
          for (short i=0;i<slice_entry_count;i++){
           
            chunk.limit((i+1)*slice_entry_size);
            chunk.position(i*slice_entry_size);
           
            ByteBuffer  slice = chunk.slice();
           
            sliceBuffer new_buffer = new sliceBuffer( slice, slot, i );
           
            if ( i == 0 ){
             
              sb = new_buffer;
             
              slice_use_count[slice_index]++;
             
            }else{
             
              my_slice_entries.add( new_buffer );
            }
          }
        }else{
         
          if ( !slice_alloc_fails[slice_index] ){
           
            slice_alloc_fails[slice_index= true;
           
            Debug.out( "Run out of slice space for '" + SLICE_ENTRY_SIZES[slice_index] + ", reverting to normal allocation" );
          }
         
          ByteBuffer buff = ByteBuffer.allocate( _length );
         
            return( new DirectByteBuffer( _allocator, buff, this ));

        }
      }
View Full Code Here

    plugin_connection = new ConnectionImpl(connection, incoming);

    changePeerState( PEPeer.CONNECTING );

    ByteBuffer  initial_outbound_data = null;

    if ( use_crypto ){
     
      BTHandshake handshake = new BTHandshake( manager.getHash(),
          manager.getPeerId(),
                    manager.isExtendedMessagingEnabled(), other_peer_handshake_version );
     
      if (Logger.isEnabled())
          Logger.log(new LogEvent(this, LOGID,
              "Sending encrypted handshake with reserved bytes: " +
                ByteFormatter.nicePrint(handshake.getReserved(), false)));

      DirectByteBuffer[] ddbs = handshake.getRawData();

      int  handshake_len = 0;

      for (int i=0;i<ddbs.length;i++){

        handshake_len += ddbs[i].remaining( DirectByteBuffer.SS_PEER );
      }

      initial_outbound_data = ByteBuffer.allocate( handshake_len );

      for (int i=0;i<ddbs.length;i++){

        DirectByteBuffer  ddb = ddbs[i];

        initial_outbound_data.put( ddb.getBuffer( DirectByteBuffer.SS_PEER ));

        ddb.returnToPool();
      }

      initial_outbound_data.flip();

      handshake_sent = true;
    }

    int  priority;
View Full Code Here

TOP

Related Classes of java.nio.ByteBuffer

Copyright © 2015 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.