Example #1
0
  /**
   * 创建新的buffer,保存老的buffer。
   *
   * @throws IllegalStateException 如果不在buffer模式,或<code>getWriter</code> 方法曾被调用,或<code>
   *     getOutputStream</code>方法从未被调用
   */
  public void pushBuffer() {
    if (!buffering) {
      throw new IllegalStateException("Buffering mode is required to pushBuffer");
    }

    if (stream == null && writer == null) {
      throw new IllegalStateException(
          "getOutputStream() or getWriter() method has not been called yet");
    }

    flushBufferAdapter();

    // 向stream或writer stack中压入新的buffer。
    if (stream != null) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();

      bytesStack.push(bytes);

      ((BufferedServletOutputStream) stream).updateOutputStream(bytesStack.peek());

      logger.logMessage(
          LogLevel.DEBUG, "Pushed new byte buffer (stack size is " + bytesStack.size() + ")");
    } else {
      StringWriter chars = new StringWriter();

      charsStack.push(chars);

      ((BufferedServletWriter) writer).updateWriter(charsStack.peek());

      logger.logMessage(
          LogLevel.DEBUG, "Pushed new character buffer (stack size is " + charsStack.size() + ")");
    }
  }
Example #2
0
  /**
   * 弹出最近的buffer,如果堆栈中只有一个buffer,则弹出后再创建一个新的。
   *
   * @return 最近的buffer内容,如果<code>getOutputStream</code>方法从未被调用,则返回空的byte array
   * @throws IllegalStateException 如果不在buffer模式,或<code>getWriter</code>方法曾被调用
   */
  public ByteArray popByteBuffer() {
    if (!buffering) {
      throw new IllegalStateException("Buffering mode is required to popByteBuffer");
    }

    if (writer != null) {
      throw new IllegalStateException(
          "Unable to popByteBuffer() since the getWriter() method has been called");
    }

    if (stream == null) {
      return new ByteArray(EMPTY_BYTE_ARRAY, 0, 0);
    } else {
      flushBufferAdapter();

      ByteArrayOutputStream block = bytesStack.pop();

      if (bytesStack.size() == 0) {
        bytesStack.push(new ByteArrayOutputStream());
      }

      ((BufferedServletOutputStream) stream).updateOutputStream(bytesStack.peek());

      logger.logMessage(
          LogLevel.DEBUG, "Popped the last byte buffer (stack size is " + bytesStack.size() + ")");

      return block.toByteArray();
    }
  }
Example #3
0
  /**
   * 清除所有buffers,常用于显示出错信息。
   *
   * @throws IllegalStateException 如果response已经commit
   */
  public void resetBuffer() {
    if (buffering) {
      flushBufferAdapter();

      if (stream != null) {
        bytesStack.clear();
        bytesStack.add(new ByteArrayOutputStream());
        ((BufferedServletOutputStream) stream).updateOutputStream(bytesStack.peek());
      }

      if (writer != null) {
        charsStack.clear();
        charsStack.add(new StringWriter());
        ((BufferedServletWriter) writer).updateWriter(charsStack.peek());
      }
    }

    super.resetBuffer();
  }