Package com.google.opengse.iobuffer

Examples of com.google.opengse.iobuffer.IOBuffer


      encodeData(output_buf, true);

      // prepend response headers if they haven't already been committed
      if (!isCommitted()) {
        // prepend response headers to response body
        IOBuffer headers = prepareHeaders(keepAlive, true);
        output_buf.prepend(headers);
      }

      LOGGER.log(Level.FINEST, "request finished ("
          + output_buf.availableBytes() + " bytes)", output_buf);
View Full Code Here


    encodeData(output_buf, done);

    // If the headers haven't been written yet, prepare them and prepend to the
    // output_buf.
    if (isCommitted() == false) {
      IOBuffer headers = prepareHeaders(true, done);
      headers.flush();
      output_buf.prepend(headers);
    }
    // remember if we've finalized the encoding here so that it's
    // not done twice when HttpConnection.runServlet calls
    // HttpResponseImpl.finish
View Full Code Here

   * Gzips iobuffer data.
   */
  private void gzipEncodeData(IOBuffer iobuffer, boolean done) {
    try {
      if (deflater_output_buf == null) {
        deflater_output_buf = new IOBuffer();
        deflater_output_stream = new ThriftyGZIPOutputStream(
            new IOBufferOutputStream(deflater_output_buf));
      }
      compressEncodeData(iobuffer, done);
    } catch (IOException e) {
View Full Code Here

  /**
   * Deflates iobuffer data.
   */
  private void deflateEncodeData(IOBuffer iobuffer, boolean done) {
    if (deflater_output_buf == null) {
      deflater_output_buf = new IOBuffer();
      deflater_output_stream = new ThriftyDeflaterOutputStream(
          new IOBufferOutputStream(deflater_output_buf));
    }
    compressEncodeData(iobuffer, done);
  }
View Full Code Here

   * @param done     <code>true</code> if this is the last data to encode
   */
  private void partialContentEncodeData(IOBuffer iobuffer, boolean done) {
    try {
      int len = iobuffer.availableBytes();
      IOBuffer newIOBuffer = new IOBuffer();

      while (len > 0) {
        // If we're already past the last range, simply skip data.
        if (rangeIndex == range.getNumRanges()) {
          iobuffer.skipBytes(len);
          rangeCurrentPos += len;
          len = 0;
          continue;
        }
        Range.Pair p = range.getRange(rangeIndex, rangeContentLength);
        // If necessary, skip to the start of the current range.
        int skip = p.getStart() - rangeCurrentPos;
        if (skip > 0) {
          int skipped = (int) iobuffer.skipBytes(skip);
          rangeCurrentPos += skipped;
          len -= skipped;
          // If there is no data left, continue.
          if (len == 0) {
            continue;
          }
        }
        // The byte range is inclusive, so we must transfer 1 + getEnd().
        int transfer = Math.min((p.getEnd() - rangeCurrentPos + 1), len);
        if (transfer < 0) {
          throw new IllegalStateException();
        }

        if (transfer > 0) {
          // If we're at the start of a multipart byterange, output boundary.
          if (range.getNumRanges() > 1 && rangeCurrentPos == p.getStart()) {
            // It's ok for additional CRLFs to preceed first boundary.
            newIOBuffer.writeBytes("\r\n--".getBytes());
            newIOBuffer.writeBytes(rangeBoundary.getBytes());
            if (rangeContentType != null) {
              newIOBuffer.writeBytes("\r\n".getBytes());
              newIOBuffer.writeBytes("Content-Type: ".getBytes());
              newIOBuffer.writeBytes(rangeContentType.getBytes());
            }
            newIOBuffer.writeBytes("\r\n".getBytes());
            newIOBuffer.writeBytes("Content-Range: ".getBytes());
            newIOBuffer.writeBytes(("bytes " + p.getStart() + "-" + p.getEnd()
                + "/" + rangeContentLength).getBytes());
            newIOBuffer.writeBytes("\r\n\r\n".getBytes());
          }
          newIOBuffer.transfer(iobuffer, transfer);
          rangeCurrentPos += transfer;
          len -= transfer;
        }
        // Advance to the next range if we completed the current one.
        if (rangeCurrentPos == (p.getEnd() + 1)) {
          rangeIndex += 1;
        }
      }

      if (done && !head_request) {
        // Sanity checks
        if (rangeIndex < range.getNumRanges()) {
          Range.Pair p = range.getRange(rangeIndex, rangeContentLength);
          if (p.getStart() != rangeContentLength) {
            throw new IllegalStateException();
          }
        } else {
          if (rangeIndex != range.getNumRanges()) {
            throw new IllegalStateException();
          }
        }
        // If we encoded as multipart/byteranges, output final boundary.
        if (range.getNumRanges() > 1) {
          newIOBuffer.writeBytes("\r\n--".getBytes());
          newIOBuffer.writeBytes(rangeBoundary.getBytes());
          newIOBuffer.writeBytes("--\r\n".getBytes());
        }
      }

      // Prepend new iobuffer
      if (iobuffer.availableBytes() != 0) {
        throw new IllegalStateException();
      }
      if (newIOBuffer.isEmpty() == false) {
        iobuffer.prepend(newIOBuffer);
      }
    } catch (IOException e) {
      // should never reach this statement. IOException in IOBuffers only are
      // thrown from handling consume callbacks. new_iobuf doesn't have a
View Full Code Here

   * @param done     <code>true</code> if this is the last data to encode
   */
  private void chunkEncodeData(IOBuffer iobuffer, boolean done) {
    try {
      int len = iobuffer.availableBytes();
      IOBuffer tmpBuffer = new IOBuffer();

      if (len > 0) {
        String stringLength = Integer.toHexString(len).toUpperCase();

        tmpBuffer.writeBytes(stringLength.getBytes());
        tmpBuffer.writeBytes("\r\n".getBytes());
        tmpBuffer.transfer(iobuffer, len);
        tmpBuffer.writeBytes("\r\n".getBytes());

        // should have transferred all available bytes
        if (iobuffer.availableBytes() != 0) {
          throw new IllegalStateException();
        }
      }

      // if this is the last chunk in the response, append
      // 1*("0") CRLF CRLF, as per specification
      if (done) {
        tmpBuffer.writeBytes("0\r\n\r\n".getBytes());
      }

      // prepend new iobuffer
      if (!tmpBuffer.isEmpty()) {
        iobuffer.prepend(tmpBuffer);
      }
    } catch (IOException e) {
      // should never reach this statement. IOException in IOBuffers only are
      // thrown from handling consume callbacks. new_iobuf doesn't have a
View Full Code Here

    // Mark the request committed
    committed = true;

    // write response header
    IOBuffer headers = new IOBuffer();
    try {
      // An IOBuffer without a consume callback (headers)
      // cannot throw an IOException; ignore the possibility
      writeIOBuffer(headers);
    } catch (IOException e) {
View Full Code Here

  /**
   * Returns a copy of the i/o buffer containing the post body.
   */
  private IOBuffer getPostBody() {
    if (post_body_ != null) {
      return new IOBuffer(post_body_);
    } else {
      return new IOBuffer();
    }
  }
View Full Code Here

  /**
   * Sets the post body i/o buffer to the provided string
   */
  void setPostBody(final String body)
      throws CharacterCodingException, IOException {
    _setPostBody(new IOBuffer(body, "US-ASCII"), body.length());
  }
View Full Code Here

   * Sets the post body i/o buffer to a copy of the provided i/o buffer. Any
   * parameters in the given post body will be parsed the next time parameters
   * are requested from this object.
   */
  void _setPostBody(final IOBuffer buf, final int length) throws IOException {
    post_body_ = new IOBuffer();
    buf.flush();
    post_body_.transfer(buf, length);
    post_body_.flush();
    parsed_params_ = null// Reparse the parameters when needed.
  }
View Full Code Here

TOP

Related Classes of com.google.opengse.iobuffer.IOBuffer

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.