package com.example.lion.takephoto;

import android.support.annotation.Nullable;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
import java.util.Random;
import java.util.TimerTask;
import java.util.UUID;

class HttpHelper {
    private static final String TAG = "uploadFile";
    private static final int CONNECT_TIME_OUT = 2*1000;
    private static final int READ_TIME_OUT = 5*1000;
    private static final String CHARSET = "utf-8";
    private static final String PREFIX = "--";
    private static final String LINE_END = "\r\n";
    private static final String CONTENT_TYPE = "multipart/form-data";

    private static final int TEST_SIZE = 4*1024*1024;
    private static Random rand = new Random();
    private static byte[] testBytes = null;


    public static String uploadFile(File file, String RequestURL, String name) throws IOException {
        String BOUNDARY = UUID.randomUUID().toString();
        URL url = new URL(RequestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(READ_TIME_OUT);
        conn.setConnectTimeout(CONNECT_TIME_OUT);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Charset", CHARSET);
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Content-Type", CONTENT_TYPE + "; boundary=" + BOUNDARY);
        OutputStream outputSteam=conn.getOutputStream();
        DataOutputStream dos = new DataOutputStream(outputSteam);
        String sb = PREFIX +
                BOUNDARY + LINE_END +
                "Content-Disposition: form-data; name=\"" + name +"\"; filename=\"" + file.getName() + "\"" + LINE_END +
                "Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END +
                LINE_END;
        dos.write(sb.getBytes());
        InputStream is = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len=is.read(bytes))!=-1)
        {
            dos.write(bytes, 0, len);
        }
        is.close();
        dos.write(LINE_END.getBytes());
        byte[] end_data = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes();
        dos.write(end_data);
        dos.flush();
        int res = conn.getResponseCode();
        if(res == 200) return conn.getResponseMessage();
        throw new IOException("response code: "+res);
    }

    @Nullable
    public static String test(String RequestURL) throws IOException {
        File file = File.createTempFile("speedTest", "http");
        file.delete();
        OutputStream os = new FileOutputStream(file);
        if (testBytes == null) {
            testBytes = new byte[TEST_SIZE];
            rand.nextBytes(testBytes);
        }
        os.write(testBytes);
        os.flush();
        os.close();
        long startTick, endTick;
        startTick = System.currentTimeMillis();
        uploadFile(file, RequestURL, "test");
        endTick = System.currentTimeMillis();
        double duration = endTick - startTick;
        return "success, speed: " + String.format(Locale.US, "%.1f", (TEST_SIZE/1024.0)/(duration/1000) ) + " KiB/s";
    }
}

@@ -41,45 +41,37 @@
 ort 
-android.support.annotation.Nullable;%0A
+java.io.ByteArrayInputStream;
 %0Aimp
@@ -157,41 +157,8 @@
 am;%0A
-import java.io.FileOutputStream;%0A
 impo
@@ -348,36 +348,8 @@
 om;%0A
-import java.util.TimerTask;%0A
 impo
@@ -914,17 +914,16 @@
  null;%0A%0A
-%0A
     publ
@@ -949,22 +949,29 @@
 load
-File(File file
+Stream(InputStream is
 , St
@@ -998,16 +998,33 @@
 ing name
+, String filename
 ) throws
@@ -1926,18 +1926,12 @@
 file
-.getN
+n
 ame
-()
  + %22
@@ -2101,60 +2101,8 @@
 ));%0A
-        InputStream is = new FileInputStream(file);%0A
     
@@ -2601,22 +2601,8 @@
  %7D%0A%0A
-    @Nullable%0A
     
@@ -2626,13 +2626,30 @@
 ing 
-test(
+uploadFile(File file, 
 Stri
@@ -2661,16 +2661,29 @@
 questURL
+, String name
 ) throws
@@ -2709,138 +2709,195 @@
     
-File file = File.createTempFile(%22speedTest%22, %22http%22);%0A        file.delet
+InputStream is = new FileInputStream(file);%0A        return uploadStream(is, RequestURL, name, file.getNam
 e()
+)
 ;%0A    
+%7D%0A%0A
     
-OutputStream os = new FileOutputStream(file);
+public static String test(String RequestURL) throws IOException %7B
 %0A   
@@ -3032,66 +3032,70 @@
     
-os.write(testBytes);%0A        os.flush();%0A        os.close(
+ByteArrayInputStream bais = new ByteArrayInputStream(testBytes
 );%0A 
@@ -3192,17 +3192,19 @@
 load
-File(file
+Stream(bais
 , Re
@@ -3219,16 +3219,20 @@
 , %22test%22
+, %22%22
 );%0A     

add .uploadStream(); fix #1