405327 Modular Start.ini

Improved start.jar download mechanism
This commit is contained in:
Greg Wilkins 2013-05-02 09:38:41 +10:00
parent 369a13d265
commit 57691ac881
1 changed files with 15 additions and 82 deletions

View File

@ -39,6 +39,7 @@ import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
@ -328,92 +329,24 @@ public class Main
if (file.exists())
return;
String[] parse = split[1].split("/",4);
String host=parse[2];
String uri="/"+split[1].substring(3+host.length());
URL url = new URL(split[0].substring(11)+":"+split[1]);
try (Socket socket = new Socket(host,80))
System.err.println("DOWNLOAD: "+url+" to "+location);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
byte[] buf=new byte[8192];
try (InputStream in = url.openStream(); OutputStream out = new FileOutputStream(file);)
{
String request="GET "+uri+" HTTP/1.0\r\n"+
"Host: "+host+"\r\n"+
"User-Agent: jetty-start.jar\r\n"+
"\r\n";
socket.getOutputStream().write(request.getBytes("ISO-8859-1"));
socket.getOutputStream().flush();
InputStream in = socket.getInputStream();
int state=0;
loop: while (state>=0)
while(true)
{
char c = (char)in.read();
int len = in.read(buf);
switch (state)
{
case 0:
c=Character.toLowerCase(c);
if (c==' ')
state=1;
else if (c!='h' && c!='t' && c!='p' && c!='/' && c!='.' && !Character.isDigit(c))
break loop;
break;
case 1:
if (c==' ')
state=2;
else if (c!='2' && c!='0')
break loop;
break;
case 2:
if (c=='\r')
state=3;
else if (c=='\n')
state=4;
break;
case 3:
if (c=='\n')
state=4;
else if (c=='\r')
state=-1;
break;
case 4:
if (c=='\r')
state=5;
else if (c=='\n')
state=-1;
else
state=2;
break;
case 5:
if (c=='\n')
state=-1;
else
state=2;
break;
}
}
if (state>=0)
throw new IOException("Bad HTTP response: "+state);
System.err.println("DOWNLOAD: "+uri+" from "+host+" to "+location);
if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
byte[] buf=new byte[8192];
try (OutputStream out = new FileOutputStream(file))
{
while(!socket.isInputShutdown())
{
int len = in.read(buf);
if (len>0)
out.write(buf,0,len);
if (len<0)
break;
}
if (len>0)
out.write(buf,0,len);
if (len<0)
break;
}
}
}