Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

subclass to manipulate ServletOutputStream casting error

Thread view: 
Edward - 30 Jan 2006 20:44 GMT
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
Edward - 01 Feb 2006 00:18 GMT
> 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
[quoted text clipped - 71 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.