package com.timepath.util.logging;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPOutputStream;

/**
 * @author TimePath
 */
public class DBInbox {

    private DBInbox() {}

    public static String send(String user, String file, String directory, String message) throws IOException {
        byte[] in = message.getBytes(StandardCharsets.UTF_8);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(in.length);
        try(GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
            gzip.write(in);
        }
        byte[] bytes = baos.toByteArray();
        URL url = new URL("http://dbinbox.com/send/" + user + "/" + directory);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "keep-alive");
        conn.setRequestProperty("Content-Length", String.valueOf(bytes.length));
        String boundary = "**********";
        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        try(DataOutputStream out = new DataOutputStream(conn.getOutputStream())) {
            out.writeBytes("--" + boundary + "\r\n");
            out.writeBytes("Content-Disposition: form-data; name=\"files[]\"; filename=\"" + file + "\"\r\n");
            out.writeBytes("Content-Type: application/octet-stream\r\n\r\n");
            out.write(bytes);
            out.writeBytes("\r\n--" + boundary + "--\r\n");
            out.flush();
        }
        try(BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
            StringBuilder sb = new StringBuilder();
            for(String line; ( line = br.readLine() ) != null; sb.append('\n').append(line)) ;
            return sb.substring(1);
        }
    }
}

@@ -1803,12 +1803,8 @@
     
-for(
 Stri
@@ -1811,17 +1811,36 @@
 ng line;
- 
+%0A            while (
 ( line =
@@ -1863,17 +1863,35 @@
  != null
-;
+) %7B%0A               
  sb.appe
@@ -1915,11 +1915,23 @@
 ine)
-) ;
+;%0A            %7D
 %0A   
@@ -1963,9 +1963,32 @@
 ing(
-1
+Math.min(1, sb.length())
 );%0A 

Fix index out of bounds on empty response