This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes DataOutputStream.write( bytes[], int, int ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "DataOutputStream.write( bytes[], int, int )" Watch "DataOutputStream.write( bytes[], int, int )" New topic
Author

DataOutputStream.write( bytes[], int, int )

Mauricio Bonetti
Greenhorn

Joined: Aug 25, 2008
Posts: 4
I am trying to build a server that allows file transfering but I am still having problems to send bytes into the output stream ( clientSocket.getoutputStream() ), using DataOutputStream.write( bytes[], int, int ).


I've read the specification but i found it was not very clear.

I've saw an example it used a loop while to send the bytes, but i did not realize why do i have to implement this loop.

As follows:

while ( true )
{
int length = fis.read( buffer ) ; // fis = FileInputStream

if ( length == -1 )
break ;
else
dos.write(buffer, 0, length ) ; // dos = FileOutputStream
}

Thanks in advance.
Mario Minutolo
Greenhorn

Joined: Aug 06, 2008
Posts: 23
Originally posted by Mauricio Bonetti:
I am trying to build a server that allows file transfering but I am still having problems to send bytes into the output stream ( clientSocket.getoutputStream() ), using DataOutputStream.write( bytes[], int, int ).


I've read the specification but i found it was not very clear.

I've saw an example it used a loop while to send the bytes, but i did not realize why do i have to implement this loop.

As follows:

while ( true )
{
int length = fis.read( buffer ) ; // fis = FileInputStream

if ( length == -1 )
break ;
else
dos.write(buffer, 0, length ) ; // dos = FileOutputStream
}

Thanks in advance.


When you read a stream you 'try' to read a certain number of bytes, but you do not know how many bytes are left on the stream.

So you use a buffer with a certain capacity an try to read it to the full, gettin a result the number fo byte read:


After this your buffer has been loaded with data from the stream.

If the lenght is -1 the input stream has been read to the full.
Why not 0 ... it is possible sometimes that while receiving 0 byte the stream has not reached the end, but caution should be used here, since a stream receiving 0 bytes could be 'broken', you should consider some time-out option when receiving 0 bytes.

When you write a stream you serialize a buffer into your stream, the easyer way to do it , is using the same buffer as the one you read, limiting the output to the number of bytes you have read. This is the reason why you use the variable into the write function.


The the problem you have I think is that while not having a deep understanding on the way Java deals with streams, you are trying to use sockets streams which are deceitful little bastards.

There are tons of pitfalls you can fall into when you use sockets. My advice is this, try the same first with filestream to understand well how it work, and then try it with sockets.

Better still, do not use sockets, try to find some library which does this already, there are even 100kb big HTTP server which can be used to create simple file transfers.

Best Luck
[ August 26, 2008: Message edited by: Mario Minutolo ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DataOutputStream.write( bytes[], int, int )
 
Similar Threads
IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)
dataOutputStream.write
Why does this truncate my files?
writing to a file
Best Way to Edit a Byte Array