Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-06
3
Medium Priority
?
330 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
  • 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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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

971 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