Skip to content

Commit

Permalink
sitemesh#34 fix calling .resetBuffer() / .reset() has no effect on co…
Browse files Browse the repository at this point in the history
…m.opensymphony.module.sitemesh.filter.PageResponseWrapper
  • Loading branch information
sammyhk authored and Sammy Chu committed Jul 2, 2015
1 parent f13d79f commit bbf7bef
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public boolean writeSitemeshBufferFragment(SitemeshBufferFragment bufferFragment
public SitemeshBuffer getSitemeshBuffer() {
return new DefaultSitemeshBuffer(buf, count, fragments);
}

@Override
public void reset() {
super.reset();
fragments.clear();
}
}
52 changes: 37 additions & 15 deletions src/java/com/opensymphony/module/sitemesh/filter/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.opensymphony.module.sitemesh.util.FastByteArrayOutputStream;

import javax.servlet.ServletOutputStream;

import java.io.IOException;
import java.io.PrintWriter;

Expand All @@ -23,20 +24,23 @@ public class Buffer {
private final String encoding;
private final static TextEncoder TEXT_ENCODER = new TextEncoder();

private SitemeshBufferWriter bufferedWriter;
private FastByteArrayOutputStream bufferedStream;
private final FastByteArrayOutputStream bufferedStream = new FastByteArrayOutputStream();
private final SitemeshBufferWriter bufferedWriter = new SitemeshBufferWriter(128);
private PrintWriter exposedWriter;
private ServletOutputStream exposedStream;

private boolean usingWriter = false;
private boolean usingOutputStream = false;

public Buffer(PageParser pageParser, String encoding) {
this.pageParser = pageParser;
this.encoding = encoding;
}

public SitemeshBuffer getContents() throws IOException {
if (bufferedWriter != null) {
if (bufferedWriter.size() > 0) {
return bufferedWriter.getSitemeshBuffer();
} else if (bufferedStream != null) {
} else if (bufferedStream.size() > 0) {
return new DefaultSitemeshBuffer(TEXT_ENCODER.encode(bufferedStream.toByteArray(), encoding));
} else {
return new DefaultSitemeshBuffer(new char[0]);
Expand All @@ -48,32 +52,50 @@ public Page parse() throws IOException {
}

public PrintWriter getWriter() {
if (bufferedWriter == null) {
if (bufferedStream != null) {
throw new IllegalStateException("response.getWriter() called after response.getOutputStream()");
}
bufferedWriter = new SitemeshBufferWriter(128);
if (usingOutputStream) {
throw new IllegalStateException("response.getWriter() called after response.getOutputStream()");
}
if (exposedWriter == null) {
exposedWriter = new SitemeshPrintWriter(bufferedWriter);
}
usingWriter = true;
return exposedWriter;
}

public ServletOutputStream getOutputStream() {
if (bufferedStream == null) {
if (bufferedWriter != null) {
throw new IllegalStateException("response.getOutputStream() called after response.getWriter()");
}
bufferedStream = new FastByteArrayOutputStream();
if (usingWriter) {
throw new IllegalStateException("response.getOutputStream() called after response.getWriter()");
}
if (exposedStream == null) {
exposedStream = new ServletOutputStream() {

@Override
public void write(int b) {
bufferedStream.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
bufferedStream.write(b, off, len);
}
};
}
usingOutputStream = true;
return exposedStream;
}

public boolean isUsingStream() {
return bufferedStream != null;
return usingOutputStream;
}

public void resetBuffer() {
bufferedWriter.reset();
bufferedStream.reset();
}

public void reset() {
usingOutputStream = false;
usingWriter = false;
resetBuffer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import java.io.IOException;
import java.io.PrintWriter;

Expand Down Expand Up @@ -193,4 +194,22 @@ public SitemeshBuffer getContents() throws IOException {
return buffer.getContents();
}
}

@Override
public void reset() {
super.reset();

if (this.buffer != null) {
this.buffer.reset();
}
}

@Override
public void resetBuffer() {
super.resetBuffer();

if (this.buffer != null) {
this.buffer.resetBuffer();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ public SitemeshPrintWriter(SitemeshBufferWriter sitemeshWriter) {
this.sitemeshWriter = sitemeshWriter;
}

@Override
public void write(int c) {
super.write(c);
super.flush();
}

@Override
public void write(char[] buf, int off, int len) {
super.write(buf, off, len);
super.flush();
}

@Override
public void write(String s, int off, int len) {
super.write(s, off, len);
super.flush();
}

public boolean writeSitemeshBufferFragment(SitemeshBufferFragment bufferFragment) throws IOException {
return sitemeshWriter.writeSitemeshBufferFragment(bufferFragment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

package com.opensymphony.module.sitemesh.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
Expand All @@ -25,7 +24,7 @@
* @author <a href="mailto:[email protected]">Scott Farquhar</a>
* @version $Revision: 1.2 $
*/
public class FastByteArrayOutputStream extends ByteArrayOutputStream {
public class FastByteArrayOutputStream extends OutputStream {
private static final int DEFAULT_BLOCK_SIZE = 8192;

/** Internal buffer. */
Expand Down Expand Up @@ -125,8 +124,9 @@ else if ((offset < 0) || (offset + length > data.length)
}
}

public synchronized void reset() {
buffer = new byte[blockSize];
public void reset() {
index = 0;
size = 0;
buffers = null;
}

Expand All @@ -137,12 +137,4 @@ public String toString(String enc) throws UnsupportedEncodingException {
public String toString() {
return new String(toByteArray());
}

public void flush() throws IOException {
// does nothing
}

public void close() throws IOException {
// does nothing
}
}

0 comments on commit bbf7bef

Please sign in to comment.