I came across this code from Apache a while back while investing this
problem. I never actually tried them. Perhaps you can give the classes a
whirl and let us know?
http://xml.apache.org/xerces2-j/samples-socket.html
Brian Franklin
-----Original Message-----
From: Malachi de Aelfweald [mailto:malachid_at_temporal-wave.com]
Sent: Friday, August 22, 2003 12:43 PM
To: users_at_jaxb.dev.java.net
Subject: RE: RE: Re: Unmarshalling via InputStream
If anyone is interested, attached are my multiplexed streams that seem
to work. Key note is that you have to manually call .finish() on both
streams since .close() has to be blocked.
------------------------------------------------------------------------
---
Example Usage:
try{
System.out.println("Creating
MultiplexedStreams");
MultiplexedInputStream mis = new
MultiplexedInputStream(clientSocket.getInputStream());
MultiplexedOutputStream mos = new
MultiplexedOutputStream(clientSocket.getOutputStream());
System.out.println("reading taskman from the
socket");
Taskman taskman = (Taskman)u.unmarshal(mis);
mis.resetTransmissionBlock();
System.out.println("Sending back to client");
m.marshal(taskman, mos);
mos.endTransmissionBlock();
System.out.println("closing socket");
mis.finish();
mos.finish();
clientSocket.close();
System.out.println("done");
}catch(Exception e){
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}
------------------------------------------------------------------------
---
package com.temporalwave.io;
import java.io.*;
public class MultiplexedOutputStream
extends FilterOutputStream
{
public static int ETB = 23;
public MultiplexedOutputStream(OutputStream out)
{
super(out);
}
public void endTransmissionBlock()
throws IOException
{
out.write(ETB);
}
public void close()
throws IOException
{
// System.out.println(this.getClass().getName() + " is
blocking CLOSE!");
}
public void finish()
throws IOException
{
out.close();
}
}
------------------------------------------------------------------------
---
package com.temporalwave.io;
import java.io.*;
public class MultiplexedInputStream
extends FilterInputStream
{
public static int EOS = -1;
public static int ETB = 23;
protected boolean blockEnded;
public MultiplexedInputStream(InputStream in)
{
super(new PushbackInputStream(in));
blockEnded = false;
}
public int available()
throws IOException
{
if(blockEnded) return 0;
int avail = in.available();
if(avail <= 0)
return avail;
/**
* Now verify no ETB in that section
*/
byte[] buf = new byte[avail];
in.read(buf);
((PushbackInputStream)in).unread(buf);
for(int pos=0; pos<avail; pos++)
{
if(buf[pos] == ETB)
return pos-1;
}
return avail;
}
public int read()
throws IOException
{
if(blockEnded) return -1;
int readByte = in.read();
if((readByte == ETB) || (readByte == EOS))
return endTransmissionBlock();
return readByte;
}
public int read(byte[] b, int off, int len)
throws IOException
{
if(blockEnded) return -1;
byte[] buf = new byte[len];
int numRead = in.read(buf, 0, len);
for(int pos=0; pos<numRead; pos++)
{
if(buf[pos] == ETB)
{
endTransmissionBlock();
return pos-1;
}
b[(off+pos)] = buf[pos];
}
return numRead;
}
public long skip(long n)
throws IOException
{
if(blockEnded) return 0;
for(long pos=0; pos<n; pos++)
{
int readByte = read();
if(readByte == -1)
return pos-1;
}
return n;
}
public boolean transmissionBlockHasEnded(){return blockEnded;}
public void resetTransmissionBlock(){blockEnded = false;}
protected int endTransmissionBlock()
{
blockEnded = true;
return -1;
}
public void close()
throws IOException
{
// System.out.println(this.getClass().getName() + " is
blocking CLOSE!");
}
public void finish()
throws IOException
{
in.close();
}
}
------------------------------------------------------------------------
---
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net