Exemplo n.º 1
0
    @JSFunction
    public static Object writeBuffer(Context cx, Scriptable thisObj, Object[] args, Function func) {
      ensureArg(args, 0);
      Buffer.BufferImpl buf = (Buffer.BufferImpl) args[0];
      TCPImpl tcp = (TCPImpl) thisObj;

      clearErrno();
      QueuedWrite qw = (QueuedWrite) cx.newObject(thisObj, QueuedWrite.CLASS_NAME);
      ByteBuffer bbuf = buf.getBuffer();
      qw.initialize(bbuf);
      tcp.byteCount += bbuf.remaining();
      tcp.offerWrite(qw, cx);
      return qw;
    }
Exemplo n.º 2
0
    private void processReads(Context cx) {
      if (!readStarted) {
        return;
      }
      int read;
      do {
        try {
          read = clientChannel.read(readBuffer);
        } catch (IOException ioe) {
          if (log.isDebugEnabled()) {
            log.debug("Error reading from channel: {}", ioe, ioe);
          }
          read = -1;
        }
        if (log.isDebugEnabled()) {
          log.debug("Read {} bytes from {} into {}", read, clientChannel, readBuffer);
        }
        if (read > 0) {
          readBuffer.flip();
          Buffer.BufferImpl buf = Buffer.BufferImpl.newBuffer(cx, this, readBuffer, true);
          readBuffer.clear();

          if (onRead != null) {
            onRead.call(cx, onRead, this, new Object[] {buf, 0, read});
          }
        } else if (read < 0) {
          setErrno(Constants.EOF);
          removeInterest(SelectionKey.OP_READ);
          if (onRead != null) {
            onRead.call(cx, onRead, this, new Object[] {null, 0, 0});
          }
        }
      } while (readStarted && (read > 0));
    }