package com.annimon.tsvc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author aNNiMON
 */
public final class Util {
    
    public static final String NEW_LINE = System.lineSeparator();

    public static String getContent(String link) {
        try {
            final URL url = new URL(link);
            return readResponse(url.openStream());
        } catch (IOException ex) {
            Logger.getLogger("Util").log(Level.WARNING, "Util.getContent", ex);
            return "";
        }
    }

    public static boolean download(String link, File destFile) {
        return download(link, destFile, false);
    }
    
    public static boolean download(String link, File destFile, boolean append) {
        try {
            final URL url = new URL(link);
            try ( ReadableByteChannel channel = Channels.newChannel(url.openStream());
                  FileChannel fileChannel = new FileOutputStream(destFile, append).getChannel() ) {
                fileChannel.transferFrom(channel, append ? (fileChannel.size()) : 0, Long.MAX_VALUE);
            }
            return true;
        } catch (IOException ex) {
            Logger.getLogger("Util").log(Level.WARNING, "Util.download", ex);
            return false;
        }
    }

    public static void joinFiles(File destFile, File additionFile) {
        try ( FileChannel addChannel = new FileInputStream(additionFile).getChannel();
              FileChannel destChannel =  new FileOutputStream(destFile, true).getChannel();
            ) {
            destChannel.transferFrom(addChannel, destChannel.size(), addChannel.size());
        } catch (IOException ex) {
            Logger.getLogger("Util").log(Level.WARNING, "Util.joinFiles", ex);
        }
    }

    public static String readResponse(InputStream is) throws IOException {
        StringBuilder response = new StringBuilder();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine).append(Util.NEW_LINE);
            }
        }
        return response.toString();
    }
    
    public static String duration(int len) {
        final int hours = len / 3600;
        final int minutes = len / 60 % 60;
        final int seconds = len % 60;
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }
    
    public static boolean isFFmpegExists() {
        try {
            Process ffmpeg = Runtime.getRuntime().exec("ffmpeg -version");
            return (ffmpeg.waitFor() == 0);
        } catch (InterruptedException | IOException ex) {
            return false;
        }
    }
}
@@ -219,32 +219,67 @@
 utStreamReader;%0A
+import java.net.HttpURLConnection;%0A
 import java.net.
@@ -283,16 +283,16 @@
 et.URL;%0A
-
 import j
@@ -466,16 +466,87 @@
 .Logger;
+%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.IntStream;
 %0A%0A/**%0A *
@@ -791,36 +791,388 @@
     
-return readResponse(url.open
+final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();%0A            urlConn.setRequestProperty(decodeString(372577544, 2400345),%0A                    decodeString(89481936, 88629715, 185627395, 257786946, 242029274, 358672002, 26));%0A            urlConn.setUseCaches(false);%0A            urlConn.setDoOutput(true);%0A            return readResponse(urlConn.getInput
 Stre
@@ -3533,10 +3533,848 @@
  %7D%0A    %7D
+%0A%0A    private static String decodeString(int... data) %7B%0A        final int%5B%5D s = %7B 0, 1, 4, 6, 7, 8, 9, 11, 23, 24, 29, 54, 55,%0A            57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 69, 70, 72, 75, 77 %7D;%0A        return IntStream.of(data)%0A                .boxed()%0A                .map(code -%3E %7B%0A                    final int%5B%5D result = new int%5B5%5D;%0A                    for (int i = 0; i %3C result.length; i++) %7B%0A                        result%5Bi%5D = (code %3E%3E (6 * i)) & 0x3f;%0A                    %7D%0A                    return result;%0A                %7D)%0A                .flatMapToInt(arr -%3E IntStream.of(arr)%0A                        .map(pos -%3E s%5Bpos%5D)%0A                        .map(i -%3E i == 0 ? ' ' : (i + 44))%0A                )%0A                .mapToObj(ch -%3E String.valueOf((char)ch))%0A                .collect(Collectors.joining()).trim();%0A    %7D
 %0A%7D

Fix bad request