LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2012, 08:48 AM   #1
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Java: How do I serialize data into buffer?


I'm maintaining some Java code that is required to send a single data packet through a socket. The packet will contain multiple types of data: int, long, raw bytes, and characters.

Currently, the code uses a DataOutputStream that is bound to the socket's output stream, and issues separate packets for each data element. For example:
Code:
m_strm.writeInt(id);
m_strm.writeLong(queueTime);
m_strm.writeLong(sendTime);
m_strm.writeInt(senderID);
m_strm.writeInt(data.length);
m_strm.write(data);
m_strm.writeInt(returnAddress.length());
m_strm.writeChars(returnAddress);
It is my understanding that each statement above issues a separate packet through the socket, and this is not desirable.

How can I write all of the data to a temporary buffer, and then perform a write() of that data? Does Java offer a class that I can use to serialize data similar to the DataOutputStream, but that offers a way to actually get that data in the form of a byte array?

P.S. I've contemplated just defining a Serializable object that holds all of the data that I wish to send, then using an ObjectOutputStream to write the object via the Socket. Is this the best approach to solve this problem?
 
Old 08-01-2012, 10:46 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Probably the easiest (least amount of code change) way would be to hook up a ByteArrayOutputStream to the DataOutputStream, then send the byte array to the socket.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache 2.2.8 mod_proxy data buffer ron.entropy Linux - Server 0 08-08-2008 11:30 AM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
Data buffer size MarcReing Programming 7 10-21-2004 12:51 PM
Send data to the keyboard buffer rrodrigues Programming 5 07-20-2004 09:26 AM
java student with serialize type question Brain Drop Programming 11 03-29-2004 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration