package org.helioviewer.jhv.view.jp2view.io.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ProtocolException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

//import org.helioviewer.jhv.base.ProxySettings;
import org.helioviewer.jhv.base.Regex;
import org.helioviewer.jhv.view.jp2view.io.ByteChannelInputStream;
import org.helioviewer.jhv.view.jp2view.io.LineRead;

public class HTTPSocket {

    private static final int TIMEOUT_CONNECT = 30000;
    private static final int TIMEOUT_READ = 30000;
    private static final int PORT = 80;

    private final SocketChannel channel;

    protected final int usedPort;
    protected final String usedHost;

    protected final InputStream inputStream;

    protected HTTPSocket(URI uri) throws IOException {
        //socket = new Socket(ProxySettings.proxy);
        channel = SocketChannel.open();

        channel.socket().setReceiveBufferSize(Math.max(262144 * 8, 2 * channel.socket().getReceiveBufferSize()));
        channel.socket().setTrafficClass(0x10);
        channel.socket().setSoTimeout(TIMEOUT_READ);
        channel.socket().setKeepAlive(true);
        channel.socket().setTcpNoDelay(true);

        int port = uri.getPort();
        usedPort = port <= 0 ? PORT : port;
        usedHost = uri.getHost();

        //socket.connect(new InetSocketAddress(usedHost, usedPort), TIMEOUT_CONNECT);
        channel.connect(new InetSocketAddress(usedHost, usedPort));
        inputStream = new ByteChannelInputStream(channel);
    }

    protected HTTPMessage recv() throws IOException {
        String line = LineRead.readAsciiLine(inputStream);
        if (!"HTTP/1.1 200 OK".equals(line))
            throw new ProtocolException("Invalid HTTP response: " + line);

        // Parses HTTP headers
        HTTPMessage res = new HTTPMessage();
        while (true) {
            line = LineRead.readAsciiLine(inputStream);
            if (line.isEmpty())
                return res;

            String parts[] = Regex.HttpField.split(line);
            if (parts.length != 2)
                throw new ProtocolException("Invalid HTTP header field: " + line);
            res.setHeader(parts[0], parts[1]);
        }
    }

    protected int write(String str) throws IOException {
        return channel.write(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8)));
    }

    protected void close() throws IOException {
        channel.close();
    }

    public boolean isClosed() {
        return !channel.isOpen();
    }

}

@@ -891,32 +891,50 @@
 s IOException %7B%0A
+        try %7B%0A    
         //socket
@@ -973,24 +973,28 @@
 y);%0A        
+    
 channel = So
@@ -1014,32 +1014,36 @@
 pen();%0A%0A        
+ 
+   
 channel.socket()
@@ -1132,32 +1132,36 @@
 ze()));%0A        
+    
 channel.socket()
@@ -1184,16 +1184,20 @@
 (0x10);%0A
+    
         
@@ -1241,32 +1241,36 @@
 _READ);%0A        
+    
 channel.socket()
@@ -1290,32 +1290,36 @@
 (true);%0A        
+  
+  
 channel.socket()
@@ -1349,16 +1349,20 @@
         
+   
+ 
 int port
@@ -1375,24 +1375,28 @@
 .getPort();%0A
+    
         used
@@ -1431,24 +1431,28 @@
 rt;%0A        
+ 
+   
 usedHost = u
@@ -1466,32 +1466,36 @@
 ost();%0A%0A        
+  
+  
 //socket.connect
@@ -1556,16 +1556,20 @@
 NNECT);%0A
+    
         
@@ -1636,16 +1636,20 @@
         
+    
 inputStr
@@ -1691,16 +1691,127 @@
 annel);%0A
+        %7D catch (Exception e) %7B // redirect all to IOException%0A            throw new IOException(e);%0A        %7D%0A
     %7D%0A%0A 

Redirect all exceptions