I'm trying to use sockets in java to download files from the internet. My program seems to work properly and compiles but when I go to check the file afterwards then its 0 bytes in size. I'm not sure where I'm going wrong with this.
BufferedInputStream download = new BufferedInputStream(socket.getInputStream());
FileOutputStream newFile = new FileOutputStream(fileName);
output.print("GET " + address + " HTTP/1.0\r\n\n");
output.flush();
byte[] input = new byte[10240];
int finished = download.read(input, 0, 10240);
I then have a while loop that makes sure that finished doesn't equal -1 and calls for more bytes of data while writing it to the file using:
newFile.write(input, 0, finished);
I know that my socket is configured properly since I can download and read the HTTP header with it. My program never enters the while loop to write the data, the read() function returns -1 right away. How would I go about fixing my code?
Thanks!
edit: I see that this was marked as duplicate for an answer that used the URL class to do this same thing. I cannot use URL or URLConnection.