Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Bytebuffer to byte array and Object streams

Thread view: 
Jordi - 22 Apr 2007 18:38 GMT
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?
Tom Hawtin - 22 Apr 2007 21:04 GMT
> 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();
[quoted text clipped - 6 lines]
>             return array;
>         }

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

Tom Hawtin
Jordi - 23 Apr 2007 19:13 GMT
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
Tom Hawtin - 23 Apr 2007 20:25 GMT
> 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
Jordi - 23 Apr 2007 22:57 GMT
Thanks Tom

> > Thanks Tom.
>
[quoted text clipped - 14 lines]
>
> Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.