Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now