/**
   * Read length bytes of data from the InputStream argument and write them to this output stream.
   */
  @Override
  public int write(InputStream is, int length) throws IOException {
    int writeLength = 0;

    TempBuffer tb = TempBuffer.allocate();
    byte[] buffer = tb.getBuffer();

    try {
      while (length > 0) {
        int sublen;

        if (length < buffer.length) sublen = length;
        else sublen = buffer.length;

        sublen = is.read(buffer, 0, sublen);

        if (sublen < 0) break;

        write(buffer, 0, sublen);

        writeLength += sublen;
        length -= sublen;
      }

      return writeLength;
    } finally {
      TempBuffer.free(tb);
    }
  }
  private boolean setLobParameter(Env env, int index, Value value) {
    if (_preparedStmt == null) {
      return false;
    }

    try {
      if (value == null || value.isNull()) {
        _preparedStmt.setObject(index, null);
      } else if (value.isString()) {
        _preparedStmt.setBinaryStream(index, value.toInputStream(), value.length());
      } else {
        InputStream inputStream = value.toInputStream();

        if (inputStream == null) {
          env.warning(
              L.l(
                  "type {0} ({1}) for parameter index {2} cannot be used for lob",
                  value.getType(), value.getClass(), index));
          return false;
        }

        int length = -1;

        if (value instanceof FileReadValue) {
          length = (int) ((FileReadValue) value).getLength();

          if (length <= 0) length = -1;
        }

        if (length < 0) {
          TempBuffer tempBuffer = TempBuffer.allocate();

          try {
            byte[] bytes = new byte[1024];

            int len;

            while ((len = inputStream.read(bytes, 0, 1024)) != -1) tempBuffer.write(bytes, 0, len);
          } catch (IOException e) {
            env.warning(e);
            return false;
          }

          TempReadStream tempReadStream = new TempReadStream(tempBuffer);
          tempReadStream.setFreeWhenDone(true);

          _preparedStmt.setBinaryStream(
              index, new ReadStream(tempReadStream), tempBuffer.getLength());
        } else {
          _preparedStmt.setBinaryStream(index, inputStream, length);
        }
      }
    } catch (SQLException e) {
      setError(env, e);
      return false;
    }

    return true;
  }
Exemplo n.º 3
0
  public Value receive(Env env, @Optional("1") long timeout) throws JMSException {
    Message message = _consumer.receive(timeout);

    if (message == null) return BooleanValue.FALSE;

    if (message instanceof ObjectMessage) {
      Object object = ((ObjectMessage) message).getObject();

      return env.wrapJava(object);
    } else if (message instanceof TextMessage) {
      return env.createString(((TextMessage) message).getText());
    } else if (message instanceof StreamMessage) {
      Object object = ((StreamMessage) message).readObject();

      return env.wrapJava(object);
    } else if (message instanceof BytesMessage) {
      BytesMessage bytesMessage = (BytesMessage) message;
      int length = (int) bytesMessage.getBodyLength();

      StringValue bb = env.createBinaryBuilder(length);

      TempBuffer tempBuffer = TempBuffer.allocate();
      int sublen;

      while (true) {
        sublen = bytesMessage.readBytes(tempBuffer.getBuffer());

        if (sublen > 0) bb.append(tempBuffer.getBuffer(), 0, sublen);
        else break;
      }

      TempBuffer.free(tempBuffer);

      return bb;
    } else if (message instanceof MapMessage) {
      MapMessage mapMessage = (MapMessage) message;

      Enumeration mapNames = mapMessage.getMapNames();

      ArrayValue array = new ArrayValueImpl();

      while (mapNames.hasMoreElements()) {
        String name = mapNames.nextElement().toString();

        Object object = mapMessage.getObject(name);

        array.put(env.createString(name), env.wrapJava(object));
      }

      return array;
    } else {
      return BooleanValue.FALSE;
    }
  }
  @Override
  public PrintWriter startTextMessage() throws IOException {
    if (_textOut == null) {
      _textOut = new WebSocketWriter(getWriteStream(), TempBuffer.allocate().getBuffer());
      _textWriter = new WebSocketPrintWriter(_textOut);
    }

    _textOut.init();

    return _textWriter;
  }
  @Override
  public OutputStream startBinaryMessage() throws IOException {
    if (_isWriteClosed.get())
      throw new IllegalStateException(L.l("{0} is closed for writing.", this));

    if (_binaryOut == null)
      _binaryOut = new WebSocketOutputStream(getWriteStream(), TempBuffer.allocate().getBuffer());

    _binaryOut.init();

    return _binaryOut;
  }
Exemplo n.º 6
0
  private boolean handleRequest(
      HttpServletRequest req,
      HttpServletResponse res,
      ClientSocket stream,
      OutputStream out,
      boolean keepalive)
      throws ServletException, IOException {
    ReadStream rs = stream.getInputStream();
    WriteStream ws = stream.getOutputStream();

    writeHeader(ws, FCGI_BEGIN_REQUEST, 8);

    int role = FCGI_RESPONDER;

    ws.write(role >> 8);
    ws.write(role);
    ws.write(keepalive ? FCGI_KEEP_CONN : 0); // flags
    for (int i = 0; i < 5; i++) ws.write(0);

    setEnvironment(stream, ws, req);

    InputStream in = req.getInputStream();
    TempBuffer tempBuf = TempBuffer.allocate();
    byte[] buf = tempBuf.getBuffer();
    int len = buf.length;
    int sublen;

    writeHeader(ws, FCGI_PARAMS, 0);

    boolean hasStdin = false;
    while ((sublen = in.read(buf, 0, len)) > 0) {
      hasStdin = true;
      writeHeader(ws, FCGI_STDIN, sublen);
      ws.write(buf, 0, sublen);
    }

    TempBuffer.free(tempBuf);
    tempBuf = null;

    /*
    if (hasStdin)
      writeHeader(fcgiSocket, ws, FCGI_STDIN, 0);
    */
    writeHeader(ws, FCGI_STDIN, 0);

    ws.flush();

    FastCGIInputStream is = new FastCGIInputStream(stream);

    int ch = parseHeaders(res, is);

    if (ch >= 0) out.write(ch);

    TempBuffer tb = TempBuffer.allocate();
    byte[] buffer = tb.getBuffer();

    while ((sublen = is.read(buffer, 0, buffer.length)) > 0) {
      out.write(buffer, 0, sublen);
    }

    TempBuffer.free(tb);

    return !is.isDead() && keepalive;
  }