Solved

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

Posted on 2011-09-06
3
322 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 104
servlet doXXX methods 3 49
web services creation SOAP vs REST 5 42
Java: anonymous class 4 22
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 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