Bytebuffer to byte array and Object streams

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?
 
T

Tom Hawtin

Jordi said:
I am trying to send objects through a bytebuffer and have a server
that uses NIO for this, and a threaded client.
java.io.StreamCorruptedException: invalid stream header

Okay, so you've corrupted the data. I suggest you break the problem
down. Do you get any data at all? Have you positioned the buffer
correctly (flip, or whatever)? What did you send and what do you
receive? Try it with some simpler data? Do you have a complete set of
unit tests? Etc.
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;
}

Why write both of these when the second, simpler implementation will
always work and presumably be quicker.

Tom Hawtin
 
J

Jordi

Thanks Tom.

I will do all you say, but please, would you mind telling me if the
aArray method (or the toArray, if aArray doesn't) is correct to turn a
ByteBuffer into a byte array ?

And if you could know some resource related to how to do this
(transmit objects thru ByteBuffer) I will appreciate if you tell me.

I searched the web for near a week and can't find any.

Thanks

Jordi
 
T

Tom Hawtin

Jordi said:
Thanks Tom.

I will do all you say, but please, would you mind telling me if the
aArray method (or the toArray, if aArray doesn't) is correct to turn a
ByteBuffer into a byte array ?

The array method unwraps a byte[] from a ByteBuffer. It is only valid
ByteBuffers that wrap byte arrays, i.e. those created with allocate or
wrap. ByteBuffer created with allocateDirect use memory outside of the
Java heap and therefore have no backing array (the array method will
throw an exception).

For a wrapped ByteBuffer, a bulk get operation will do much the same
thing as array() followed by a copy of that array. However, array() wont
work with direct allocated buffers. Therefore use the bulk get
operations, and it wont matter what type of ByteBuffer you are dealing with.

Tom Hawtin
 
J

Jordi

Thanks Tom

Jordi said:
Thanks Tom.
I will do all you say, but please, would you mind telling me if the
aArray method (or the toArray, if aArray doesn't) is correct to turn a
ByteBuffer into a byte array ?

The array method unwraps a byte[] from a ByteBuffer. It is only valid
ByteBuffers that wrap byte arrays, i.e. those created with allocate or
wrap. ByteBuffer created with allocateDirect use memory outside of the
Java heap and therefore have no backing array (the array method will
throw an exception).

For a wrapped ByteBuffer, a bulk get operation will do much the same
thing as array() followed by a copy of that array. However, array() wont
work with direct allocated buffers. Therefore use the bulk get
operations, and it wont matter what type of ByteBuffer you are dealing with.

Tom Hawtin
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top