コード例 #1
0
  public static void write(HttpServletResponse response, File file) throws IOException {

    if (response instanceof ByteBufferServletResponse) {
      ByteBufferServletResponse byteBufferResponse = (ByteBufferServletResponse) response;

      ByteBuffer byteBuffer = ByteBuffer.wrap(FileUtil.getBytes(file));

      byteBufferResponse.setByteBuffer(byteBuffer);
    } else if (response instanceof StringServletResponse) {
      StringServletResponse stringResponse = (StringServletResponse) response;

      String s = FileUtil.read(file);

      stringResponse.setString(s);
    } else {
      FileInputStream fileInputStream = new FileInputStream(file);

      FileChannel fileChannel = fileInputStream.getChannel();

      try {
        int contentLength = (int) fileChannel.size();

        response.setContentLength(contentLength);

        response.flushBuffer();

        fileChannel.transferTo(0, contentLength, Channels.newChannel(response.getOutputStream()));
      } finally {
        fileChannel.close();
      }
    }
  }
コード例 #2
0
  public static void write(HttpServletResponse response, ByteBuffer byteBuffer) throws IOException {

    if (response instanceof ByteBufferServletResponse) {
      ByteBufferServletResponse byteBufferResponse = (ByteBufferServletResponse) response;

      byteBufferResponse.setByteBuffer(byteBuffer);
    } else {
      write(response, byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
    }
  }
コード例 #3
0
  @Override
  protected void processFilter(
      HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
      throws Exception {

    ByteBufferServletResponse byteBufferResponse = new ByteBufferServletResponse(response);

    processFilter(ETagFilter.class, request, byteBufferResponse, filterChain);

    if (!ETagUtil.processETag(request, response, byteBufferResponse)) {
      ServletResponseUtil.write(response, byteBufferResponse.getByteBuffer());
    }
  }
コード例 #4
0
  public static void write(
      HttpServletResponse response, byte[] bytes, int offset, int contentLength)
      throws IOException {

    try {

      // LEP-3122

      if (!response.isCommitted()) {

        // LEP-536

        if (contentLength == 0) {
          contentLength = bytes.length;
        }

        response.setContentLength(contentLength);

        response.flushBuffer();

        if (response instanceof ByteBufferServletResponse) {
          ByteBufferServletResponse byteBufferResponse = (ByteBufferServletResponse) response;

          byteBufferResponse.setByteBuffer(ByteBuffer.wrap(bytes, offset, contentLength));
        } else {
          ServletOutputStream servletOutputStream = response.getOutputStream();

          servletOutputStream.write(bytes, offset, contentLength);
        }
      }
    } catch (IOException ioe) {
      if (ioe instanceof SocketException
          || ioe.getClass().getName().equals(_CLIENT_ABORT_EXCEPTION)) {

        if (_log.isWarnEnabled()) {
          _log.warn(ioe);
        }
      } else {
        throw ioe;
      }
    }
  }