XMLDecoder hangs when unmarshalling from a stream


L

Linda Jowers

Tested with Xerces and Java 6 and both hang when deserializing from
a stream (actually, its not "serialization" as in Java Serializable
but, rather XMLDecoder which is a Java Bean thing where it writes
itself to XML and from XML). This works fine for a byte array but
fails for a socket. Basically, the data is received and parsed Java 6
classes but the call to readObject hangs until the socket is closed.

XMLDecoder serIn = new XMLDecoder( in1 ); // Java Beans are
requrieed to have getters/setters, are not supported in Android source
base, and have networking issues.
mhold = (MessageHolder) serIn.readObject();

So, short question is has anyone figured out how to maker
XMLDecoder's readObject method work with socket streams?

For now we are manually scanning the XML or the end tag and then
tossing a bytearray and letting the XMLDecoder work on that. It sucks,
but we don't have time to debug/rewrite the underlying library code.

Thanks!
TimJowers
Reference: some example code:
----- Example client which sends the data. Notice if you step through
the data will not be parsed even after the "flush()". It only gets
parsed once the socket is closed. Obviously not a workable solution
for a network app. --------------------------
package org.fonepays.Messages.networking.ssl;

import java.beans.XMLEncoder;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io_OutputStream;
import java.io_OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.security.cert.X509Certificate;

import javax.net.ssl.*;

import org.fonepays.Messages.MessageConst;
import org.fonepays.Messages.MessageHolder;
import org.fonepays.Messages.PhoneLogin;
import org.fonepays.Messages.Request;

public class SunClient {

public static void main( String []a) {
int port = 9999;
SSLServerSocket server;
try {
//SSLSocketFactory factory = (SSLSocketFactory)
SSLSocketFactory.getDefault();
SSLSocketFactory factory = NaiveTrustManager.getSocketFactory();
SSLSocket client = (SSLSocket) factory.createSocket();
// auto-connects iif we pass in server endpoint info
// SSLSocket client = (SSLSocket) factory.createSocket
("localhost", port);

SocketAddress endpoint = new InetSocketAddress("localhost",
port );
client.connect(endpoint);
System.out.println( "Client connected via SSL" );
System.out.println( "Connected socket " +
client.getInetAddress().getHostAddress() );

SSLSession session = client.getSession();
for( Object ob : session.getPeerCertificates() )
System.out.println( "Cert: " + ob.getClass().getName() );

OutputStream outputstream = client.getOutputStream();
// test sending a Java Bean via XML encoding. Seems to
hang when Xerces and even the Java 6 libraries try to unmarshall the
object.
PhoneLogin phlog = new PhoneLogin();
phlog.setPhoneNumber( "8035551212" );
phlog.userName = "alex";
phlog.setPassword("passwoooooord");
XMLEncoder serOut;
MessageHolder mhold;
mhold = new MessageHolder();
mhold.message = (Request) phlog;
mhold.type = MessageConst.LOGIN_TERMINAL;

// dump that to stdout for review too
System.out.println();
System.out.println();
System.out.println("Here is the MessageHolder object:");
serOut = new XMLEncoder( outputstream );
serOut.writeObject( mhold );
serOut.flush();


OutputStreamWriter outputstreamwriter = new
OutputStreamWriter(outputstream);
BufferedWriter bufferedwriter = new BufferedWriter
(outputstreamwriter);
String string = "Hi from this client";
bufferedwriter.write(string + '\n');
bufferedwriter.flush();

} catch(Exception e) {
e.printStackTrace();
}
}
}



----- Example Server.
--------------------------------------------------------------------------------------------------------
package org.fonepays.Messages.networking.ssl;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

import org.fonepays.Messages.MessageHolder;
import org.fonepays.Messages.networking.JavaSpecificXMLMarshaller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public
class EchoServer {
public
static
void
main(String[] arstring) {
try {
// -Djavax.net.ssl.keyStore=cacerts -
Djavax.net.ssl.keyStorePassword=changeit
//System.setProperty("javax.net.ssl.trustStore","cacerts");
System.setProperty("javax.net.ssl.keyStore","cacerts");
System.setProperty
("javax.net.ssl.keyStorePassword","changeit");

SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory)
SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =
(SSLServerSocket)
sslserversocketfactory.createServerSocket(9999);
SSLSocket sslsocket = (SSLSocket) sslserversocket.accept
();

InputStream inputstream = sslsocket.getInputStream();

MessageHolder mhold =
JavaSpecificXMLMarshaller.receiveJavaBeanXML(inputstream);
System.out.println( "Received an object via Java Bean XML
serialization/unmarshalling " + mhold.toString() );
InputStreamReader inputstreamreader = new InputStreamReader
(inputstream);
BufferedReader bufferedreader = new BufferedReader
(inputstreamreader);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println(string);
System.out.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}


--- Method to unserialize (decode/unmarshall) -------------
public static MessageHolder receiveJavaBeanXML(InputStream in1) {
MessageHolder mhold;
XMLDecoder serIn = new XMLDecoder( in1 ); // Java Beans are requried
to have getters/setters, are not supported in Android source base, and
have networking issues.
mhold = (MessageHolder) serIn.readObject();
System.out.println( "Got an XML Java bean message of type: " +
mhold.type + " containing a " + mhold.getMessage().getClass().getName
() );
XMLEncoder serOut = new XMLEncoder( System.out );
serOut.writeObject( mhold );
serOut.flush();
return mhold;
}
 
Ad

Advertisements

M

Mike Amling

Linda said:
Tested with Xerces and Java 6 and both hang when deserializing from
a stream (actually, its not "serialization" as in Java Serializable
but, rather XMLDecoder which is a Java Bean thing where it writes
itself to XML and from XML). This works fine for a byte array but
fails for a socket. Basically, the data is received and parsed Java 6
classes but the call to readObject hangs until the socket is closed.

Is your XMLDecoder waiting for more data? Does it need an
end-of-file? Is there a way to know when all of the data has arrived?
If Yes to all the above, then, as a workaround, you could try using a
FilterInputStream subclass that provides an artificial end of file.
I had a similar problem with an XML parser that wanted an end-of-file
from a socket, and that's how I did it.

--Mike Amling
 

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

Top