LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-24-2014, 01:06 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
writing to a file chunk by chunk


Dear all,

I basically have a TCP client\server where the server sends a big file to the client chunk by chunk. I basically want to ask how is it possible to write to an output file a byte array each time a chunk is received.
An example can be:
Quote:
public class Chunk implements Serializable
{
private byte [] data;
// getters and setters
}

public class Client extends Thread
{

public void run()
{
Chunk chunk = (Chunk) readMessage();
}
}
Thank you.
 
Old 10-24-2014, 01:50 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
http://docs.oracle.com/javase/7/docs...putStream.html
 
1 members found this post helpful.
Old 10-24-2014, 02:01 PM   #3
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
NevemTeve,

Thanks for your reply. I do not really know on how much I can use a FileOutputStream to write to the file. For instance, I might receive chunks in a random order. I had a thought about FileOutputStream but I am not 100% sure if write(byte[], int, int) will necessarily work. Do you believe that write(byte[], int, int) will necessarily work?

Thank you.
 
Old 10-24-2014, 02:25 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You could create a file full of zeros for the full file size and then write the chunks in the right place as you receive them.

I guess you could also just buffer them, but with large files this will not work.
 
1 members found this post helpful.
Old 10-25-2014, 12:10 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> For instance, I might receive chunks in a random order.

With TCP? How would it be possible? Perhaps you thought of UDP?
 
Old 10-25-2014, 03:38 AM   #6
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
NevemTeve,

Once more again thanks for your interest. I would really like to ask you that because I do not have so much experience with TCP sockets programming. Setting the case that a TCP client\server will communicate with the use of Object Messages rather than sending data through a byte stream what I would like to know if there is message delivery guarantee, and if there is any need for acknoledging Object Messages. Therefore, is every message guaranteed to be received. Will Java retransmit itself corrupted messages?

Sincerely,
Emmanouil.
 
Old 10-25-2014, 05:51 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I don't know what 'Object Messages' are, but I can tell you what TCP is: full-duplex reliable bytestream between two parties (actually, it is two independent bytestreams). The bytes arrive in the order they were sent (no loss, no duplication, no reorder), but message boundaries aren't reserved.
 
1 members found this post helpful.
Old 10-25-2014, 06:32 AM   #8
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
NevemTeve,

So if i try to send a message of 32Kb, which might be quiet large, does TCP quarantees that the message will be delivered correctly?
 
Old 10-25-2014, 06:57 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Actually, there's no messages, only byte-streams: the remote side will receive every byte in the same order you sent them, but not neccessarily in one part: it might first receive one byte, then 30000 bytes, then 767 bytes... (Of course, the connection still might get lost during the transfer.)
 
1 members found this post helpful.
Old 10-25-2014, 07:26 AM   #10
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
I actually used the FileOutputStream that you told me and I can successfully send and write to disk some files. The only that really bothers me is if I try to write a bad chunk (a message altered due to communication problems) to the FileOutputStream I do not really believe that I can overwrite the chunk. The FileOutputStream I think only supports continuous writes of bytes. Do you really know any possible way to specify a method like
Quote:
write(byte [] data, int streamStartOffset)
This should actually write the data starting at specified streamStartOffset. So if I have a file of 1024 bytes, write(data, 0) will indicate that the data will be written starting from index 0.

Really thanks.
 
Old 10-25-2014, 07:40 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> write a bad chunk (a message altered due to communication problems)

With TCP? How would it be possible?

Nontheless, there is RandomAccessFile in Java: http://docs.oracle.com/javase/7/docs...ccessFile.html

Last edited by NevemTeve; 10-25-2014 at 07:43 AM.
 
  


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
Bidirectional chunk x{3} Linux - Server 0 08-14-2012 09:23 PM
[C] help parsing lines into chunks and writing each chunk gnashley Programming 2 09-11-2010 08:49 AM
Chunk size for RAID ssd7 Linux - Server 3 03-11-2010 02:44 PM
extracting a chunk of text from a large text file lothario Linux - Software 3 02-28-2007 08:16 AM
chunk of questions re. MDK 7.2 jin Linux - Newbie 5 12-19-2000 05:13 PM

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

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

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