R
riloo
Hi,
I'm trying to send an object through sockets using the following
method :
send :
Object > GZIP > byte array > OutputStream of a secure socket
receive :
InputStream > array of byte buffers
array of byte buffers > byte array
byte array > GZIP > Object
I use the following classes for sending the object :
ObjectOutputStream(GZIPOutputStream(ByteArrayOutputStream)))
- I can't use directly ObjectOutputStream using secure sockets
- ObjectInputStream seems not to work when the stream goes through
GZIPInputStream
- When I don't GZIP the stream, everything works fine... but only for
the first object. Since the second, I can notice that I receive a byte
array that is different from the first, but when I put it in the
ByteArrayInputStream and then ObjectInputStream the object received
is... the first one. What I can't understand is that the streams are
created during the reading process, so that they are different from a
message to the next.
Here is the code for the receiving process :
public Message getMessage() {
ArrayList byteArrays = new ArrayList();
boolean finish=false;
isFree=false;
Message result = null;
int nb=0;
try {
// Read the data from the socket InputStream
while (!finish) {
byte[] buf = new byte[4096];
System.out.println("Filling a buffer");
//int nb = input.read(buf);
nb = inCon.read(buf);
System.out.println(nb+" bytes read from the stream to
the buffer");
if (nb == -1) finish = true;
else if(nb<4096) {byteArrays.add(buf);finish = true;}
else byteArrays.add(buf);
}
System.out.println(byteArrays.size()+" arrays of bytes
read");
// Create an array of bytes containing all the bytes
received
int size = 4096*(byteArrays.size()-1)+nb;
byte[] resultArray = new byte[size];
int i=0;
for (Iterator ite = byteArrays.listIterator();
ite.hasNext(); ) {
byte[] buf = (byte[]) ite.next();
for (int j=0;j<4096;j++) {
if (i*4096+j < size)
resultArray[i*4096+j]=buf[j];
else break;
}
i++;
}
// Read the data with an ObjectInputStream, using a
ByteArrayInputStream
System.out.println("size of the array :
"+resultArray.length);
ByteArrayInputStream baInput = new
ByteArrayInputStream(resultArray);
//ObjectInputStream in = new ObjectInputStream(new
GZIPInputStream(baInput));
ObjectInputStream in = new ObjectInputStream(baInput);
result = (Message)in.readObject();
System.out.println("received message :
"+result.toString());
in.close();
/*result = (Message)in.readObject();*/
}
catch (IOException e) {
log.add(log.RMI, log.MAJOR,"(getMessage) IO Exception :
"+e.getLocalizedMessage());
}
catch (ClassNotFoundException cne) {
log.add(log.RMI, log.MAJOR,"(getMessage) ClassNotFound
Exception : "+cne.getLocalizedMessage());
}
isFree=true;
return result;
}
Many thanks for your help before I become crazy, even if it's only an
idea...
Gilles
I'm trying to send an object through sockets using the following
method :
send :
Object > GZIP > byte array > OutputStream of a secure socket
receive :
InputStream > array of byte buffers
array of byte buffers > byte array
byte array > GZIP > Object
I use the following classes for sending the object :
ObjectOutputStream(GZIPOutputStream(ByteArrayOutputStream)))
- I can't use directly ObjectOutputStream using secure sockets
- ObjectInputStream seems not to work when the stream goes through
GZIPInputStream
- When I don't GZIP the stream, everything works fine... but only for
the first object. Since the second, I can notice that I receive a byte
array that is different from the first, but when I put it in the
ByteArrayInputStream and then ObjectInputStream the object received
is... the first one. What I can't understand is that the streams are
created during the reading process, so that they are different from a
message to the next.
Here is the code for the receiving process :
public Message getMessage() {
ArrayList byteArrays = new ArrayList();
boolean finish=false;
isFree=false;
Message result = null;
int nb=0;
try {
// Read the data from the socket InputStream
while (!finish) {
byte[] buf = new byte[4096];
System.out.println("Filling a buffer");
//int nb = input.read(buf);
nb = inCon.read(buf);
System.out.println(nb+" bytes read from the stream to
the buffer");
if (nb == -1) finish = true;
else if(nb<4096) {byteArrays.add(buf);finish = true;}
else byteArrays.add(buf);
}
System.out.println(byteArrays.size()+" arrays of bytes
read");
// Create an array of bytes containing all the bytes
received
int size = 4096*(byteArrays.size()-1)+nb;
byte[] resultArray = new byte[size];
int i=0;
for (Iterator ite = byteArrays.listIterator();
ite.hasNext(); ) {
byte[] buf = (byte[]) ite.next();
for (int j=0;j<4096;j++) {
if (i*4096+j < size)
resultArray[i*4096+j]=buf[j];
else break;
}
i++;
}
// Read the data with an ObjectInputStream, using a
ByteArrayInputStream
System.out.println("size of the array :
"+resultArray.length);
ByteArrayInputStream baInput = new
ByteArrayInputStream(resultArray);
//ObjectInputStream in = new ObjectInputStream(new
GZIPInputStream(baInput));
ObjectInputStream in = new ObjectInputStream(baInput);
result = (Message)in.readObject();
System.out.println("received message :
"+result.toString());
in.close();
/*result = (Message)in.readObject();*/
}
catch (IOException e) {
log.add(log.RMI, log.MAJOR,"(getMessage) IO Exception :
"+e.getLocalizedMessage());
}
catch (ClassNotFoundException cne) {
log.add(log.RMI, log.MAJOR,"(getMessage) ClassNotFound
Exception : "+cne.getLocalizedMessage());
}
isFree=true;
return result;
}
Many thanks for your help before I become crazy, even if it's only an
idea...
Gilles