...
Http- load loadContent ( : ):
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.GZIPOutputStream;
public class BadHttpServer {
private static int request = 0, connection = 0;
private static final BadHttpServer SINGLETON = new BadHttpServer();
private ServerSocket serverSocket;
private String htmlString, formattedDate;
private byte[] compressedHTML;
public String getURL() {
return "http://localhost:60000";
}
synchronized byte[] getCompressedHTML() {
return compressedHTML;
}
synchronized void setCompressedHTML(String string) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(string.length() / 2);
GZIPOutputStream gos = new GZIPOutputStream(baos);) {
gos.write(string.getBytes("utf-8"));
gos.close();
this.compressedHTML = baos.toByteArray();
formattedDate = String.format(Locale.US, "%1$ta, %1$td %1$tb %1$tY %1$tH:%1$tM:%1$tS %1$tZ",
Calendar.getInstance(TimeZone.getTimeZone("GMT")));
} catch (IOException e) {
e.printStackTrace();
}
}
private synchronized String getFormattedDate() {
return formattedDate;
}
private BadHttpServer() {
try {
serverSocket = new ServerSocket(60000);
} catch (IOException e) {
e.printStackTrace();
}
new Thread() {
{
setDaemon(true);
setName("serverSocketThread");
}
public void run() {
while (true) {
try {
new Thread(acceptAndSendHTML(serverSocket.accept())) {
{
setDaemon(true);
setName("acceptThread" + connection++);
}
}.start();
} catch (IOException e) {
e.printStackTrace();
}
}
};
}.start();
}
protected Runnable acceptAndSendHTML(final Socket socket) {
return new Runnable() {
@Override
public void run() {
try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
BufferedWriter asciiWriter = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(), "ASCII"));
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), 1000)) {
while (!socket.isClosed()) {
int request = BadHttpServer.request++;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(br.readLine() + "\r\n");
for (String line = ""; !(line = br.readLine()).isEmpty(); stringBuilder.append(line + "\r\n"))
;
System.out.print("Connection " + request + " request: " + stringBuilder);
byte[] compressedHTML = getCompressedHTML();
asciiWriter.write("HTTP/1.1 100 Continue\r\n");
asciiWriter.write("\r\n");
asciiWriter.write("HTTP/1.1 200 OK\r\n");
asciiWriter.write("Date: " + getFormattedDate() + "\r\n");
asciiWriter.write("Content-Type: text/html; charset=utf-8\r\n");
asciiWriter.write("Content-Encoding: gzip\r\n");
asciiWriter.write("Content-Charset: utf-8\r\n");
asciiWriter.write("Content-Length: " + compressedHTML.length + "\r\n");
System.out.println("Content-Length: " + compressedHTML.length + "\r\n");
asciiWriter.write("\r\n");
asciiWriter.flush();
for (int writtenOverall = 0, writtenThisIteration = 0; writtenOverall < compressedHTML.length; writtenOverall += writtenThisIteration) {
bos.write(compressedHTML, writtenOverall,
writtenThisIteration = Math.min(compressedHTML.length - writtenOverall, 1000));
Thread.sleep(10);
}
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}
public static BadHttpServer getIntance() {
return SINGLETON;
}
public synchronized void setHtmlString(String htmlString) {
this.htmlString = htmlString;
setCompressedHTML(htmlString);
}
synchronized String getHtmlString() {
return htmlString;
}
}
loadContent
BadHttpServer.getIntance().setHtmlString(htmlString);
Platform.runLater(() -> wvIn.getEngine().load(BadHttpServer.getIntance().getURL()));
- loadWorker RUNNING SUCCEDED:
via BadHttpServer:
javascript and style after body: 13,5 s
javascript and style before body: 8,6 s
so I continued to test with javascript and style before body.
without javascript injection: 8,5 s
I continued to test with javascript injection.
Next I did with loadContent: 170 s - no UI resposivness during loading
Yep - almost 3 minutes...
So I returned back with BadHttpServer.
Next I tried "throttling the bandwidth" by changing the sleep time.
no sleep: 8,2 s
5 ms sleep: 6,8 s
10 ms sleep: 6,7 s
20 ms sleep: 11,6 s
In these cases the UI was responsive, although it was lagging a bit.
The progressIndicator showed nicely, pausing a bit at 90%.
By increasing the sleep time, the UI was more responsive.
, , loadContent , - , ...