[java.nio] StreamCorruptedException when deserializing objects
Hello everybody!
I made a messaging (chat) program using java.io where all the client-server communication is done by serializing small objects. Now I would like to covert the server side to the NIO concept and I'm already struck. I successfully pass objects to the client and the client deserializes them, but only the first one! When it try to read the second it fails with a StreamCorruptedException.
Here�s a sample (testing) code. In the server run() method I first serialize a string, then get its byte array from ByteArrayOutputStream and in the loop periodically send this byte array through the channel. On the client side I just read the deserialized object.
Server run():
public void run() {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject("abcdefgh");
byte[] objectArr = baos.toByteArray();
baos.close();
oos.close();
ByteBuffer buff = ByteBuffer.allocate(objectArr.length);
buff.put(objectArr);
buff.flip();
while(true) {
selector.select();
Set keys = selector.selectedKeys();
for (Object o : keys) {
SelectionKey key = (SelectionKey)o;
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.channel();
clientChannel = server.accept();
if (clientChannel == null)
continue;
clientChannel.configureBlocking(false);
SelectionKey clientKey = clientChannel.register(selector, SelectionKey.OP_WRITE);
} else if (key.isWritable()) {
SocketChannel client = (SocketChannel) key.channel();
if (buff.hasRemaining()) {
client.write(buff);
} else {
buff.clear();
buff.put(objectArr);
buff.flip();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
return;
} catch (IOException e) {
e.printStackTrace();
}Client run():
public void run() {
try {
soc = new Socket("localhost", 4000);
ObjectInputStream ois = new ObjectInputStream(soc.getInputStream());
while(true) {
Object d = ois.readObject();
System.out.println("data = " + d.toString());
} catch (Exception ex) {
ex.printStackTrace();
}At the second read I get a StreamCorruptedException.
Apart from this I would like some hints in how to implement the application with NIO. For example how can I tell the objects apart on the client side � should I send every time a byte array before the object, which tells the length of the next coming object? This is probably not a 100% bulletproof solution and presents additional data transfer?
Than you in advance!
OK, I found a solution but I don't like it, because I don't understand it.
The ObjectOutputStream adds a header (4 bytes) to the stream - if I send the array with those four bytes I get an StreamCorruptedException. If I send the array without the header I also get a StreamCorruptedException: "invalid stream header".
If I reconstruct the object, by calling ObjectOutputStream.writeObject() and get it's byte array from ByteArrayOutputStream.toByteArray(), every time I have to fill the ByteBuffer, then it works.
Here's the modified sending block, for the above example:
} else if (key.isWritable()) {
SocketChannel client = (SocketChannel) key.channel();
if (buff.hasRemaining()) {
client.write(buff);
} else {
//* ---- added code ---------
baos.reset();
oos.writeObject("abcdefgh");
objectArr = baos.toByteArray();
buff.clear();
buff.put(objectArr);
buff.flip();
} I really don't understand why I have to write the object in the object stream every time. I used ObjectOS and ByteArrayOS, to get the object byte array and then I thought I could forget about those streams and use this array to fill the ByteBuffer. What changes if I send the object through this process with every iteration (and how this harms speed - it's like sending everything twice)? If someone would explain this to me I would appreciate it much.
Similar Messages
-
StreamCorruptedException when deserializing from a DatagramPacket
Using jrockit 1.4.2_05 or jrockit 1.5.0_03 on Windows XP,I get some troubles when deserializing an object from a DatagramPacket received from a multicast socket. It seems that jrockit incorrectly read the object. Some fields are altered, and if the object is too long I get a StreamCorruptedException. When I run the receiver program with the Sun JDK, it works correctly.
I have a sender program and a receiver program that exchange objects on a multicast address. The sender serializes the object into a ByteArrayOutputStream. Then I retrieve the byte[] array and put it in a DatagramPacket that is sent into the multicast socket.
The receiver listens to the multicast socket, receive data into a datagram, retrieves the buffer (byte[]), wraps a ByteArrayInputStream from which it read the object.
Here are some traces I get:
At the sender:
1140261074833 SENDER - SENT name=(Annapurna);heigth=8091;history=Lachenal et herzog - 1950.
At the receiver:
1140261075082 RECEIVER - RECEIVED (7) name=(Annande ;heigth=8091;history=Lachenal et herzog - 1950.
With a longer (but still short !) message, I get:
At the sender:
1140261075333 SENDER - SENT name=(Everest );heigth=8848;history=Hillary : tensing 1953, tout le monde veut y aller, un vrai depotoir.
At the receiver:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Z)Ljava.lang.Object;(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Ljava.lang.Object;Ljava.io.ObjectStreamClass;)V(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Ljava.lang.Object;Ljava.io.ObjectStreamClass;)V(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Z)Ljava.lang.Object;(Unknown Source)
at java.io.ObjectInputStream.readObject0(Z)Ljava.lang.Object;(Unknown Source)
at java.io.ObjectInputStream.readObject()Ljava.lang.Object;(Unknown Source)
at grp.XCast.recv()Ljava.lang.Object;(XCast.java:44)
at grp.XCast.run()V(XCast.java:145)
at java.lang.Thread.run()V(Unknown Source)
at java.lang.Thread.startThreadFromVM(Ljava.lang.Thread;)V(Unknown Source)
Any idea where it comes from ?Hi,
A bit more on this topic.
I think I've got it.
My code looks like this:
- Class fields:
DatagramPacket rpack = null;
MulticastSocket xsock = new MulticastSocket(6789);
- Constructor:
byte[] rbuf = new byte[1024];
rpack = new DatagramPacket(rbuf, rbuf.length);
- Receive method:
while (some condition) {
xsock.receive(this.rpack);
With Sun JDK, this sequence works well. With jrockit I ave to change it like following to make it works:
while (some condition) {
rpack.setLength(1024);
xsock.receive(this.rpack);
Looking at the J2SDK API, I read that:
"The length of the packet is the number of bytes from the packet's data buffer that will be sent, or the number of bytes of the packet's data buffer that will be used for receiving data."
With jrockit, it seems that the socket receive method modify the length returned by the DatagramPacket getLength method, so that if a first receive read a small message, the next call to receive will truncate the data if the latter message is longer than the former.
Sun JDK seems to behave differently. The length seems to be based on the length of the buffer that was passed to the DatagramPacket constructor.
Have you got a clue of who's right ?
Or do I miss something else ?
Anyway, it's borrowing to have such different behaviour from a JVM to the next. -
Java.nio.BufferUnderflowException when send a idoc xml using sapjco3.jar
When I use sapjco3.jar to send a idoc
I have 2 IDOC ,one size is 200kb ,another is 336kb
the first one can send to sap correct, but the bigger one got Exception (java.nio.BufferUnderflowException)
If any thing I have to note when writing java code ? thanks ....
Exception:
send TXLOG-12918467-ToIDOC_20110513161427170_1125436.xml
send idoc D:\TXLOG-12918467-ToIDOC_20110513161427170_1125436.xml to SAP GateWay finished ,IDoc Tid is AC1140540B534DD1EAE61781
send TXLOG-12964100.xml
java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Buffer.java:486)
at java.nio.HeapCharBuffer.get(HeapCharBuffer.java:129)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.parse(DefaultIDocXMLParser.java:365)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:87)
at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:68)
at com.testrite.msg.dex.sender.SAPSender.sendMessage(SAPSender.java:61)
at com.testrite.msg.dex.sender.SAPSender.main(SAPSender.java:37)
My Code :
JCoDestination destination;
String dest = "BCE";
destination = JCoDestinationManager.getDestination(dest);
IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
String tid = destination.createTID();
IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
IDocDocumentList iDocList = processor.parse(iDocRepository, new BufferedInputStream(new FileInputStream("D:
TXLOG-12964100.xml"));
JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);hi,
it only works with idoc to idoc scenarios I think
when you send from SAP to SAP through XI
have a look at my weblog:
/people/michal.krawczyk2/blog/2005/11/21/xi-idoc-to-idoc-tunneling--how-fast-and-easy-can-you-get
Regards,
michal -
EOFException when deserializing object in if statement
Hi,
I'm a java newbie and I've got a program in which I've got an method to open a serialized file.
I am running into problems when determining the class of the object to be deserialized.
When the code is as such everything works fine:
ObjectInputStream ois;
protected void open() {
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
ois = new ObjectInputStream(new FileInputStream(file));
frameSeqA72 = (SeqA72) ois.readObject();
getContentPane().add(frameSeqA72);
frameSeqA72.setVisible(true);
ois.close();
catch (IOException ioe) {ioe.printStackTrace();}
catch (ClassNotFoundException cnfe) {}
}For some reason I don't understand everything changes when I use an if statement in the code as follows:
ObjectInputStream ois;
protected void open() {
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
ois = new ObjectInputStream(new FileInputStream(file));
if (ois.readObject() instanceof SeqA72) {
frameSeqA72 = (SeqA72) ois.readObject();
getContentPane().add(frameSeqA72);
frameSeqA72.setVisible(true);
ois.close();
catch (IOException ioe) {ioe.printStackTrace();}
catch (ClassNotFoundException cnfe) {}
}An EOFException is thrown when this method is called -
"EOFException at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267)..."
My serialized objects are of different classes so I want to determine the class before casting...so Im using "if (ois.readObject() instanceof SeqA72)".
Why would the first version work and the second throw an exception?
Am I going about this in a bad way?
CaseyYou're calling readObject twice, so it's trying to read two objects.
Read it once and save a reference in a variable. Use that variable, instead of calling readObject again. -
StreamCorruptedException when sending object
Hi,
I have successfully created an ObjectOutputStream on the server side of my application, along with an ObjectInputStream on the client side. I think my server sends objects correctly, but when my client tries to receive the objects, I get a StreamCorruptedException (my objects do implement Serializable). Does anyone know what would be the likely cause of this?It means that something unexpected was read or that an unexpected IOException occurred during ObjectInputStream.readObject().
Are you writing anything else to the ObjectOutputStream, e.g. with a writeInt() method?
Is there a detail member in the exception, & if so what does it say? -
Problems deserializing objects with Microsoft VM
I am getting a 30 second delay when deserializing objects: using Java 1.1.8 ObjectOutputStream.defaultReadObject() causes a 30 second delay. This only happens on Internet Explorer using the Microsoft Virtual Machine.Any ideas what's causing this delay?
HERE'S THE CODE ..
URLConnection con = servlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
ObjectOutputStream out = new ObjectOutputStream(SecurityToolkit.openOutputStream(con));
int numObjects = objs.length;
for (int x = 0; x < numObjects; x++) {
out.writeObject(objs[x]);
out.flush();
out.close();
InputStream inStream = SecurityToolkit.openInputStream(con);
ObjectInputStream in = new ObjectInputStream(inStream);
return in;
So .. this is the ObjectInputStream returned and objects are read off this stream. But reading hangs for 30 seconds and then starts reading again. -
StreamCorruptedException when trying to deserialize Object containing a Vec
Hello,
I am trying to send an Object containing an (currently empty) Vector with ObjectInput/Output-Streams.
But when deserializing the Object I always get a StreamCorruptedException: null.
As soon as I remove the Vector from the Object everything works as it should.
Anybody can tell me, what is happening here? I didn�t expect any trouble with the Vector since it implements Serializable and doesn�t contain anything, yet.
Thanks,
PhilippHello again.
to exclude problems with the HttpMessage-class I rewrote the applet-part to use an URLConnection. No you can see, what the HttpMessag-class does normally. It still raises the same error.
Here it is:
String argstring = URLEncoder.encode("command", "UTF-8") + "=" + URLEncoder.encode(command, "UTF-8");
URL url = new URL(codeBase, chatServer);
URLConnection con = url.openConnection();
// Prepare for both input and output
con.setDoInput(true);
con.setDoOutput(true);
// Turn off caching
con.setUseCaches(false);
// Work around a Netscape bug
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// Write the arguments as post data
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(argstring);
out.flush();
out.close();
InputStream in = con.getInputStream();
ObjectInputStream oin = new ObjectInputStream(in);
ChatCommandReply reply = (ChatCommandReply) oin.readObject();
} catch (Exception ex)
ex.printStackTrace();
BTW, the stackTrace looks like this:
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.byteshift.vobaclub.chat.client.Chatroom.sendCommand(Chatroom.java:217)
at com.byteshift.vobaclub.chat.client.Chatroom.stop(Chatroom.java:256)
at com.byteshift.vobaclub.chat.client.ChatApplet.stop(ChatApplet.java:72)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Regards,
Philipp -
I have an applet which calls a JSP to write data object to the db and then the
JSP sends back the updated data object. The writing part is ok but the response
is giving the following error. The data object is in a separate class which implements
Serialized.
Here's the code in the applet calling the JSP and the response from the JSP
URL server = null;
String urlConnectionString = "http://localhost:7001/isLoginValid.jsp";
try
server = new URL(urlConnectionString);
catch(MalformedURLException e)
System.out.println("URL exception: " + e );
// send request
ObjectInputStream response = null;
Object result = null;
try
URLConnection conn = server.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream request = new ObjectOutputStream(new
BufferedOutputStream(conn.getOutputStream()));
request.writeObject((Object)dvo);
request.flush();
request.close();
// get the result input stream
response = new ObjectInputStream(new BufferedInputStream
(conn.getInputStream()));
// read response back from the server
result = response.readObject();
if( result!=null && (result instanceof DataVO))
dvo = (DataVO)result;
String vo = dvo.printDataVO();
System.out.println("*DataVO*\n"+vo);
else
System.out.println("not an instanceof DataVO");
catch(IOException ignored)
System.out.println("Error in DataVO response");
ignored.printStackTrace();
Here's the code in the JSP sending the response back to the applet. The 'dvo'
object is the object which is serialized and has gets and sets for the diff. data
elements. When I print the 'dvo' before writing the object to outputStream it
prints the correct values for the data element.
// send response
response.setStatus(HttpServletResponse.SC_OK);
ObjectOutputStream outputStream = new ObjectOutputStream (new BufferedOutputStream
(response.getOutputStream()));
outputStream.writeObject(dvo);
outputStream.flush();
ERROR is as follows:
Error in DataVO response
java.io.StreamCorruptedException: InputStream does not contain a serialized object
at java/io/ObjectInputStream.readStreamHeader
at java/io/ObjectInputStream.<init>
What am I doing wrong?. Please respond soon. The applet is run on IIS and the
JSP in on weblogic 6.1. I'm not sure if that makes any difference.
I have an applet which calls a JSP to write data object to the db and then the
JSP sends back the updated data object. The writing part is ok but the response
is giving the following error. The data object is in a separate class which implements
Serialized.
Here's the code in the applet calling the JSP and the response from the JSP
URL server = null;
String urlConnectionString = "http://localhost:7001/isLoginValid.jsp";
try
server = new URL(urlConnectionString);
catch(MalformedURLException e)
System.out.println("URL exception: " + e );
// send request
ObjectInputStream response = null;
Object result = null;
try
URLConnection conn = server.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream request = new ObjectOutputStream(new
BufferedOutputStream(conn.getOutputStream()));
request.writeObject((Object)dvo);
request.flush();
request.close();
// get the result input stream
response = new ObjectInputStream(new BufferedInputStream
(conn.getInputStream()));
// read response back from the server
result = response.readObject();
if( result!=null && (result instanceof DataVO))
dvo = (DataVO)result;
String vo = dvo.printDataVO();
System.out.println("*DataVO*\n"+vo);
else
System.out.println("not an instanceof DataVO");
catch(IOException ignored)
System.out.println("Error in DataVO response");
ignored.printStackTrace();
Here's the code in the JSP sending the response back to the applet. The 'dvo'
object is the object which is serialized and has gets and sets for the diff. data
elements. When I print the 'dvo' before writing the object to outputStream it
prints the correct values for the data element.
// send response
response.setStatus(HttpServletResponse.SC_OK);
ObjectOutputStream outputStream = new ObjectOutputStream (new BufferedOutputStream
(response.getOutputStream()));
outputStream.writeObject(dvo);
outputStream.flush();
ERROR is as follows:
Error in DataVO response
java.io.StreamCorruptedException: InputStream does not contain a serialized object
at java/io/ObjectInputStream.readStreamHeader
at java/io/ObjectInputStream.<init>
What am I doing wrong?. Please respond soon. The applet is run on IIS and the
JSP in on weblogic 6.1. I'm not sure if that makes any difference.
-
Am running into following exception even after following all guidelines to implement POF. The main objective is to perform Distributed Bulk cache loading.
Oracle Coherence GE 3.7.1.10 <Error> (thread=Invocation:InvocationService, member=1): Failure to deserialize an Invocable object: java.io.StreamCorruptedException: unknown user type: 1001
java.io.StreamCorruptedException: unknown user type: 1001
at com.tangosol.io.pof.PofBufferReader.readAsObject(PofBufferReader.java:3312)
at com.tangosol.io.pof.PofBufferReader.readObject(PofBufferReader.java:2604)
at com.tangosol.io.pof.ConfigurablePofContext.deserialize(ConfigurablePofContext.java:371)
at com.tangosol.coherence.component.util.daemon.queueProcessor.Service.readObject(Service.CDB:1)
at com.tangosol.coherence.component.net.Message.readObject(Message.CDB:1)
at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.InvocationService$InvocationRequest.read(InvocationService.CDB:8)
at com.tangosol.coherence.component.util.daemon.queueProcessor.service.Grid.deserializeMessage(Grid.CDB:19)
at com.tangosol.coherence.component.util.daemon.queueProcessor.service.Grid.onNotify(Grid.CDB:31)
at com.tangosol.coherence.component.util.Daemon.run(Daemon.CDB:42)
at java.lang.Thread.run(Thread.java:662)
Following is the pof-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<pof-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-pof-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-pof-config/1.1/coherence-pof-config.xsd">
<user-type-list>
<include>coherence-pof-config.xml</include>
<user-type>
<type-id>1001</type-id>
<class-name>com.westgroup.coherence.bermuda.loader.DistributedLoaderAgent</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>1002</type-id>
<class-name>com.westgroup.coherence.bermuda.profile.lpa.LPACacheProfile</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>1003</type-id>
<class-name>com.westgroup.coherence.bermuda.profile.lpa.Address</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>1004</type-id>
<class-name>com.westgroup.coherence.bermuda.profile.lpa.Discipline</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
<user-type>
<type-id>1005</type-id>
<class-name>com.westgroup.coherence.bermuda.profile.lpa.Employment</class-name>
<serializer>
<class-name>com.tangosol.io.pof.PofAnnotationSerializer</class-name>
<init-params>
<init-param>
<param-type>int</param-type>
<param-value>{type-id}</param-value>
</init-param>
<init-param>
<param-type>java.lang.Class</param-type>
<param-value>{class}</param-value>
</init-param>
<init-param>
<param-type>boolean</param-type>
<param-value>true</param-value>
</init-param>
</init-params>
</serializer>
</user-type>
</user-type-list>
<allow-interfaces>true</allow-interfaces>
<allow-subclasses>true</allow-subclasses>
</pof-config>
cache-config.xml
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.oracle.com/coherence/coherence-cache-config"
xsi:schemaLocation="http://xmlns.oracle.com/coherence/coherence-cache-config http://xmlns.oracle.com/coherence/coherence-cache-config/1.1/coherence-cache-config.xsd">
<defaults>
<serializer>pof</serializer>
</defaults>
<caching-scheme-mapping>
<cache-mapping>
<cache-name>DistributedLPACache</cache-name>
<scheme-name>LPANewCache</scheme-name>
<init-params>
<init-param>
<param-name>back-size-limit</param-name>
<param-value>250MB</param-value>
</init-param>
</init-params>
</cache-mapping>
</caching-scheme-mapping>
<caching-schemes>
<!-- Distributed caching scheme. -->
<distributed-scheme>
<scheme-name>LPANewCache</scheme-name>
<service-name>HBaseLPACache</service-name>
<serializer>
<instance>
<class-name>com.tangosol.io.pof.ConfigurablePofContext</class-name>
<init-params>
<init-param>
<param-type>java.lang.String</param-type>
<param-value>pof-config.xml</param-value>
</init-param>
</init-params>
</instance>
</serializer>
<backing-map-scheme>
<read-write-backing-map-scheme>
<internal-cache-scheme>
<class-scheme>
<class-name>com.tangosol.util.ObservableHashMap</class-name>
</class-scheme>
</internal-cache-scheme>
<cachestore-scheme>
<class-scheme>
<class-name>com.westgroup.coherence.bermuda.profile.lpa.LPACacheProfile</class-name>
</class-scheme>
</cachestore-scheme>
<read-only>false</read-only>
<write-delay-seconds>0</write-delay-seconds>
</read-write-backing-map-scheme>
</backing-map-scheme>
<autostart>true</autostart>
</distributed-scheme>
<invocation-scheme>
<scheme-name>InvocationService</scheme-name>
<service-name>InvocationService</service-name>
<thread-count>5</thread-count>
<autostart>true</autostart>
</invocation-scheme>
</caching-schemes>
</cache-config>
DistributedLoaderAgent (user type 1001)
import java.io.IOException;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import org.apache.log4j.Logger;
import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofWriter;
import com.tangosol.io.pof.PortableObject;
import com.tangosol.io.pof.annotation.Portable;
import com.tangosol.io.pof.annotation.PortableProperty;
import com.tangosol.net.AbstractInvocable;
import com.tangosol.net.InvocationService;
@Portable
public class DistributedLoaderAgent extends AbstractInvocable implements PortableObject{
private static final long serialVersionUID = 10L;
private static Logger m_logger = Logger.getLogger(DistributedLoaderAgent.class);
@PortableProperty(0)
public String partDumpFileName = null;
public String getPartDumpFileName() {
return partDumpFileName;
public void setPartDumpFileName(String partDumpFileName) {
this.partDumpFileName = partDumpFileName;
public DistributedLoaderAgent(){
super();
m_logger.debug("Configuring this loader ");
public DistributedLoaderAgent(String partDumpFile){
super();
m_logger.debug("Configuring this loader to load dump file "+ partDumpFile);
partDumpFileName = partDumpFile;
@Override
public void init(InvocationService service) {
// TODO Auto-generated method stub
super.init(service);
@Override
public void run() {
// TODO Auto-generated method stub
try{
m_logger.debug("Invoked DistributedLoaderAgent");
MetadataTranslatorService service = new MetadataTranslatorService(false, "LPA");
m_logger.debug("Invoking service.loadLPACache");
service.loadLPACache(partDumpFileName);
}catch(Exception e){
m_logger.debug("Exception in DistributedLoaderAgent " + e.getMessage());
@Override
public void readExternal(PofReader arg0) throws IOException {
// TODO Auto-generated method stub
setPartDumpFileName(arg0.readString(0));
@Override
public void writeExternal(PofWriter arg0) throws IOException {
// TODO Auto-generated method stub
arg0.writeString(0, getPartDumpFileName());
Please assist.OK I have two suggestions.
1. Always create and flush the ObjectOutputStream before creating the ObjectInputStream.
2. Always close the output before you close the input. Actually once you close the output stream both the input stream and the socket are closed anyway so you can economize on this code. In the above you have out..writeObject() followed by input.close() followed by out.close(). Change this to out.writeObject() followed by out.close(). It may be that something needed flushing and the input.close() prevented the flush from happening. -
Getting "java.io.StreamCorruptedException: invalid stream header"
When creating a self made Stream (MacInputStream) and then using an ObjectInputStream over it to read Objects from a socket, I get this error:
java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:764)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:277)
at TServidor.run(TServidor.java:32)
Is there any special feature that the "self-made" streams have to implement to be possible to use ObjectInput streams over them :P ?
Here is the MacInputStream.java code:
import java.io.Closeable;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import javax.crypto.Mac;
public class MacInputStream extends FilterInputStream implements Closeable{
private Mac mac; // algorithm
private byte [] mmac; //message MAC
private boolean FIRST_TIME;
public MacInputStream(InputStream is,Mac mac) {
super(is);
this.mac=mac;
FIRST_TIME=true;
public int read() throws IOException{
if(FIRST_TIME){
mmac = new byte [mac.getMacLength()];
super.read(mmac);
if(super.in.available()==0){
FIRST_TIME=true;
return -1;
int rbyte = super.in.read();
FIRST_TIME=false;
mac.update((byte)rbyte);
System.out.println("available: "+super.in.available());
if(super.in.available()==0){
byte [] macres =mac.doFinal();
System.out.println("message MAC: "+new String(mmac));
System.out.println("calculated MAC: "+new String(macres));
if(!Arrays.equals(macres, mmac)){
throw new IOException("violated integrity");
return rbyte;
public int read(byte [] b) throws IOException{
if(FIRST_TIME){
mmac = new byte [mac.getMacLength()];
super.in.read(mmac);
if(super.available()==0){
FIRST_TIME=true;
return -1;
int rbytes = super.in.read(b);
FIRST_TIME=false;
mac.update(b);
if(super.available()==0){
byte [] macres =mac.doFinal();
if(!Arrays.equals(macres, mmac)){
throw new IOException("violated integrity");
return rbytes;
}And here is the "main" function where the exception gets thrown:
public void run() {
try {
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
Mac mac = Mac.getInstance("HmacMD5");
Key key = KeyGenerator.getInstance("HmacMD5").generateKey();
oos.writeObject(key);
oos.flush();
mac.init(key);
ObjectInputStream cis = new ObjectInputStream(new MacInputStream(s.getInputStream(),mac));
String test;
try {
while (true) {
test = (String)cis.readObject();
System.out.println(ct + " : " + test);
} catch (EOFException e) {
System.out.println("["+ct + "]");
} finally {
if (cis!=null) cis.close();
if (oos!=null) oos.close();
} catch (Exception e) {
e.printStackTrace();
}It's exactly in the line: ObjectInputStream cis = new ObjectInputStream(new MacInputStream(s.getInputStream(),mac));Any ideas?
I'm starting to desperate :P(a) I still don't see where you are writing the MAC that you're reading. You're reading something, but it's all or part of the Object stream header I described above, which is why ObjectInputStream' constructor is throwing that exception.
(b) You don't need to override read(byte[] b) when you extend FilterInputStream, but you do need to override read(byte[] b, int offset, int length), and you need to do it like this:
public int read(byte[] buffer, int offset, int length) throws IOException
int count = 0;
do
int c = read();
if (c < 0)
break;
buffer[offset+count++] = (byte)c;
} while (count < length && available() > 0);
return count > 0 ? count : -1;
}This way the read() method gets to see every byte that's read and to do its MAC thing or whatever it does. The above is one of only two correct uses of available() in existence: it ensures that you only block once while reading, which is the correct behaviour e.g. on a network. -
Socket disconnection event not being reveived java.nio
Hi All
My question is as below
i am using java.nio package + win 2K + jdk1.4.1_02
I have a Server accepting socket connection and is configured
in Nonblocking mode.
-The server receives (selector) events (accept event)
when a client requests for a connection .
-The server is getting read and write events when
client wants to read and write
-The server gets a read event when the client disconnects
My only problem is that the Server is not getting any events
when the client disconnect due to network break down
for EX when the network connection breaks the client disconnects
but the server selector is not getting any events
I am storing the client Socket objects in a Hash Table and
if i execute the .isConnected() method it returns true
even though the the client is down
The only work around is to try to read and write on the socket
i get an IO exception and am able to make out that the
client is disconnected
But this is not desirable for my client
as client modification to give any kind of echo is not possible and
a read /write event will cause problem.
Can any one tell how to detect client side disconnection
with out Read and Write on the client socket
I get an socket Exception when the socket is in Blocking IO and is blocked on read/ write but I am facing this problem in NonBlockingIO
Regards
Avinashint ct = read.selectNow();
if (ct > 0)
Set readyKeys = read.selectedKeys();
Iterator i = readyKeys.iterator();
while (i.hasNext())
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (key.isReadable())
SelectableChannel nextReady = (SelectableChannel) key.channel();
SocketChannel sc = (SocketChannel) key.channel();
int rd = sc.read(buffer);
// Link is dead
if (rd == -1)
key.cancel();
key.attach(null);
nextReady.close();
continue;
// Process your data
catch (Exception e)
e.printStackTrace(); -
Serializing/Deserializing Objects..Urgent !!!
Hello Everyone,
Out of the blue I got an exception like
java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
I am implementing "Applet to Servlet communication".
Actually i am trying to deserialize the object which was passed to the servlet.But when i read it back i am geting the above exception.
Can anyone throw some light on it?
Her is my code
APPLET CODE:
private void interactWithServlet() {
WBObject result = null;
try {
// Create an object we can use to communicate with the servlet
URL servletURL = new URL(sURL);
URLConnection servletConnection = servletURL.openConnection();
servletConnection.setDoOutput(true);
servletConnection.setUseCaches(false);
servletConnection.setRequestProperty("Content-Type", "application/octet-stream;charset=utf-8");
ObjectOutputStream request = new ObjectOutputStream(
new BufferedOutputStream(servletConnection.getOutputStream()));
WBObject wbObj=m_whiteBoardComponent.getDesignPanel().getWBObject();
int size=wbObj.getChildren().size();
for(int i=0;i<size;i++){
PickObject pick=(PickObject)wbObj.getChildren().get(i);
System.out.println("the pick object name is"+pick.getName());
int count=pick.getChildren().size();
for(int j=0;j<count;j++){
ItemObject item=(ItemObject)pick.getChildren().get(j);
System.out.println("the item object name is"+item.getName());
request.writeObject(wbObj);
request.flush();
request.close();
ObjectInputStream response = new ObjectInputStream(
new BufferedInputStream(servletConnection.getInputStream()));
result = (WBObject)response.readObject();
System.out.println("The object is"+(result instanceof WBObject));
response.close();
} catch (Exception e) {
e.printStackTrace();
SERVLET CODE:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
WBObject o = null;
ObjectInputStream inputStream = new ObjectInputStream(
new BufferedInputStream(req.getInputStream()));
try {
o = (WBObject)inputStream.readObject();
inputStream.close();
} catch( ClassNotFoundException ex ) {
ex.printStackTrace();
// send response
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/octet-stream;charset=utf-8");
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(resp.getOutputStream()));
oos.writeObject(o);
oos.close();
Best Regards
Ashishclient
URLConnection servletConnection = servletURL.openConnection();try
HttpURLConnection servletConnection = (HttpURLConnection)servletURL.openConnection();server
// send response
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/octet-stream;charset=utf-8");
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(resp.getOutputStream()));
oos.writeObject(o);
oos.close();try
// send response
//resp.setStatus(HttpServletResponse.SC_OK);
//resp.setContentType("application/octet-stream;charset=utf-8");
ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(resp.getOutputStream()));
oos.writeObject(o);
oos.flush();
oos.close(); -
Any java.nio guru out there ?
Hi help,
I'm studying new java.io classes. I've compiled and learnt
a basic server like this...
import java.io.*;
import java.net.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
public class Server {
private static int port = 9999;
public static void main(String args[])
throws Exception {
Selector selector = Selector.open();
ServerSocketChannel channel =
ServerSocketChannel.open();
channel.configureBlocking(false);
InetSocketAddress isa = new InetSocketAddress(port);
channel.socket().bind(isa);
// Register interest in when connection
channel.register(selector, SelectionKey.OP_ACCEPT);
// Wait for something of interest to happen
while (selector.select() > 0) {
// Get set of ready objects
Set readyKeys = selector.selectedKeys();
Iterator readyItor = readyKeys.iterator();
// Walk through set
while (readyItor.hasNext()) {
// Get key from set
SelectionKey key =
(SelectionKey)readyItor.next();
// Remove current entry
readyItor.remove();
if (key.isAcceptable()) {
// Get channel
ServerSocketChannel keyChannel =
(ServerSocketChannel)key.channel();
// Get server socket
ServerSocket serverSocket = keyChannel.socket();
// Accept request
Socket socket = serverSocket.accept();
// Return canned message
PrintWriter out = new PrintWriter
(socket.getOutputStream(), true);
out.println("Hello, NIO");
out.close();
} else {
System.err.println("Ooops");
// Never ends
} now I'd like to write a Proxy Server. In other
words the Server must
1. listen for a request
2. forward the request to another server
3. listen its reply
4. forward it back to the original caller.
I've tried to modify the basic class but with no success....
unfortunately I'm not that smart with these new Apis.
anybody can give me some guidelines about how should I
struct it ?
Thanks a lot
FrancescoNot a guru but I'll offer some suggestions. Are these persistent connections to the proxy, or is a connection made per request? You'll probably do things a little bit differently in the two different cases.
In general though, you can have one thread running a request dispatcher which accepts incoming connections and puts read ready channels on a queue. Another thread(or threads) can take requests off the queue and process them. Also, when you accept new connections, I think its better to do it through channels, rather than work directly with the socket. Something like this:
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
SelectionKey newReadKey = clientChannel.register(selector, SelectionKey.OP_READ);
Where are you getting into problems? -
Java.io.StreamCorruptedException: invalid stream header
I am having a problem with sending two objects (over a socket). I have read in other posts that this could be due to trying to receive incompatible data types but my applications work fine if I send my objects synchronously rather than asynchronously.
I will try my best to describe what my problem is as my code is very long.
I have a server and a client application (2 apps). Multiple clients connect to the server and send their details (as an object) to the server. The server then amends the object (adds some more data) and sends it back to the clients. Both the SendObject and ReceiveObject class are threads and I have created a Listener (within the client) that activates when an object is received (asynchronous communication). The Listener method looks to see if the event is an instance of a particular class and casts is as appropriate (as per below).
public void receivedObject(ReceivedObjectEvent e) {
ReceiveObjectThread obj = (ReceiveObjectThread) e.getObject();
if(obj.getObject() instanceof Player) {
thePlayer = (Player) obj.getObject();
theTable.setHandData(thePlayer.getHand());
if(obj.getObject() instanceof GameData) {
gameData = (GameData) obj.getObject();
theTable.setPlayerList(gameData.getOpponents());
}The objects that are passed between applications both implement Serializable.
This all works fine synchronously object passing. However, if I try and spawn two sendObject threads within the server and the corresponding two receive threads within the client and wait for the Listener to activate (asynchronously) I get the following error:
java.io.StreamCorruptedException: invalid stream header: 00057372
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at ReceiveObjectThread.run(ReceiveObjectThread.java:84)
java.io.StreamCorruptedException: invalid stream header: ACED0006
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at ReceiveObjectThread.run(ReceiveObjectThread.java:84)
I am sure that this problem is due to my limited knowledge on socket and data transfer. Therefore any help on this one will be gratefully received.Hello ejp, your reply is very much appreciated.
If I explain how I have implemented my sockets you may be able to see where I wrong.
When a player connects, the client sends the server a �player� object. The server receives the �player� object and passes the socket from which it connected (within the server) to a socket property within the �player� class. Whenever the server needs to send an object to that client (player), it sends the output stream from the socket property within that �player� object. ( player.getSocket().getOutputStream() ).
Below is the code from the �SendObjectThread� class.
* This class allows an object to be passed over a Socket
* @author Harold Clements
* @version 1.0.1 12-Jun-2007 (12-Jul-2007)
//http://www.seasite.niu.edu/cs580java/Object_Serialization.html
public class SendObjectThread extends Thread {
private OutputStream out;
private Object obj;
* This constructor allows the user to passes the two parameters for transmitting.
* @param out The data stream that the object is going to be sent to.
* @param obj The object to be sent.
public SendObjectThread(OutputStream out, Object obj) {
this.out = out;
this.obj = obj;
* The main thread
public void run() {
try {
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(obj);
objOut.flush();
} catch (IOException e) {
e.printStackTrace();
}The client only has one socket which is defined when the client first makes a connection with the server. The �getOutputStream()� and �getInputStream()� are used for all communication from the client.
Is this what you described in your first option?
The funny thing about it all is if I create a new �receiveObjectTread� and wait for that to finish, then create another �receiveObjectTread� both objects in question (Player and GameData) are received correctly and the application works. I only have the problem when I set both threads off and leave it for the �ReceivedObjectEvent� listener to pick them up and cast them (as per my first post).
Thanks again for your help,
Harold Clements -
StreamCorruptedException when reading from an ObjectInputStream
Hello folks!
I hope there's someone out there who can help me.
I try to write a client-server-communication (via sockets). When starting the application I get the following exception:
java.io.StreamCorruptedException: Type code out of range, is -84
at java.io.ObjectInputStream.peekCode(Unknown Source)
at java.io.ObjectInputStream.refill(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.sap.swf.protegecontrol.mcd.communication.StateTestClient.connect(StateTestClient.java:86)
at com.sap.swf.protegecontrol.mcd.communication.StateTestClient.run(StateTestClient.java:42)
at java.lang.Thread.run(Unknown Source)
I tried several suggestions from several other threads, but nothing worked for me.
Here's the excerpt of my coding:
public void connect() {
while (newConnectionRequested) {
listening = true;
Socket client = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Object input = null;
try {
client = new Socket(this.server, this.port);
oos = new ObjectOutputStream(client.getOutputStream());
catch (Exception e) {
e.printStackTrace();
System.exit(1);
System.out.println("New connection requested by client");
try {
oos.writeObject(new StateRequestInfo());
oos.flush();
catch (Exception e) {
e.printStackTrace();
try {
ois = new ObjectInputStream(client.getInputStream());
catch (Exception e) {
e.printStackTrace();
while (listening) {
try {
input = ois.readObject();
catch (Exception e) {
e.printStackTrace();
System.exit(0);
if (input != null) {
Display display = Mcd.getDisplay();
if (input instanceof String) {
System.out.println((String)input);
if (input instanceof StateInfo) {
this.state = (StateInfo)input;
if (display != null) {
display.syncExec(new Runnable() {
public void run() {
Mcd.getGUIInstance().setState(state);
packagesReceived++;
if (packagesReceived == this.END_VAL) {
listening = false;
packagesReceived = 0;
try {
oos.close();
ois.close();
client.close();
catch (Exception e) {
e.printStackTrace();
System.out.println("Connection terminated by client");
toLive++;
if (toLive == this.END_APP) {
newConnectionRequested = false;
System.out.println("Client loop ended!");
Comment: The method opens a new client socket, connects to the server and sends a StateRequestInfo-object to inform the server which type of communication is preferred. The "state"-communication should work as follows: the server sends StateInfo-objects until the client closes (when received a certain amount of packages) and re-opens connection -
this runs in an endless loop in a separate threaad.
The State-Info Object is quite large, because it contains a Vector which contains multiple objects of state-information concerning another part of the application. All self-written objects are serializable.
I hope that's enough info. Thanks in advance.The solution for me was to create a new
ObjectInputStream for each read, which will then
expect:
header | bob | header | dougThanks for the hint, but I tried this already and it didn't fix anything.
An alternative is to skip 4 bytes (the length of the
header) after each read, but that may not work well
with socket streams.This can't be the solution because the StreamCorruptedException is also thrown when receiving the first object - that's why I think skipping the header after receiving the first StateInfo wouldn't fix anything.
But thanks for your efforts!
By the way, that's an excerpt of the server-code:
public void run() {
ObjectOutputStream out = null;
long currtime;
while (sending) {
out = null;
try {
out = new ObjectOutputStream(client.getOutputStream());
catch (Exception e) {
// e.printStackTrace();
if (out == null) {
System.out.println("Connection terminated by client");
break;
currtime = System.currentTimeMillis();
if (currtime > (reftime + this.TIME_INC)) {
System.out.println("Sending data...");
try{
out.writeObject(theManagerInfo.getStateManager().getStateInfo());
out.flush();
catch(Exception e){
reftime = currtime;
try {
if(out != null){
out.close();
client.close();
catch (Exception e) {
e.printStackTrace();
Comment: the server runs in a separate thread and sends periodically StateInfo-Objects to the client that requestet the socket communication.
Having received a certain amount of StateInfos the client automatically curts the link and requests a new communication
Maybe you are looking for
-
Enum in a Typedef Reverts to Default (LabVIEW 2009)
When I add additional items to a typedef'd enum, constants of that enum on some BD's revert back to the default zero index enum value. In the past (8.6.1 and previous), adding items to an enum in no way would ever change constants that were already d
-
Problem with brand new 55" Dynex DX-55L150A11
The audio doesn't work when we go through HDMI. Have gone through all 4 inputs and used 3 different HDMI cables. Is there some switch or menu that needs to activate audio that I'm missing, or do I really need to go return this so I can get the same b
-
Dear Sir or madam: I have collected a lot of electroencephalogram data, which looks like a continuous sine wave plus a noise signal. what i want to do now is to fit the data with the bilinear model as the attached jpg file. the problem is the paramet
-
Ok, so yesterday I paid for a Creative cloud membership and had (attempted) to download Photoshop, Media Encoder, and Flash Professional.. Photoshop downloaded but the other two did not show the downloaded in the Creative Cloud Desktop app, showing e
-
Power Light flashes when switching on my Mac mini
I recently experienced a strange behavior of my Intel Mac mini (Core solo CPU): After switching on the Mac I do not hear the music clip but instead the power light at the front flashes. It is a different kind of flashing than when the Mac mini is in