Why is the program throwing an IO Exception?

Hallo,

I am trying to figure out how the URL and Socket class works, to write a small program, which is downloading stuff, from a given internet side.

import TerminalIO.*;

import java.net.*;

import java.io.*;

publicclass Downloader

{

private String dlto;

private String dlfrom;

privateint dlport=80;

publicboolean done=false;

publicstaticvoid main(String [] args)

{

KeyboardReader in =new KeyboardReader();

char again='n';

do

{

System.out.println("Press enter if u want to save in the default folder");

String to = in.readLine("Where do u want to save?: ");

String from = in.readLine("From where do u want to download?: ");

Downloader dl =new Downloader(to, from);

System.out.println("It is "+dl.done+" that it is done");

again=in.readChar("Enter Y for yes if u want to run the program again,\nN if u don't:\n");

}while(again =='Y' || again =='y');

}

public Downloader(String where, String what)

{

dlto=where;

dlfrom=what;

start();

}

privatevoid start()

{

try

{

int len=0;

InetAddress addr = InetAddress.getByName(dlfrom);

//Displays from where the client is downloading

System.out.println("Downloading from: "+addr.getHostName());

System.out.println("At IP: "+addr.getHostAddress());

//tries to connect to witht the URL object, if it fails uses the Socket object

Socket sock=null;

URL url=null;

try

{

url =new URL(addr.getHostName());

}

catch(IOException e)

{

sock =new Socket(addr.getHostName(), dlport);

}

//getting the InputStream

InputStream in;

if(url ==null)

{

in=sock.getInputStream();

}

else

{

in = url.openStream();

}

//Saving the Downloaded Data

FileOutputStream out =new FileOutputStream(dlto);

System.out.println("Byte length, to download: "+in.available());

byte[] b =newbyte[in.available()];

System.out.println("Downloading to: "+dlto);

while (in.read(b) != -1)

{

out.write(b, 0, in.read(b));

}

//closing the output

out.close();

in.close();

done=true;

}

catch (MalformedURLException e)

{

System.out.println("Address not found");

}

catch (IOException e)

{

System.err.println(e.toString());

System.out.println("-->IO Exception");

}

}

}

this is the Code i just wrote, but like i said in the subject, it is throwing an IOException in anycase. I could even try to download data from my own harddrive. So what shall i do? The thing which is confusing me the most, is that it actually displays the Name and the IP address, so that it seems to have access to the internet, but somehow it does not download the things, but i have no clue why. Please help.

null

[5617 byte] By [betlora] at [2007-10-2 20:22:50]
# 1

available() tells how many bytes are there in the buffer at the given moment. It is not appropriate tfor this purpose. What if available == 0 at the beginning but the input bytes will be available later?

Besides, you read two times into the buffer but consume it only once.

[ c o d e ] does not appear to work any longer.

<pre>

System.out.println("Byte length, to download: "+in.available());

byte[] b = new byte[in.available()];

System.out.println("Downloading to: "+dlto);

while (in.read(b) != -1)

{

out.write(b, 0, in.read(b));

}

</pre>

BIJ001a at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...
# 2

Oops. Would you like to try again? Probably not your fault, they just updated the Forums.

Make sure you tell us which IOException is being thrown, and from where, and what the exception text says.

and are you really trying to create a Socket inside an exception handler? and are you really trying to use available()? Don't do either of these two things.

ejpa at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...
# 3
<pre>int len=1024;System.out.println("Byte length, to download: "+len);byte[] b = new byte[len];System.out.println("Downloading to: "+dlto);int r;while ((r=in.read(b)) != -1){out.write(b, 0, r);}</pre>
BIJ001a at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...
# 4

I figured out how to get around the exception, but somehow, it still does not work correctly, the hole thing does not copy the hole thing, which is confusing me.

dint runs=0;

boolean terminate=false;

int len=0;

while ((len=in.read(b)) != -1 && terminate)

{

out.write(b, 0, len);

runs++;

if(runs ==2000 && in.read(b)==0)

{

terminate=true;

print+=("--"+"\n");

print+=("The file is empty"+"\n");

print+=("--"+"\n");

}

}

Sorry, [ c o d e ] does not work anymore

this somehow just copies an emtpy string, but how can i make it that it works, even with an element which length i don't now? by the way in.read(b) returns 3000 something with the test file i am using, but still the created file has a bit length of 0

sorry, somehow all edditing commades do not work enymore.

Message was edited by:

betlor

betlora at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...
# 5
You mean && !terminate
ejpa at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...
# 6
Oh, hm, that was kind of obvious, but thanks.
betlora at 2007-7-13 23:05:31 > top of Java-index,Core,Core APIs...