示例#1
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();
    }
  }
示例#2
0
    public void flush() throws IOException {
      ByteArray bytes = buffer.toByteArray();

      if (bytes.getLength() > 0) {
        ByteArrayInputStream inputBytes =
            new ByteArrayInputStream(bytes.getRawBytes(), bytes.getOffset(), bytes.getLength());
        InputStreamReader reader = new InputStreamReader(inputBytes, charset);

        StreamUtil.io(reader, writer, true, false);
        writer.flush();

        buffer.reset();
      }
    }
示例#3
0
  /**
   * 返回最上层的ByteArray
   *
   * @return 最近的buffer内容,如果<code>getOutputStream</code>方法从未被调用,则返回空的byte array
   * @throws IllegalStateException 如果不在buffer模式,或<code>getWriter</code>方法曾被调用
   */
  public ByteArray peekByteBuffer() {
    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.peek();

      return block.toByteArray();
    }
  }
示例#4
0
 public void write(byte[] b, int off, int len) throws IOException {
   buffer.write(b, off, len);
 }
示例#5
0
 public void write(byte[] b) throws IOException {
   buffer.write(b, 0, b.length);
 }
示例#6
0
 public void write(int b) throws IOException {
   buffer.write((byte) b);
 }
示例#7
0
 public void close() throws IOException {
   bytes.flush();
   bytes.close();
 }
示例#8
0
 public void flush() throws IOException {
   bytes.flush();
 }