Code example for ByteBuffer

Methods: array, getInt, remaining

0
          return false; 
        } 
 
        // since we're already in the select loop here for sure, we can just 
        // modify our selection key directly. 
        if (buffer_.remaining() == 0) {
          // get rid of the read select interests 
          selectionKey_.interestOps(0);
          state_ = FrameBufferState.READ_FRAME_COMPLETE;
        } 
 
        return true; 
      } 
 
      // if we fall through to this point, then the state must be invalid. 
      Log.e("Thrift", "Read was called but state is invalid (" + state_ + ")");
      return false; 
    } 
 
    /** 
     * Give this FrameBuffer a chance to write its output to the final client. 
     */ 
    public boolean write() { 
      if (state_ == FrameBufferState.WRITING) {
        try { 
          if (trans_.write(buffer_) < 0) {
            return false; 
          } 
        } catch (IOException e) {
          Log.w("Thrift", "Got an IOException during write!", e);
          return false; 
        } 
 
        // we're done writing. now we need to switch back to reading. 
        if (buffer_.remaining() == 0) {
          prepareRead(); 
        } 
        return true; 
      } 
 
      Log.e("Thrift", "Write was called, but state is invalid (" + state_ + ")");
      return false; 
    } 
 
    /** 
     * Give this FrameBuffer a chance to set its interest to write, once data 
     * has come in. 
     */ 
    public void changeSelectInterests() { 
      if (state_ == FrameBufferState.AWAITING_REGISTER_WRITE) {
        // set the OP_WRITE interest 
        selectionKey_.interestOps(SelectionKey.OP_WRITE);
        state_ = FrameBufferState.WRITING;
      } else if (state_ == FrameBufferState.AWAITING_REGISTER_READ) {
        prepareRead(); 
      } else if (state_ == FrameBufferState.AWAITING_CLOSE) {
        close(); 
        selectionKey_.cancel();
      } else { 
        Log.e("Thrift", "changeSelectInterest was called, but state is invalid (" + state_ + ")");
      } 
    } 
 
    /** 
     * Shut the connection down. 
     */ 
    public void close() { 
      // if we're being closed due to an error, we might have allocated a 
      // buffer that we need to subtract for our memory accounting. 
      if (state_ == FrameBufferState.READING_FRAME || state_ == FrameBufferState.READ_FRAME_COMPLETE) {
        readBufferBytesAllocated.addAndGet(-buffer_.array().length);
      } 
      trans_.close();
    } 
 
    /** 
     * Check if this FrameBuffer has a full frame read. 
     */ 
    public boolean isFrameFullyRead() { 
      return state_ == FrameBufferState.READ_FRAME_COMPLETE;
    } 
 
    /** 
     * After the processor has processed the invocation, whatever thread is 
     * managing invocations should call this method on this FrameBuffer so we 
     * know it's time to start trying to write again. Also, if it turns out that 
     * there actually isn't any data in the response buffer, we'll skip trying 
     * to write and instead go back to reading. 
     */ 
    public void responseReady() { 
      // the read buffer is definitely no longer in use, so we will decrement 
      // our read buffer count. we do this here as well as in close because 
      // we'd like to free this read memory up as quickly as possible for other 
      // clients. 
      readBufferBytesAllocated.addAndGet(-buffer_.array().length);
 
      if (response_.len() == 0) {
        // go straight to reading again. this was probably an oneway method 
        state_ = FrameBufferState.AWAITING_REGISTER_READ;
        buffer_ = null;
      } else { 
        buffer_ = ByteBuffer.wrap(response_.get(), 0, response_.len());
 
        // set state that we're waiting to be switched to write. we do this 
        // asynchronously through requestSelectInterestChange() because there is 
        // a possibility that we're not in the main thread, and thus currently 
        // blocked in select(). (this functionality is in place for the sake of 
        // the HsHa server.) 
        state_ = FrameBufferState.AWAITING_REGISTER_WRITE;
      } 
      requestSelectInterestChange(); 
    } 
 
    /** 
     * Actually invoke the method signified by this FrameBuffer. 
     */ 
    public void invoke() { 
      TTransport inTrans = getInputTransport();
      TProtocol inProt = inputProtocolFactory_.getProtocol(inTrans);
      TProtocol outProt = outputProtocolFactory_.getProtocol(getOutputTransport());
 
      try { 
        processorFactory_.getProcessor(inTrans).process(inProt, outProt);
        responseReady(); 
        return; 
      } catch (TException te) {
        Log.w("Thrift", "Exception while invoking!", te);
      } catch (Throwable t) {
        Log.e("Thrift", "Unexpected throwable while invoking!", t);
      } 
      // This will only be reached when there is a throwable. 
      state_ = FrameBufferState.AWAITING_CLOSE;
      requestSelectInterestChange(); 
    } 
 
    /** 
     * Wrap the read buffer in a memory-based transport so a processor can read 
     * the data it needs to handle an invocation. 
     */ 
    private TTransport getInputTransport() { 
      return new TMemoryInputTransport(buffer_.array());
    } 
 
    /** 
     * Get the transport that should be used by the invoker for responding. 
     */ 
    private TTransport getOutputTransport() { 
      response_ = new TByteArrayOutputStream();
      return outputTransportFactory_.getTransport(new TIOStreamTransport(response_));
    } 
 
    /** 
     * Perform a read into buffer. 
     *  
     * @return true if the read succeeded, false if there was an error or the 
     *         connection closed. 
     */ 
    private boolean internalRead() { 
      try { 
        if (trans_.read(buffer_) < 0) {
          return false; 
        } 
        return true; 
      } catch (IOException e) {
        Log.w("Thrift", "Got an IOException in internalRead!", e);