Solved

Am I outputting a file from a network stream correctly in Java?

Posted on 2011-09-06
3
327 Views
Last Modified: 2012-05-12
I'm using the following method to write the bytes I receive over a network stream to a file.  However, I thought that according to the api that whenever the end of the stream was reached, a -1 value would be returned.  This never occurs in my case.  Instead, when the end of the file is reached, an IOException is thrown and transmission terminates.  The resulting output file is the correct size and contains the correct data, but i thought a -1 was supposed to be sent by the stream.read message when the end of stream was reached.

public void writeIncomingByteDataFromClient(Socket client, FileOutputStream fileOut) {
		
		int byteAsInteger = -1;


		do {

			try {

				byteAsInteger = client.getInputStream().read();
				System.out.println(byteAsInteger);

			} catch (IOException e) {

				System.err
						.println("Error reading incoming byte data from client: "
								+ client.getInetAddress().toString() + e.getMessage());
				
				return;

			}
		
			if (byteAsInteger != -1) {
				try {
					fileOut.write(byteAsInteger);
				} catch (IOException e) {
					System.err.println("IO Exception When Writing Output Stream: " + e.getMessage());			
				}
				
			}

		} while (byteAsInteger != -1);
		
		

		return;
	}

Open in new window

0
Comment
Question by:davysouthernboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36489320



try something like that:

you rather raed it this way :
try{


while((byteAsInteger = client.getInputStream().read()) != -1){


fileOut.write(byteAsInteger);

};


} catch(Exception ex){

}
0
 

Author Comment

by:davysouthernboy
ID: 36489487
So, are you saying that I should indeed receive a -1 instead of an exception being thrown when the end of stream is reached on the server side?

Here is my function from a test client.  Should i explicitly send -1  from the client to signal end of transmission????

 
public void uploadFileToServer()
	{
		int byteRead = -1;
		
		do {
			try {
				byteRead = fileInputStream.read();
				
				if(byteRead != -1)
				{
					clientOutputStream.write(byteRead);
				}
			} catch (IOException e) {
				System.err.println("Error reading from file: " + e.getMessage());
				return;
			}
		}while(isClientConnected() && byteRead != -1);

}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 50 total points
ID: 36489544
No, I don't think you should - it the read() method would return it itself when it encounters end of file - you nedd ust to check for it properly

read this:

http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#read%28%29
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question