コード例 #1
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  /**
   * Flush the write buffer to the channel (if needed)
   *
   * @throws IOException
   */
  private boolean flushWrite(final boolean block) throws IOException, BadDescriptorException {
    if (reading || !modes.isWritable() || buffer.position() == 0) return false; // Don't bother
    int len = buffer.position();
    int nWritten = 0;
    buffer.flip();

    // For Sockets, only write as much as will fit.
    if (descriptor.getChannel() instanceof SelectableChannel) {
      SelectableChannel selectableChannel = (SelectableChannel) descriptor.getChannel();
      synchronized (selectableChannel.blockingLock()) {
        boolean oldBlocking = selectableChannel.isBlocking();
        try {
          if (oldBlocking != block) {
            selectableChannel.configureBlocking(block);
          }
          nWritten = descriptor.write(buffer);
        } finally {
          if (oldBlocking != block) {
            selectableChannel.configureBlocking(oldBlocking);
          }
        }
      }
    } else {
      nWritten = descriptor.write(buffer);
    }
    if (nWritten != len) {
      buffer.compact();
      return false;
    }
    buffer.clear();
    return true;
  }
コード例 #2
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  public synchronized int writenonblock(ByteList buf) throws IOException, BadDescriptorException {
    checkWritable();
    ensureWrite();

    // Ruby ignores empty syswrites
    if (buf == null || buf.length() == 0) return 0;

    if (buffer.position() != 0 && !flushWrite(false)) return 0;

    if (descriptor.getChannel() instanceof SelectableChannel) {
      SelectableChannel selectableChannel = (SelectableChannel) descriptor.getChannel();
      synchronized (selectableChannel.blockingLock()) {
        boolean oldBlocking = selectableChannel.isBlocking();
        try {
          if (oldBlocking) {
            selectableChannel.configureBlocking(false);
          }
          return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
        } finally {
          if (oldBlocking) {
            selectableChannel.configureBlocking(oldBlocking);
          }
        }
      }
    } else {
      // can't set nonblocking, so go ahead with it...not much else we can do
      return descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
    }
  }
コード例 #3
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  /**
   * Flush the write buffer to the channel (if needed)
   *
   * @throws IOException
   */
  private void flushWrite() throws IOException, BadDescriptorException {
    if (reading || !modes.isWritable() || buffer.position() == 0) return; // Don't bother

    int len = buffer.position();
    buffer.flip();
    int n = descriptor.write(buffer);

    if (n != len) {
      // TODO: check the return value here
    }
    buffer.clear();
  }
コード例 #4
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  /**
   * @throws IOException
   * @throws BadDescriptorException
   */
  private int bufferedWrite(ByteBuffer buf) throws IOException, BadDescriptorException {
    checkWritable();
    ensureWrite();

    // Ruby ignores empty syswrites
    if (buf == null || !buf.hasRemaining()) return 0;

    final int nbytes = buf.remaining();
    if (nbytes >= buffer.capacity()) { // Doesn't fit in buffer. Write immediately.
      flushWrite(); // ensure nothing left to write

      descriptor.write(buf);
      // TODO: check the return value here
    } else {
      if (nbytes > buffer.remaining()) flushWrite();

      buffer.put(buf);
    }

    if (isSync()) flushWrite();

    return nbytes - buf.remaining();
  }
コード例 #5
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  /**
   * @throws IOException
   * @throws BadDescriptorException
   */
  private int bufferedWrite(ByteList buf) throws IOException, BadDescriptorException {
    checkWritable();
    ensureWrite();

    // Ruby ignores empty syswrites
    if (buf == null || buf.length() == 0) return 0;

    if (buf.length() > buffer.capacity()) { // Doesn't fit in buffer. Write immediately.
      flushWrite(); // ensure nothing left to write

      int n = descriptor.write(ByteBuffer.wrap(buf.getUnsafeBytes(), buf.begin(), buf.length()));
      if (n != buf.length()) {
        // TODO: check the return value here
      }
    } else {
      if (buf.length() > buffer.remaining()) flushWrite();

      buffer.put(buf.getUnsafeBytes(), buf.begin(), buf.length());
    }

    if (isSync()) flushWrite();

    return buf.getRealSize();
  }