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 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 javax.imageio.*;
import java.math.*;
import org.mp3transform.Bitstream;
import org.mp3transform.Decoder;
import org.mp3transform.Header;
import org.mp3transform.wav.*;
import javazoom.jl.player.*;
import java.text.NumberFormat;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
class main {
static void playMP3ThroughWAV(File mp3File) {
File wavFile = replaceExtension(mp3File, ".wav");
mp3ToWAVUnlessExists(mp3File, wavFile);
playWAV(wavFile);
}
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 mp3ToWAVUnlessExists(File in, File out) {
if (!out.exists())
mp3ToWAV(in, out);
return out;
}
static void playWAV(File f) {
playWAVAndWait(f);
}
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);
}
// mp3transform
static void mp3ToWAV(String in, String out) { try {
mp3ToWAV(new File(in), new File(out));
} catch (Exception __e) { throw rethrow(__e); } }
static File mp3ToWAV(File in, File out) { try {
mp3ToWAV(new BufferedInputStream(new FileInputStream(in)), out);
return out;
} catch (Exception __e) { throw rethrow(__e); } }
static void mp3ToWAV(InputStream in, File out) { try {
class WavConverter extends Decoder {
void convert(String sourceFileName, String destFileName)
throws IOException {
InputStream fileIn = new FileInputStream(sourceFileName);
BufferedInputStream in = new BufferedInputStream(fileIn, 128 * 1024);
convert(in, destFileName);
in.close();
}
void convert(InputStream sourceStream, String destFileName)
throws IOException {
int frameCount = Integer.MAX_VALUE;
WavConverter decoder = new WavConverter();
Bitstream stream = new Bitstream(sourceStream);
frameCount = Integer.MAX_VALUE;
try {
for (int frame = 0; frame < frameCount; frame++) {
Header header = stream.readFrame();
if (header == null) {
break;
}
if (decoder.channels == 0) {
int channels = (header.mode() == Header.MODE_SINGLE_CHANNEL) ? 1
: 2;
int freq = header.frequency();
decoder.initOutputBuffer(channels, freq, destFileName);
}
decoder.decodeFrame(header, stream);
stream.closeFrame();
}
} finally {
decoder.close();
}
}
private short[] buffer;
private WaveFileWriter outWave;
int channels;
public void initOutputBuffer(int numberOfChannels, int freq, String fileName)
throws IOException {
super.initOutputBuffer(null, numberOfChannels);
channels = numberOfChannels;
buffer = new short[BUFFER_SIZE];
for (int i = 0; i < numberOfChannels; ++i) {
bufferPointer[i] = (short) i;
}
outWave = nuObject(WaveFileWriter.class, fileName, freq, (short) 16, (short) numberOfChannels);
}
public void appendSamples(int channel, double[] f) {
int p = bufferPointer[channel];
for (int i = 0; i < 32; i++) {
double sample = f[i];
short s = ((sample > 32767.0f) ? 32767
: ((sample < -32768.0f) ? -32768 : (short) sample));
buffer[p] = s;
p += channels;
}
bufferPointer[channel] = p;
}
public void writeBuffer() throws IOException {
call(outWave, "writeData", buffer, bufferPointer[0]);
for (int i = 0; i < channels; ++i) {
bufferPointer[i] = i;
}
}
public void close() throws IOException {
callOpt(outWave, "close");
}
}
new WavConverter().convert(in, f2s(out));
} catch (Exception __e) { throw rethrow(__e); } }
// JavaLayer 1.0.1
static void playWAVAndWait(File wav) { try {
if (callService("playWAVAndWait", wav)) return;
String method = soundPlayMethod();
if (eq(method, "aplay")) {
print("Playing WAV (aplay)...");
backtick("aplay " + bashQuote(wav));
} else if (eq(method, "cmdmp3.exe")) {
print("Playing WAV (cmdmp3.exe)...");
backtick(winQuote(cmdmp3_exe()) + " " + winQuote(wav));
} else {
print("Playing WAV (JavaZoom), " + fileSize(wav) + " bytes...");
final Player player = new Player(new FileInputStream(wav));
player.play();
while (licensed()) { try {
if (player.isComplete()) break;
} catch (Throwable __e) { _handleException(__e); } sleepSeconds(10); }
player.close();
}
} catch (Exception __e) { throw rethrow(__e); } }
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;
if (x >= s.length()) return "";
if (y < x) y = x;
if (y > s.length()) y = s.length();
return s.substring(x, y);
}
static int smartLastIndexOf(String s, char c) {
if (s == null) return 0;
int i = s.lastIndexOf(c);
return i >= 0 ? i : l(s);
}
static boolean ewic(String a, String b) {
return endsWithIgnoreCase(a, b);
}
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 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 Object nuObject(String className, Object... args) { try {
return nuObject(classForName(className), args);
} catch (Exception __e) { throw rethrow(__e); } }
// too ambiguous - maybe need to fix some callers
/*static O nuObject(O realm, S className, O... args) {
ret nuObject(_getClass(realm, className), args);
}*/
static A nuObject(Class c, Object... args) { try {
if (args.length == 0) return nuObjectWithoutArguments(c); // cached!
Constructor m = nuObject_findConstructor(c, args);
makeAccessible(m);
return (A) m.newInstance(args);
} catch (Exception __e) { throw rethrow(__e); } }
static Constructor nuObject_findConstructor(Class c, Object... args) {
for (Constructor m : c.getDeclaredConstructors()) {
if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
continue;
return m;
}
throw fail("Constructor " + c.getName() + getClasses(args) + " not found"
+ (args.length == 0 && (c.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0 ? " - hint: it's a non-static class!" : ""));
}
static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
if (types.length != args.length) {
if (debug)
System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
return false;
}
for (int i = 0; i < types.length; i++)
if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
if (debug)
System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
return false;
}
return true;
}
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 Object callOpt(Object o) {
return callF(o);
}
static A callOpt(Object o, String method, Object... args) {
return (A) callOpt_withVarargs(o, method, args);
}
static boolean callService(String name, Object... args) {
return isTrue(callCreatorOpt("callService", name, args));
}
static String soundPlayMethod() {
if (isLinux()) {
if (onPATH("aplay")) return "aplay";
} else if (isWindows())
return "cmdmp3.exe";
return "JavaSound";
}
static boolean eq(Object a, Object b) {
return a == b || (a == null ? b == null : b != null && a.equals(b));
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
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