Package java.nio

Examples of java.nio.ByteBuffer


        // Register accepts on the server socket with the selector. This
        // step tells the selector that the socket wants to be put on the
        // ready list when accept operations occur.
        Selector selector = Selector.open();

        ByteBuffer cmdBuffer = ByteBuffer.allocateDirect(MAX_COMMAND_BUFLEN);
        IOHandler ioHandler = new IOHandler(cmdBuffer);
        AcceptHandler acceptHandler = new AcceptHandler(ioHandler);

        channel.register(selector, SelectionKey.OP_ACCEPT, acceptHandler);
View Full Code Here


         
          if ( !pool.isEmpty()){
           
            Object[] entry = (Object[])pool.removeLast();
           
            ByteBuffer buff = (ByteBuffer)entry[0];
           
                buff.clear();
               
            buff.limit( length );
           
            return( new DirectByteBuffer( allocator, buff, this ));
          }
        } 
 
        DirectByteBuffer  buffer = new DirectByteBuffer( allocator, ByteBuffer.allocate( pool_sizes[pool_index] ), this );
       
        ByteBuffer buff = buffer.getBufferInternal();
                 
        buff.limit( length );
       
        return( buffer );
 
      }else{
     
View Full Code Here

  returnBufferSupport(
    DirectByteBuffer  buffer )
  {
    if ( USE_POOLS ){
     
      ByteBuffer  buff = buffer.getBufferInternal();
     
      int  length = buff.capacity();
     
      int  pool_index = getPoolIndex( length );
     
      if ( pool_index != -1 ){
       
View Full Code Here

            channel = openConnection(_sockAddr);
        } catch (IOException ioe) {
            throw new IllegalStateException("failed opening a socket", ioe);
        }
        final byte[] dst;
        final ByteBuffer rbuf = _rbufPool.borrowObject();
        try {
            sendRequest(channel, idx);
            dst = recvResponse(channel, rbuf);
        } catch (IOException ioe) {
            IOUtils.closeQuietly(channel);
View Full Code Here

        } catch (IOException ioe) {
            throw new IllegalStateException("failed opening a socket", ioe);
        }
        final int size = idxs.length;
        final byte[][] dst = new byte[size][];
        final ByteBuffer rbuf = _rbufPool.borrowObject();
        try {
            sendRequest(channel, idxs);
            for(int i = 0; i < size; i++) {
                dst[i] = recvResponse(channel, rbuf);
            }
View Full Code Here

        int buflen = bFilePath.length + 16 + (Primitives.LONG_BYTES * size);//+ 4 + 4 + 4 + 4 + 8n;
        if(buflen > RemotePagingService.MAX_COMMAND_BUFLEN) {
            throw new IllegalStateException("command size exceeds limit in MAX_COMMAND_BUFLEN("
                    + RemotePagingService.MAX_COMMAND_BUFLEN + "): " + buflen + " bytes");
        }
        ByteBuffer oprBuf = ByteBuffer.allocate(buflen);
        oprBuf.putInt(buflen - 4);
        oprBuf.putInt(RemotePagingService.COMMAND_TRACK_READ);
        oprBuf.putInt(bFilePath.length); // #1
        oprBuf.put(bFilePath); // #2
        oprBuf.putInt(size); // #3
        for(int i = 0; i < size; i++) {
            oprBuf.putLong(idxs[i]); // #4
        }
        oprBuf.flip();
        NIOUtils.writeFully(channel, oprBuf);
    }
View Full Code Here

        }

        for(int i = 0; i < size; i++) {
            final long offset = offsets[i];
            // get data length
            final ByteBuffer tmpBuf = ByteBuffer.allocate(4);
            try {
                fileChannel.read(tmpBuf, offset);
            } catch (IOException e) {
                LOG.error(e);
                throw e;
            }
            tmpBuf.flip();
            final int length = tmpBuf.getInt();
            tmpBuf.rewind();
            IoBuffer ioBuf = IoBuffer.wrap(tmpBuf);
            out.write(ioBuf);
            // attempt zero-copy sendfile
            long position = offset + 4;
            long count = length;
View Full Code Here

        }
    }

    private byte[] recvResponse(final ByteChannel channel, final ByteBuffer rcvBuf)
            throws IOException {
        ByteBuffer tmpBuf = ByteBuffer.allocate(4);
        NIOUtils.readFully(channel, tmpBuf, 4);
        tmpBuf.flip();
        int datalen = tmpBuf.getInt();

        final ByteBuffer buf = truncateBuffer(rcvBuf, datalen);
        NIOUtils.readFully(channel, buf, datalen);
        buf.flip();
        if(ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
            buf.order(ByteOrder.BIG_ENDIAN);
        }
        final byte[] b = new byte[datalen];
        buf.get(b);
        if(buf != rcvBuf) {
            _rbufPool.returnObject(buf);
        }
        return b;
    }
View Full Code Here

              writeBytes( encoded );
             
            }else{
                          
              ByteBuffer  bb   = Constants.DEFAULT_CHARSET.encode( tempString );          
             
              writeInt( bb.limit() );
             
              writeChar(':');
             
              writeByteBuffer(bb );
            }
           
        }else if(object instanceof Map){
         
            Map tempMap = (Map)object;
           
            SortedMap tempTree = null;
           
              // unfortunately there are some occasions where we want to ensure that
              // the 'key' of the map is not mangled by assuming its UTF-8 encodable.
              // In particular the response from a tracker scrape request uses the
              // torrent hash as the KEY. Hence the introduction of the type below
              // to allow the constructor of the Map to indicate that the keys should
              // be extracted using a BYTE_ENCODING
             
            boolean  byte_keys = object instanceof ByteEncodedKeyHashMap;
           
            //write the d           
            writeChar('d');
           
            //are we sorted?
            if ( tempMap instanceof TreeMap ){
             
                tempTree = (TreeMap)tempMap;
               
            }else{
                tempTree = new TreeMap(tempMap);
            }           
                  
            Iterator  it = tempTree.entrySet().iterator();
           
            while( it.hasNext()){
             
              Map.Entry  entry = (Map.Entry)it.next();
     
              Object o_key = entry.getKey();
                            
              Object value = entry.getValue();

              if (value != null)
        {
          if (o_key instanceof byte[])
          {
            encodeObject(o_key);
            if (!encodeObject(value))
              encodeObject("");
          } else if(o_key instanceof String)
          {
            String key = (String) o_key;
            if (byte_keys)
            {
              try
              {
                encodeObject(Constants.BYTE_CHARSET.encode(key));
                if (!encodeObject(value))
                  encodeObject("");
              } catch (UnsupportedEncodingException e)
              {
                throw (new IOException("BEncoder: unsupport encoding: " + e.getMessage()));
              }
            } else
            {
                // if we put non-ascii chars in as keys we can get horrible expanding
                // config issues as we cycle through decode/encode cycles with certain
                // characters
             
              if ( Constants.IS_CVS_VERSION ){
                char[]  chars = key.toCharArray();
               
                for ( char c: chars ){

                  if (c >= '\u0080'){

                    if ( non_ascii_logs < 50 ){
                     
                      non_ascii_logs++;
                     
                      Debug.out( "Non-ASCII key: " + key );
                    }
                   
                    break;
                  }
                }
              }
              encodeObject(key); // Key goes in as UTF-8
              if (!encodeObject(value))
                encodeObject("");
            }
          } else
            Debug.out( "Attempt to encode an unsupported map key type: " + object.getClass() + ";value=" + object);
        }    
            }
           
            writeChar('e');
           
           
        }else if(object instanceof List){
         
            List tempList = (List)object;
           
              //write out the l
           
            writeChar('l');                                  
           
            for(int i = 0; i<tempList.size(); i++){
               
              encodeObject( tempList.get(i));                           
            }  
           
            writeChar('e');                         
           
        }else if(object instanceof Long){
         
            Long tempLong = (Long)object;        
            //write out the l      
            writeChar('i');
            writeLong(tempLong.longValue());
            writeChar('e');
         }else if(object instanceof Integer){
          
      Integer tempInteger = (Integer)object;        
      //write out the l      
      writeChar('i');
      writeInt(tempInteger.intValue());
      writeChar('e');
     
       }else if(object instanceof byte[]){
        
            byte[] tempByteArray = (byte[])object;
            writeInt(tempByteArray.length);
            writeChar(':');
            if ( url_encode ){
              writeBytes(URLEncoder.encode(new String(tempByteArray, Constants.BYTE_ENCODING), Constants.BYTE_ENCODING ).getBytes());
            }else{
              writeBytes(tempByteArray);
            }
           
       }else if(object instanceof ByteBuffer ){
        
           ByteBuffer  bb = (ByteBuffer)object;
           writeInt(bb.limit());
           writeChar(':');
            writeByteBuffer(bb);
           
       }else if ( object == null ){
        
View Full Code Here

   
    public byte[]
    calculateHash(
      byte[]    data )
    {
      ByteBuffer input_buffer = ByteBuffer.wrap(data);
     
      reset();
     
      update(input_buffer);
      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

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.