This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding the outputstream of container

 
amit ang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
outputstream overriding to get the byte stream for caching, didn't succeed. There is some problem of container that is not allowing to override the ouputstream implementation of it.
This all is done to cache the outputstream bytes to save the rendering time. Is this correct ? Whether this rendering problem (caching of bytes) will really improve the performance.
Following are the files created , using tomcat to test on.
Myresponse to call the overriden outputstream.

please suggest,
Amit
=============================================================
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import servlets.MyServletOutputStream;
public class MyHttpResponseWrapper implements HttpServletResponse{
protected HttpServletResponse _response;
private ByteArrayOutputStream _bout;
private Writer _cacheWriter;
private MyServletOutputStream outStream;
// private boolean hasStream;
// private boolean hasWriter;
public MyHttpResponseWrapper(HttpServletResponse response) {
this._response = response;
try {
_bout = new ByteArrayOutputStream();
_cacheWriter = new OutputStreamWriter(_bout);
outStream = new MyServletOutputStream(_response.getOutputStream(), _bout);
// hasStream = false;
// hasWriter = false;
}
// catch (IOException e) {
// System.out.println(
// "Got IOException constructing cached response: " + e.getMessage());
// }
catch (Exception er ) {
System.out.println(
"Got Exception constructing cached response: " + er.getMessage());
}
}
public PrintWriter getWriter() throws IOException {
System.out.println("Inside the overriden method ....getWriter()..");
// hasWriter = true;
return new PrintWriter(outStream,true);
//return _response.getWriter();
}
public ServletOutputStream getOutputStream() throws java.io.IOException
{
System.out.println("Inside the overriden method ....getOutputStream()..");
return outStream;
// if (hasWriter) {
// throw new IllegalStateException(
// "Cannot get output stream after getting writer");
// }
// MyServletOutputStream sos = (MyServletOutputStream)_response.getOutputStream();
// ByteArrayOutputStream baout = new ByteArrayOutputStream();
// byte[] byt;
// sos.write(byt);
// BufferedInputStream inputStream = new BufferedInputStream (new FileInputStream
("abc.html"));
// copyInToOut (inputStream, new BufferedOutputStream (response.getOutputStream()));
// inputStream.close();
/* FileOutputStream fo = new FileOutputStream("jspreq.ser");
DataOutputStream doo = new DataOutputStream(sos);
ObjectOutputStream oo = new ObjectOutputStream(doo);
oo.writeObject(sos);
oo.close();
doo.close();
fo.close();
*/
// return sos;
}
public void setStatus(int sc) {
_response.setStatus(sc);
}
public void setStatus(int sc, String sm) {
_response.setStatus(sc,sm);
}
public void setIntHeader(String name, int value) {
_response.setIntHeader(name, value);
}
public void setHeader(String name, String value) {
_response.setHeader(name, value);
}
public void addCookie(Cookie cookie) {
_response.addCookie(cookie);
}
public void addDateHeader(String name, long date) {
_response.addDateHeader(name, date);
}
public void addHeader(String name, String value) {
_response.addHeader(name, value);
}
public void addIntHeader(String name, int value) {
_response.addIntHeader(name, value);
}
public boolean containsHeader(String name) {
return _response.containsHeader(name);
}
public String encodeRedirectURL(String url) {
return _response.encodeRedirectURL(url);
}
public String encodeRedirectUrl(String url) {
return _response.encodeRedirectUrl(url);
}
public String encodeURL(String url) {
return _response.encodeURL(url);
}
public String encodeUrl(String url) {
return _response.encodeUrl(url);
}
public void sendError(int sc) throws IOException {
_response.sendError(sc);
}
public void sendError(int sc, String msg) throws IOException {
_response.sendError(sc,msg);
}
public void sendRedirect(String location) throws IOException {
_response.sendRedirect(location);
}
public void setDateHeader(String name, long date) {
_response.setDateHeader(name ,date);
}
public void setContentLength(int len) {
_response.setContentLength(len);
}
public void setContentType(String type) {
_response.setContentType(type);
}
public void setBufferSize(int size) {
_response.setBufferSize(size);
}
public int getBufferSize() {
return _response.getBufferSize();
}
public void flushBuffer() throws IOException {
_response.flushBuffer();
}
public boolean isCommitted() {
return _response.isCommitted();
}
public void reset() {
_response.reset();
}
public void setLocale(Locale loc) {
setLocale(loc);
}
public Locale getLocale() {
return _response.getLocale();
}
public String getCharacterEncoding() {
return _response.getCharacterEncoding();
}
protected void copyInToOut (BufferedInputStream in, BufferedOutputStream out) throws
IOException
{
int length;
byte data[] = new byte[8192];
while ( (length=in.read(data)) != -1 )
{
out.write (data, 0, length);
}
}
}
=====================================================================
import java.io.OutputStream;
import java.io.IOException;
import java.io.CharConversionException;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import javax.servlet.ServletOutputStream;
import java.io.*;
public class MyServletOutputStream extends ServletOutputStream {
private ServletOutputStream _servletOut;
private OutputStream _cacheOut;
public MyServletOutputStream(ServletOutputStream servletOut, OutputStream cacheOut) {
System.out.println("Inside MyServletOutputStream constructor...");
this._servletOut = servletOut;
this._cacheOut = cacheOut;
}
public void write(int b) throws IOException {
_servletOut.write(b);
_cacheOut.write(b);
}
public void write(byte b[]) throws IOException {
_servletOut.write(b);
_cacheOut.write(b);
}
public void write(byte buf[], int offset, int len) throws IOException {
_servletOut.write(buf, offset, len);
_cacheOut.write(buf, offset, len);
}
public void print(boolean b) throws IOException { _servletOut.print(b); }
public void print(char c) throws IOException { _servletOut.print(c);}
public void print(double d) throws IOException { _servletOut.print(d);}
public void print(float f) throws IOException { _servletOut.print(f);}
public void print(int i) throws IOException { _servletOut.print(i); }
public void print(long l) throws IOException { _servletOut.print(l);}
public void print(java.lang.String s) throws IOException { _servletOut.print(s);}
public void println() throws IOException { _servletOut.println(); }
public void println(boolean b) throws IOException { _servletOut.print(b);}
public void println(char c) throws IOException { _servletOut.print(c); }
public void println(double d) throws IOException { _servletOut.print(d); }
public void println(float f) throws IOException { _servletOut.print(f); }
public void println(int i) throws IOException { _servletOut.print(i); }
public void println(long l) throws IOException { _servletOut.print(l); }
public void println(java.lang.String s) throws IOException { _servletOut.print(s); }
}
===================================================================
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
outputstream overriding to get the byte stream for caching, didn't succeed. There is some problem of container that is not allowing to override the ouputstream implementation of it.
This all is done to cache the outputstream bytes to save the rendering time. Is this correct ? Whether this rendering problem (caching of bytes) will really improve the performance.

1. I can think of no valid reason to override the implementation of the response ServletOutputStream. It is a basic implementation of java.io.OutputStream which you can use as a base to implement any kind of behavior. For example a BufferedOutputStream with any size of buffer desired. You need to study the java.io classes - they are rather elegantly designed.
2. The extent to which buffer size affects your algorithm and overall performance is something to experiment with.
Bill
[ December 02, 2003: Message edited by: William Brogden ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic