subclass to manipulate ServletOutputStream casting error

E

Edward

I have created a class that extends ServletOutputStream. Right now it
does not do anything but my plan is to use this to capture and
manipulate data (byte[] b) sent to the output stream for my own
devices. The class is below:

package testPkg;

import java.io.IOException;
import javax.servlet.ServletOutputStream;

public class TestOutputStream extends ServletOutputStream{
public TestOutputStream(){
super();
}
public void write(byte[] b, int off, int len) throws IOException {
super.write(b,off,len);
}
public void write(int b){
}
}

<<<<<<<<<<<<<<<<<<<<<<<

I also have a simple servlet that I am trying to call this class in:

package testPkg;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OutImageTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

FileInputStream fis = new FileInputStream("C:\\test.gif");
TestOutputStream sos =
(TestOutputStream)response.getOutputStream();
byte [] buffer = new byte [1024];
int len = 0;
while ((len = fis.read(buffer))>=0) {
sos.write(buffer, 0, len);
}
fis.close();
}
}

<<<<<<<<<<<<<<<<<<<<<<<

If I replace the line above:

TestOutputStream sos =
(TestOutputStream)response.getOutputStream();

with:

ServletOutputStream sos = response.getOutputStream();

the servlet works no problem but with the original line I get the
error: "java.lang.ClassCastException". Apparently it is having trouble
casting the ServletOutputStream to my class TestOutputStream even
though TestOutputStream extends ServletOutputStream although the
compiler (WebSphere, J2EE level 1.3) is requiring that I cast sos.

Anybody know what I am doing wrong?

Thanks.

Edward
 
E

Edward

Edward said:
I have created a class that extends ServletOutputStream. Right now it
does not do anything but my plan is to use this to capture and
manipulate data (byte[] b) sent to the output stream for my own
devices. The class is below:

package testPkg;

import java.io.IOException;
import javax.servlet.ServletOutputStream;

public class TestOutputStream extends ServletOutputStream{
public TestOutputStream(){
super();
}
public void write(byte[] b, int off, int len) throws IOException {
super.write(b,off,len);
}
public void write(int b){
}
}

<<<<<<<<<<<<<<<<<<<<<<<

I also have a simple servlet that I am trying to call this class in:

package testPkg;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OutImageTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {

FileInputStream fis = new FileInputStream("C:\\test.gif");
TestOutputStream sos =
(TestOutputStream)response.getOutputStream();
byte [] buffer = new byte [1024];
int len = 0;
while ((len = fis.read(buffer))>=0) {
sos.write(buffer, 0, len);
}
fis.close();
}
}

<<<<<<<<<<<<<<<<<<<<<<<

If I replace the line above:

TestOutputStream sos =
(TestOutputStream)response.getOutputStream();

with:

ServletOutputStream sos = response.getOutputStream();

the servlet works no problem but with the original line I get the
error: "java.lang.ClassCastException". Apparently it is having trouble
casting the ServletOutputStream to my class TestOutputStream even
though TestOutputStream extends ServletOutputStream although the
compiler (WebSphere, J2EE level 1.3) is requiring that I cast sos.

Anybody know what I am doing wrong?

Thanks.

Edward

FWIW I figured it out myself. I needed to instatiate the subclass
something like:

ServletOutputStream sos = response.getOutputStream();
TestOutputStream tos = new TestOutputStream(sos);

Then, when I override the methods I can manipulate the input stream.

Edward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top