public int doStartTag() throws JspException {
try {
pageContext.getOut().write(getBody());
} catch (IOException e) {
throw new RuntimeException("IOError: " + e.getMessage(), e);
}
return EVAL_PAGE;
}
public static void include(String aResult, Writer writer,ServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String resourcePath = aResult;
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
if (rd == null) {
throw new ServletException("Not a valid resource path:"
+ resourcePath);
}
// Include the resource
PageResponse pageResponse = new PageResponse(response);
// Include the resource
rd.include((HttpServletRequest) request, pageResponse);
// write the response back to the JspWriter, using the correct encoding.
String encoding = "GB2312";
if (encoding != null) {
// use the encoding specified in the property file
pageResponse.getContent().writeTo(writer, encoding);
} else {
// use the platform specific encoding
pageResponse.getContent().writeTo(writer, null);
}
}
static final class PageResponse extends HttpServletResponseWrapper {
protected PrintWriter pagePrintWriter;
protected ServletOutputStream outputStream;
private PageOutputStream pageOutputStream = null;
/**
* Create PageResponse wrapped around an existing HttpServletResponse.
*/
public PageResponse(HttpServletResponse response) {
super(response);
}
/**
* Return the content buffered inside the {@link PageOutputStream}.
*
* @return
* @throws IOException
*/
public FastByteArrayOutputStream getContent() throws IOException {
// if we are using a writer, we need to flush the
// data to the underlying outputstream.
// most containers do this - but it seems Jetty 4.0.5 doesn't
if (pagePrintWriter != null) {
pagePrintWriter.flush();
}
/**
* Return instance of {@link PageOutputStream} allowing all data written
* to stream to be stored in temporary buffer.
*/
public ServletOutputStream getOutputStream() throws IOException {
if (pageOutputStream == null) {
pageOutputStream = new PageOutputStream();
}
return pageOutputStream;
}
/**
* Return PrintWriter wrapper around PageOutputStream.
*/
public PrintWriter getWriter() throws IOException {
if (pagePrintWriter == null) {
pagePrintWriter = new PrintWriter(new
OutputStreamWriter(
getOutputStream(), getCharacterEncoding()));
}
return pagePrintWriter;
}
}
static final class PageOutputStream extends ServletOutputStream {
private FastByteArrayOutputStream buffer;
public PageOutputStream() {
buffer = new FastByteArrayOutputStream();
}
/**
* Return all data that has been written to this OutputStream.
*/
public FastByteArrayOutputStream getBuffer() throws IOException {
flush();
return buffer;
}
public void close() throws IOException {
buffer.close();
}
public void flush() throws IOException {
buffer.flush();
}
public void write(byte[] b, int o, int l) throws IOException {
buffer.write(b, o, l);
}
public void write(int i) throws IOException {
buffer.write(i);
}
public void write(byte[] b) throws IOException {
buffer.write(b);
}
}
static public class FastByteArrayOutputStream extends OutputStream {
// Static --------------------------------------------------------
private static final int DEFAULT_BLOCK_SIZE = 8192;
public String toString() {
return new String(toByteArray());
}
// OutputStream overrides ----------------------------------------
public void write(int datum) throws IOException {
if (closed) {
throw new IOException("Stream closed");
} else {
if (index == blockSize) {
addBuffer();
}
// store the byte
buffer[index++] = (byte) datum;
}
}
public void write(byte[] data, int offset, int length) throws IOException {
if (data == null) {
throw new NullPointerException();
} else if
((offset < 0) || ((offset + length) > data.length) || (length
< 0)) {
throw new IndexOutOfBoundsException();
} else if (closed) {
throw new IOException("Stream closed");
} else {
if ((index + length) > blockSize) {
int copyLength;
do {
if (index == blockSize) {
addBuffer();
}
copyLength = blockSize - index;
if (length < copyLength) {
copyLength = length;
}
System.arraycopy(data, offset, buffer, index, copyLength);
offset += copyLength;
index += copyLength;
length -= copyLength;
} while (length > 0);
} else {
// Copy in the subarray
System.arraycopy(data, offset, buffer, index, length);
index += length;
}
}
}
// Public
public void writeTo(OutputStream out) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
// write the internal buffer directly
out.write(buffer, 0, index);
}
public void writeTo(RandomAccessFile out) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
// write the internal buffer directly
out.write(buffer, 0, index);
}
public void writeTo(Writer out, String encoding) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
while (iter.hasNext()) {
byte[] bytes = (byte[]) iter.next();
/**
* Create a new buffer and store the
* current one in linked list
*/
protected void addBuffer() {
if (buffers == null) {
buffers = new LinkedList();
}
buffers.addLast(buffer);
buffer = new byte[blockSize];
size += index;
index = 0;
}
}
}
2銆佸畾涔変竴涓叿浣撶殑鏍囩綾?br>
public class ListTag extends RiseAbstractTag {
private static final long serialVersionUID = 3385568988234498913L;