Hi,

We based our servlets and JHTML pages on 3rd party libraries which
rely on the ServletOutputStream. Now we need to move to the JavaWebServer 2.0
and JSP and we've run into problems. Since JSP pages cannot obtain
the ServletOuputStream (since they can't mix text and binary output)
we wrote a wrapper class which extends ServletOutputStream but writes
everything to a Writer that's passed in to the contructor. Since our
3rd party libraries pick up the ServletOutputStream from a container
(and not directly from the response object) we dump our own
wrapper class (WrapServletOutputStream) into the container
- which works fine since WrapServletOutputStream extends ServletOutputStream.
The 3rd party library classes write output to our WrapServletOutputStream
which in turn writes the output to the Writer passed in to the constructor.
There is only 1 problem - how to write bytes to the Writer ?
Since Writers provide no means of writing bytes (and since I can't
write the bytes directly to the ServletOutputStream - since it's not
available) I overloaded the write(int) method so that it would
store the bytes in a byte array an then keep trying to convert
the byte array to a String (since the bytes really represent
characters - since we are outputting html pages). As soon as it
manages to convert the byte array to a String it resets the byte
array. This approach seems to be working so far - but I'm wondering
if it can get me into trouble later on. Can anyone see a problem
with this approach (including efficiancy issues)?

//Here's the WrapServletOutputStream class:
public class WrapServletOutputStream extends ServletOutputStream
{
Writer charOut = null;
private String lineSeparator;

// Note: the byte array is of size 10 to accomodate multi-byte languages
byte[] bytesOfAChar = new byte[10];
int count = 1;

/*
* All output sent to this stream is written to the Writer passed
* in to the constructor.
*/
public WrapServletOutputStream(Writer out)
{
charOut = out;
lineSeparator = System.getProperty("line.separator");
}

/* Here is the method which buffers the bytes into an array
* until they are successfully converted to a String
*/
public void write(int b)throws IOException
{
bytesOfAChar[count - 1] = (byte)b;

String aChar = new String(bytesOfAChar,0,count);
if(aChar.equals(""))
count++;
else
{
count = 1;
charOut.write(aChar);
charOut.flush();
}
}

public void print(String s) throws IOException
{
charOut.write(s);
charOut.flush();
}

public void print(char c) throws IOException
{
print(String.valueOf(c));
}

public void println(String s) throws IOException
{
print(s);
newLine();
}

public void println(char c) throws IOException
{
print(c);
newLine();
}

public void flush() throws IOException
{
charOut.flush();
}

public void close() throws IOException
{
charOut.close();
}

private void newLine() throws IOException
{
charOut.write(lineSeparator);
}

}

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to