• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic