import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import java.util.function.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import static x30_pkg.x30_util.DynamicObject;
import java.text.NumberFormat;
import java.nio.charset.Charset;
import java.awt.geom.*;
class main {
static String ffmpeg_mixVideoAndAudio_copy_mp4_mp3(File videoIn) {
return ffmpeg_mixVideoAndAudio_copy(videoIn,
replaceExtension(videoIn, ".mp3"),
appendBaseFileName(videoIn, "-new-audio"));
}
static String ffmpeg_mixVideoAndAudio_copy(File videoIn, File audioIn, File out) {
return ffmpeg(ffmpeg_argsForMixingVideoAndAudio_copy(videoIn, audioIn, out));
}
static File replaceExtension(File f, String extOld, String extNew) {
return newFile(replaceExtension(f2s(f), extOld, extNew));
}
static File replaceExtension(File f, String extNew) {
return replaceExtension(f, fileExtension(f), extNew);
}
static String replaceExtension(String s, String extOld, String extNew) {
s = dropSuffixIC(addPrefixOptIfNempty(".", extOld), s);
return s + addPrefixOptIfNempty(".", extNew);
}
static String replaceExtension(String name, String extNew) {
return replaceExtension(name, fileExtension(name), extNew);
}
static File appendBaseFileName(File f, String s) {
return appendToFileNameBeforeExtension(f, s);
}
static String ffmpeg(String args) {
return backtickToConsole_returnOutput(ffmpegCmd() + " " + args);
}
static String ffmpeg(File in, File out) {
return ffmpeg_simpleConvert(in, out);
}
static String ffmpeg_argsForMixingVideoAndAudio_copy(File videoIn, File audioIn, File out) {
return "-y -i " + platformQuote(videoIn)
+ " -i " + platformQuote(audioIn)
+ " -c:v copy -c:a copy -strict experimental"
+ " -map 0:v:0 -map 1:a:0"
+ " " + platformQuote(out);
}
static File newFile(File base, String... names) {
for (String name : names) base = new File(base, name);
return base;
}
static File newFile(String name) {
return name == null ? null : new File(name);
}
static File newFile(String base, String... names) {
return newFile(newFile(base), names);
}
static String f2s(File f) {
return f == null ? null : f.getAbsolutePath();
}
static String f2s(String s) { return f2s(newFile(s)); }
static String f2s(java.nio.file.Path p) {
return p == null ? null : f2s(p.toFile());
}
static String fileExtension(File f) {
if (f == null) return null;
return fileExtension(f.getName());
}
static String fileExtension(String s) {
return substring(s, smartLastIndexOf(s, '.'));
}
static String dropSuffixIC(String suffix, String s) {
return s == null ? null : ewic(s, suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static String addPrefixOptIfNempty(String prefix, String s) {
return addPrefixIfNotEmpty2(prefix, s);
}
static File appendToFileNameBeforeExtension(File f, String s) {
String ext = fileExtension(f);
String name = dropSuffixIC(ext, f.getName());
name += s;
return newFile(parentFile(f), name + ext);
}
// returns program's full output (STDOUT+STDERR)
static String backtickToConsole_returnOutput(String cmd) {
return loadTextFile(backtickToConsole(cmd));
}
static String ffmpegCmd() {
return or2(platformQuoteOrEmpty(loadTextFileTrim(ffmpegLocationFile())), "ffmpeg") + " " + ffmpeg_generalArguments();
}
static String ffmpeg_simpleConvert(File in, File out) {
return ffmpeg(ffmpeg_simpleInOutArgs(in, out));
}
static String platformQuote(String s) {
return isWindows() ? winQuote(s) : bashQuote(s);
}
static String platformQuote(File f) {
return platformQuote(f2s(f));
}
static String substring(String s, int x) {
return substring(s, x, strL(s));
}
static String substring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
int n = s.length();
if (y < x) y = x;
if (y > n) y = n;
if (x >= y) return "";
return s.substring(x, y);
}
// convenience method for quickly dropping a prefix
static String substring(String s, CharSequence l) {
return substring(s, lCharSequence(l));
}
static int smartLastIndexOf(String s, char c) {
if (s == null) return 0;
int i = s.lastIndexOf(c);
return i >= 0 ? i : l(s);
}
static int smartLastIndexOf(List l, A sub) {
int i = lastIndexOf(l, sub);
return i < 0 ? l(l) : i;
}
static boolean ewic(String a, String b) {
return endsWithIgnoreCase(a, b);
}
static boolean ewic(String a, String b, Matches m) {
return endsWithIgnoreCase(a, b, m);
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static String addPrefixIfNotEmpty2(String prefix, String s) {
return empty(s) ? "" : addPrefix(prefix, s);
}
static File parentFile(File f) {
return dirOfFile(f);
}
static String loadTextFile(String fileName) {
return loadTextFile(fileName, null);
}
static String loadTextFile(File f, String defaultContents) { return loadTextFile(f, defaultContents, "UTF-8"); }
static String loadTextFile(File f, String defaultContents, String encoding) { try {
checkFileNotTooBigToRead(f);
if (f == null || !f.exists()) return defaultContents;
FileInputStream fileInputStream = new FileInputStream(f);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, encoding);
return loadTextFile(inputStreamReader);
} catch (Exception __e) { throw rethrow(__e); } }
public static String loadTextFile(File fileName) {
return loadTextFile(fileName, null);
}
static String loadTextFile(String fileName, String defaultContents) {
return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents);
}
static String loadTextFile(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
try {
char[] buffer = new char[1024];
int n;
while (-1 != (n = reader.read(buffer)))
builder.append(buffer, 0, n);
} finally {
reader.close();
}
return str(builder);
}
// returns file with program output (STDOUT+STDERR)
static File backtickToConsole(String cmd) {
ping();
File outputFile = createTempFile(cmd, ".out");
makeEmptyFile(outputFile); // unnecessary?
TailFile tail = tailFile2(outputFile, 1000, __1 -> printNoNewLine_fix(__1));
// make a dependent child process that is exited when we exit
print("Running: " + cmd);
try {
backtickToFile(cmd, outputFile);
} catch (Throwable __e) { printStackTrace(__e); }
tail.stop();
return outputFile;
}
static String or2(String a, String b) {
return nempty(a) ? a : b;
}
static String or2(String a, String b, String c) {
return or2(or2(a, b), c);
}
static String platformQuoteOrEmpty(String s) {
return empty(s) ? "" : platformQuote(s);
}
static String loadTextFileTrim(String fileName) {
return loadTextFile_trim(fileName);
}
static String loadTextFileTrim(File f, String defaultContents) {
return loadTextFile_trim(f, defaultContents);
}
static String loadTextFileTrim(File fileName) {
return loadTextFile_trim(fileName);
}
static String loadTextFileTrim(String fileName, String defaultContents) {
return loadTextFile_trim(fileName, defaultContents);
}
static File ffmpegLocationFile() {
return javaxDataDir("ffmpeg-location.txt");
}
static String ffmpeg_generalArguments() {
return trim(unnull(loadTextFile(javaxDataDir("ffmpeg-options.txt"))));
// ret "-threads " + max(numberOfCores()-1, 1);
}
static String ffmpeg_simpleInOutArgs(File in, File out) {
return "-i " + platformQuote(in) + " " + platformQuote(out);
}
public static boolean isWindows() {
return System.getProperty("os.name").contains("Windows");
}
/** possibly improvable */
public static String winQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}
static String winQuote(File f) {
return winQuote(f.getAbsolutePath());
}
/** possibly improvable */
static String bashQuote(String text) {
if (text == null) return null;
return "\"" + text
.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n")
.replace("\r", "\\r") + "\"";
}
static String bashQuote(File f) {
return bashQuote(f.getAbsolutePath());
}
static int strL(String s) {
return s == null ? 0 : s.length();
}
static int lCharSequence(CharSequence s) {
return s == null ? 0 : s.length();
}
static int lastIndexOf(String a, String b) {
return a == null || b == null ? -1 : a.lastIndexOf(b);
}
static int lastIndexOf(String a, char b) {
return a == null ? -1 : a.lastIndexOf(b);
}
// starts searching from i-1
static int lastIndexOf(List l, int i, A a) {
if (l == null) return -1;
for (i = min(l(l), i)-1; i >= 0; i--)
if (eq(l.get(i), a))
return i;
return -1;
}
static int lastIndexOf(List l, A a) {
if (l == null) return -1;
for (int i = l(l)-1; i >= 0; i--)
if (eq(l.get(i), a))
return i;
return -1;
}
static boolean endsWithIgnoreCase(String a, String b) {
int la = l(a), lb = l(b);
return la >= lb && regionMatchesIC(a, la-lb, b, 0, lb);
}
static boolean endsWithIgnoreCase(String a, String b, Matches m) {
if (!endsWithIgnoreCase(a, b)) return false;
if (m != null)
m.m = new String[] { substring(a, 0, l(a)-l(b)) };
return true;
}
static int iteratorCount_int_close(Iterator i) { try {
int n = 0;
if (i != null) while (i.hasNext()) { i.next(); ++n; }
if (i instanceof AutoCloseable) ((AutoCloseable) i).close();
return n;
} catch (Exception __e) { throw rethrow(__e); } }
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(Iterable c) { return c == null || !c.iterator().hasNext(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(BitSet bs) { return bs == null || bs.isEmpty(); }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(double[] a) { return a == null || a.length == 0; }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(IMultiMap mm) { return mm == null || mm.size() == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static boolean empty(Rect r) { return !(r != null && r.w != 0 && r.h != 0); }
static String addPrefix(String prefix, String s) {
return s.startsWith(prefix) ? s : prefix + s;
}
static File dirOfFile(File f) {
return f == null ? null : f.getParentFile();
}
static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal();
static void checkFileNotTooBigToRead(File f) {
callF(checkFileNotTooBigToRead_tl.get(), f);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
//ifdef useNewPing
newPing();
//endifdef
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static File createTempFile() {
return createTempFile("tmp", null);
}
static File createTempFile(String prefix, String suffix) { try {
prefix = nohup_sanitize(prefix);
suffix = nohup_sanitize(suffix);
if (shouldKeepTempFiles())
return mkdirsForFile(javaxCachesDir("Temp/" + prefix + "-" + randomID() + "-" + suffix));
// File.createTempFile needs at least 3 prefix characters, so we
// fíll them up for you
File f = File.createTempFile(takeFirst(10, pad(prefix, 3, '-')), suffix);
f.deleteOnExit();
return f;
} catch (Exception __e) { throw rethrow(__e); } }
static void makeEmptyFile(File f) { try {
new FileOutputStream(f).close();
} catch (Exception __e) { throw rethrow(__e); } }
static TailFile tailFile2(File file, int interval, IVF1 onData) {
TailFile tf = new TailFile(file, interval, onData);
tf.start();
return tf;
}
static String printNoNewLine_fix(String s) {
return printNoNewLine(fixNewLines(s));
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal