• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem while transferrinf file over network thru serializable object  RSS feed

 
Gayatri Shende
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am writing a code to transfer a file from server machine to client machine.
I am using Object protocol for communication. ie client sends a filerequest object , server recognizes it and send the response back in form of object again.
My problem is :

I read the file on server machine , in chunks of 1024 bytes.
I try to write an object to Objectoutputstram of socket.
For doing this, If I store the data read from file in any outputstream/Bufferedoutputstream etc then it throws exception : NOTserializable. Please guide me, what should I use to store the data from file and write it as object.

Here is my code:

Server:


ObjectOutputStream objOut = new ObjectOutputStream (socket.getoutputStream());

objOut.writeObj(processfile());
objOut.flush();



public Object processfile()
{

byte[] packet = new byte[1024];
int bytes_trans=0; //transfered bytes
int bytes_read;//read bytes
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out=new ObjectOutputStream (bout) ;

try {
FileInputStream fin = new FileInputStream(filename);



while( (bytes_read = fin.read(packet, 0,packet.length)) >= 0)
{
bout.write(packet,0,bytes_read);


}




fin.close();

return out;

}


This code doesnt work.It throws exception. Please guide me what should I use to store the bytes read from file so that it can be sent as an object which is serializable.


Thanks
gayatri
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the exception name suggests, the object you are writing to the ObjectOutputStream is not serializable.
You are trying to write the ObjectOutputStream object returned by processFile() on to the ObjectOutputStream wrapping the socket output. I am sure this is not what you intend.

Please use code tags while posting code. ShowSomeEffort and Tell the details
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why Nitesh feels the need to admonish you to ShowSomeEffort and TellTheDetails, but his analysis is correct. You're creating a second object output stream that doesn't write to the socket. Plus, you're trying to write the second object stream object to the socket, which can't work. Instead you need to write the bytes to the first object stream. Note that ObjectOutputStream has a method that lets you write multiple bytes from an array to it.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf:
I'm not sure why Nitesh feels the need to admonish you to ShowSomeEffort and TellTheDetails


Hey Ulf the following are the reasons:

ShowSomeEffort: The exception is NotSerializableException. The javadoc and the name clearly tells :
Thrown when an instance is required to have a Serializable interface.

Combined with the stacktrace it would have clearly pointed to the object that is not serializable and may be after that the reason for the exception would have been known and the question may have been different.

TellTheDetails: The excpetion stack trace was not there which would have certainly reduced the pain of finding out which line is throwing an exception(As you can see there are more than one ObjectOutputStream involved.)

P.S.: Since the poster is new to Javaranch, i thought pointing out these links would help. Also, that was entirely my point of view and i may be in the minority.
[ February 12, 2008: Message edited by: Nitesh Kant ]
 
Pavel Cherkashin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Gayatri Shende:
first of all java.io.ObjectOutputStream being extension of java.io.OutputStream supports method

So you can use it instead of wrapping to the Object;

Seecond, if you prefer transferring objects, here you can pass plain array of bytes to method

in your example it will look like:

processFile returns plain byte array
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!