J
Jordi
Hello
I am trying to send objects through a bytebuffer and have a server
that uses NIO for this, and a threaded client.
The clients sents the object ok, but the server throws this exception:
java.io.StreamCorruptedException: invalid stream header
In the function that calls the method to read the object sent.
Dato dato = new Dato();
ByteBuffer bb = ByteBuffer.allocate(TAMANYO_BUFFER);
....
dato = leerDeBytes(aArray(bb));
The class sent is an object of type Dato, wich is my custom object.
This is the class:
/*
* Dato.java
*
*/
package hiperia.Browser3d;
import java.io.Serializable;
public class Dato implements Serializable {
public String textoChat = null;
public float f = 0.0f;
// constructor of class Dato
public Dato() {
}
}
And these are the functions called in the server that send the
exception:
public static byte [] convertirABytes (Object objeto) throws
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject (objeto);
os.flush();
os.close();
byte [] array = baos.toByteArray();
baos.close();
return array;
}
private byte[] toArray(ByteBuffer buffer) {
byte[] array = new byte[buffer.limit() - buffer.position()];
if (buffer.hasArray()) {
int offset = buffer.arrayOffset();
byte[] bufferArray = buffer.array();
System.arraycopy(bufferArray, offset, array, 0, array.length);
return array;
} else {
buffer.get(array);
return array;
}
}
private byte[] aArray(ByteBuffer buffer) {
// Retrieve all bytes in the buffer
buffer.clear();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes, 0, bytes.length);
return bytes;
}
public static Dato leerDeBytes(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bs= new ByteArrayInputStream(bytes); //
bytes es el byte[]
ObjectInputStream is = new ObjectInputStream(bs);
Dato objeto = (Dato)is.readObject();
is.close();
bs.close();
return objeto;
}
Can someone help with this?
I am trying to send objects through a bytebuffer and have a server
that uses NIO for this, and a threaded client.
The clients sents the object ok, but the server throws this exception:
java.io.StreamCorruptedException: invalid stream header
In the function that calls the method to read the object sent.
Dato dato = new Dato();
ByteBuffer bb = ByteBuffer.allocate(TAMANYO_BUFFER);
....
dato = leerDeBytes(aArray(bb));
The class sent is an object of type Dato, wich is my custom object.
This is the class:
/*
* Dato.java
*
*/
package hiperia.Browser3d;
import java.io.Serializable;
public class Dato implements Serializable {
public String textoChat = null;
public float f = 0.0f;
// constructor of class Dato
public Dato() {
}
}
And these are the functions called in the server that send the
exception:
public static byte [] convertirABytes (Object objeto) throws
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(baos);
os.writeObject (objeto);
os.flush();
os.close();
byte [] array = baos.toByteArray();
baos.close();
return array;
}
private byte[] toArray(ByteBuffer buffer) {
byte[] array = new byte[buffer.limit() - buffer.position()];
if (buffer.hasArray()) {
int offset = buffer.arrayOffset();
byte[] bufferArray = buffer.array();
System.arraycopy(bufferArray, offset, array, 0, array.length);
return array;
} else {
buffer.get(array);
return array;
}
}
private byte[] aArray(ByteBuffer buffer) {
// Retrieve all bytes in the buffer
buffer.clear();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes, 0, bytes.length);
return bytes;
}
public static Dato leerDeBytes(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bs= new ByteArrayInputStream(bytes); //
bytes es el byte[]
ObjectInputStream is = new ObjectInputStream(bs);
Dato objeto = (Dato)is.readObject();
is.close();
bs.close();
return objeto;
}
Can someone help with this?