?
Solved

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

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.

719 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