allow tasks being hidden in task window

This commit is contained in:
huangyuhui
2017-01-22 17:01:02 +08:00
parent db4615a2d3
commit 0d03e965d2
44 changed files with 278 additions and 306 deletions

View File

@@ -46,7 +46,7 @@ def buildnumber = System.getenv("TRAVIS_BUILD_NUMBER")
if (buildnumber == null)
buildnumber = System.getenv("BUILD_NUMBER")
if (buildnumber == null)
buildnumber = "0"
buildnumber = "1"
def versionroot = System.getenv("VERSION_ROOT")
if (versionroot == null)

View File

@@ -114,7 +114,7 @@ public abstract class IAssetsHandler {
try {
if (location.exists()) {
FileInputStream fis = new FileInputStream(location);
String sha = DigestUtils.sha1Hex(IOUtils.getBytesFromStream(fis));
String sha = DigestUtils.sha1Hex(IOUtils.readFully(fis).toByteArray());
IOUtils.closeQuietly(fis);
if (contents.get(i).geteTag().equals(sha)) {
++hasDownloaded;

View File

@@ -24,9 +24,9 @@ import javax.swing.JOptionPane;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.ArrayUtils;
import org.jackhuang.hellominecraft.launcher.core.auth.yggdrasil.GameProfile;
import org.jackhuang.hellominecraft.launcher.core.auth.yggdrasil.UUIDTypeAdapter;
import org.jackhuang.hellominecraft.launcher.core.auth.yggdrasil.PropertyMap;
import org.jackhuang.hellominecraft.launcher.core.auth.yggdrasil.YggdrasilAuthentication;
import org.jackhuang.hellominecraft.launcher.core.auth.yggdrasil.UUIDTypeAdapter;
import org.jackhuang.hellominecraft.util.ui.SwingUtils;
/**

View File

@@ -140,7 +140,7 @@ public class YggdrasilAuthentication {
private void loggedIn(URL url, Object input) throws AuthenticationException {
try {
String jsonResult = input == null ? NetUtils.get(url) : NetUtils.post(url, GSON.toJson(input), "application/json", proxy);
String jsonResult = input == null ? NetUtils.get(url, proxy) : NetUtils.post(url, GSON.toJson(input), "application/json", proxy);
Response response = (Response) GSON.fromJson(jsonResult, Response.class);
if (response == null)

View File

@@ -21,7 +21,7 @@ import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.NetUtils;
import org.jackhuang.hellominecraft.util.OverridableSwingWorker;
import org.jackhuang.hellominecraft.util.AbstractSwingWorker;
/**
*
@@ -55,7 +55,7 @@ public class MinecraftRemoteVersions {
return new RemoteVersionsTask(type);
}
public static class RemoteVersionsTask extends OverridableSwingWorker<MinecraftRemoteVersion> {
public static class RemoteVersionsTask extends AbstractSwingWorker<MinecraftRemoteVersion> {
DownloadType type;

View File

@@ -28,7 +28,6 @@ import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.launcher.core.service.IMinecraftService;
import org.jackhuang.hellominecraft.util.tasks.Task;
import org.jackhuang.hellominecraft.util.system.FileUtils;
import org.jackhuang.hellominecraft.util.NetUtils;
import org.jackhuang.hellominecraft.launcher.core.version.MinecraftLibrary;
import org.jackhuang.hellominecraft.util.MessageBox;
import org.jackhuang.hellominecraft.util.system.IOUtils;
@@ -55,7 +54,7 @@ public class ForgeInstaller extends Task {
try (ZipFile zipFile = new ZipFile(forgeInstaller)) {
ZipEntry entry = zipFile.getEntry("install_profile.json");
String content = IOUtils.getStreamContent(zipFile.getInputStream(entry));
String content = IOUtils.toString(zipFile.getInputStream(entry));
InstallProfile profile = C.GSON.fromJson(content, InstallProfile.class);
File from = new File(gameDir, "versions" + File.separator + profile.install.getMinecraft());
if (!from.exists())

View File

@@ -47,6 +47,7 @@ import org.jackhuang.hellominecraft.util.system.ZipEngine;
import org.jackhuang.hellominecraft.util.tasks.Task;
import org.jackhuang.hellominecraft.util.ui.WebPage;
import org.jackhuang.hellominecraft.util.MinecraftVersionRequest;
import org.jackhuang.hellominecraft.util.system.IOUtils;
import org.jackhuang.hellominecraft.util.tasks.NoShownTaskException;
/**
@@ -275,8 +276,7 @@ public final class ModpackManager {
if (callback != null)
callback.call(zip);
} finally {
if (zip != null)
zip.closeFile();
IOUtils.closeQuietly(zip);
}
}

View File

@@ -160,7 +160,7 @@ public final class Main implements Runnable {
HMCLog.warn("Failed to set look and feel...", ex);
}
Settings.UPDATE_CHECKER.outdated.register(IUpgrader.NOW_UPGRADER);
Settings.UPDATE_CHECKER.outOfDateEvent.register(IUpgrader.NOW_UPGRADER);
Settings.UPDATE_CHECKER.process(false).reg(t -> Main.invokeUpdate()).execute();
if (StrUtils.isNotBlank(Settings.getInstance().getProxyHost()) && StrUtils.isNotBlank(Settings.getInstance().getProxyPort()) && MathUtils.canParseInt(Settings.getInstance().getProxyPort())) {

View File

@@ -131,7 +131,7 @@ public final class Settings {
}
public static Collection<Profile> getProfilesFiltered() {
return CollectionUtils.map(getProfiles().values(), t -> t != null && t.getName() != null);
return CollectionUtils.filter(getProfiles().values(), t -> t != null && t.getName() != null);
}
public static Profile getOneProfile() {

View File

@@ -107,10 +107,14 @@ public class GameDownloadPanel extends Page {
public void refreshDownloads() {
DefaultTableModel model = SwingUtils.clearDefaultTable(lstDownloads);
model.addRow(new Object[] { C.i18n("message.loading"), "", "" });
MinecraftRemoteVersions.refreshRomoteVersions(Settings.getLastProfile().service().getDownloadType())
.reg((ver) -> model.addRow(new Object[] { ver.id, ver.time,
StrUtils.equalsOne(ver.type, "old_beta", "old_alpha", "release", "snapshot") ? C.i18n("versions." + ver.type) : ver.type }))
.regDone(lstDownloads::requestFocus).execute();
.reg((ver) -> model.addRow(new Object[] { ver.id, ver.time,
StrUtils.equalsOne(ver.type, "old_beta", "old_alpha", "release", "snapshot") ? C.i18n("versions." + ver.type) : ver.type }))
.regDone(() -> {
lstDownloads.requestFocus();
model.removeRow(0);
}).execute();
}
void downloadMinecraft() {

View File

@@ -54,7 +54,7 @@ import org.jackhuang.hellominecraft.launcher.core.version.GameDirType;
import org.jackhuang.hellominecraft.launcher.core.version.MinecraftVersion;
import org.jackhuang.hellominecraft.launcher.setting.VersionSetting;
import org.jackhuang.hellominecraft.util.MessageBox;
import org.jackhuang.hellominecraft.util.OverridableSwingWorker;
import org.jackhuang.hellominecraft.util.AbstractSwingWorker;
import org.jackhuang.hellominecraft.util.MinecraftVersionRequest;
import org.jackhuang.hellominecraft.util.system.OS;
import org.jackhuang.hellominecraft.util.StrUtils;
@@ -1276,7 +1276,7 @@ public final class GameSettingsPanel extends RepaintPage implements DropTargetLi
}
}
private static class OverridableSwingWorkerImpl extends OverridableSwingWorker<List<ModInfo>> {
private static class OverridableSwingWorkerImpl extends AbstractSwingWorker<List<ModInfo>> {
@Override
protected void work() throws Exception {

View File

@@ -348,7 +348,7 @@ public final class MainFrame extends DraggableFrame implements IRepaint {
isShowedMessage = false;
reloadColor(Settings.getInstance().getTheme());
windowTitle.setText(defaultTitle);
windowTitle.setForeground(Settings.UPDATE_CHECKER.OUT_DATED ? Color.red : Color.white);
windowTitle.setForeground(Settings.UPDATE_CHECKER.isOutOfDate() ? Color.red : Color.white);
}
}

View File

@@ -114,33 +114,33 @@ public class ModpackWizard extends WizardBranchController {
boolean including = false;
if ((Boolean) settings.get(ModpackInitializationPanel.KEY_INCLUDING_LAUNCHER)) {
boolean flag = true;
ZipEngine engine = new ZipEngine(loc);
Config s = new Config();
if (!IOUtils.isAbsolutePath(Settings.getInstance().getBgpath()))
s.setBgpath(Settings.getInstance().getBgpath());
s.setDownloadType(Settings.getInstance().getDownloadType());
engine.putTextFile(C.GSON.toJson(s), "hmcl.json");
engine.putFile(modpack, "modpack.zip");
File bg = new File("bg");
if (bg.isDirectory())
engine.putDirectory(bg);
bg = new File("background.png");
if (bg.isFile())
engine.putFile(bg, "background.png");
bg = new File("background.jpg");
if (bg.isFile())
engine.putFile(bg, "background.jpg");
for (URL u : Utils.getURL())
try {
File f = new File(u.toURI());
if (f.getName().endsWith(".exe") || f.getName().endsWith(".jar"))
engine.putFile(f, f.getName());
} catch (IOException | URISyntaxException e) {
HMCLog.err("Failed to add launcher files.", e);
flag = false;
break;
}
engine.closeFile();
try (ZipEngine engine = new ZipEngine(loc)) {
Config s = new Config();
if (!IOUtils.isAbsolutePath(Settings.getInstance().getBgpath()))
s.setBgpath(Settings.getInstance().getBgpath());
s.setDownloadType(Settings.getInstance().getDownloadType());
engine.putTextFile(C.GSON.toJson(s), "hmcl.json");
engine.putFile(modpack, "modpack.zip");
File bg = new File("bg");
if (bg.isDirectory())
engine.putDirectory(bg);
bg = new File("background.png");
if (bg.isFile())
engine.putFile(bg, "background.png");
bg = new File("background.jpg");
if (bg.isFile())
engine.putFile(bg, "background.jpg");
for (URL u : Utils.getURL())
try {
File f = new File(u.toURI());
if (f.getName().endsWith(".exe") || f.getName().endsWith(".jar"))
engine.putFile(f, f.getName());
} catch (IOException | URISyntaxException e) {
HMCLog.err("Failed to add launcher files.", e);
flag = false;
break;
}
}
if (flag) {
including = true;
if (!modpack.delete())

View File

@@ -111,8 +111,8 @@ public class CrashReporter implements Thread.UncaughtExceptionHandler {
System.out.println(text);
if (checkThrowable(e) && !System.getProperty("java.vm.name").contains("OpenJDK")) {
SwingUtilities.invokeLater(() -> LogWindow.INSTANCE.showAsCrashWindow(Settings.UPDATE_CHECKER.OUT_DATED));
if (!Settings.UPDATE_CHECKER.OUT_DATED)
SwingUtilities.invokeLater(() -> LogWindow.INSTANCE.showAsCrashWindow(Settings.UPDATE_CHECKER.isOutOfDate()));
if (!Settings.UPDATE_CHECKER.isOutOfDate())
reportToServer(text, s);
}
} catch (Throwable ex) {

View File

@@ -47,7 +47,7 @@ public class HelloMinecraftLookAndFeel extends SynthLookAndFeel {
public HelloMinecraftLookAndFeel(Map<String, String> settings) throws ParseException {
try {
try (InputStream is = HelloMinecraftLookAndFeel.class.getResourceAsStream("/org/jackhuang/hellominecraft/lookandfeel/synth.xml")) {
String s = IOUtils.getStreamContent(is, "UTF-8");
String s = IOUtils.toString(is, "UTF-8");
for (Map.Entry<String, String> ss : settings.entrySet())
s = s.replace("${" + ss.getKey() + "}", ss.getValue());
load(new ByteArrayInputStream(s.getBytes("UTF-8")), HelloMinecraftLookAndFeel.class);

View File

@@ -66,7 +66,7 @@ public class ForgeInstaller {
ZipFile zipFile = new ZipFile(forgeInstaller);
ZipEntry entry = zipFile.getEntry("install_profile.json");
String content = IOUtils.getStreamContent(zipFile.getInputStream(entry));
String content = IOUtils.toString(zipFile.getInputStream(entry));
InstallProfile profile = gson.fromJson(content, InstallProfile.class);
HMCLog.log("Extracting cauldron server pack..." + profile.install.filePath);
@@ -122,7 +122,7 @@ public class ForgeInstaller {
File packFile = new File(gameDir, "libraries" + File.separator + library.formatted + ".pack.xz");
if (packFile.exists() && packFile.isFile())
try {
unpackLibrary(lib.getParentFile(), IOUtils.getBytesFromStream(FileUtils.openInputStream(packFile)));
unpackLibrary(lib.getParentFile(), IOUtils.readFully(FileUtils.openInputStream(packFile)).toByteArray());
if (!checksumValid(lib, Arrays.asList(library.checksums)))
badLibs.add(library.name);
} catch (IOException e) {
@@ -165,7 +165,7 @@ public class ForgeInstaller {
private static boolean checksumValid(File libPath, List<String> checksums) {
try {
byte[] fileData = IOUtils.getBytesFromStream(FileUtils.openInputStream(libPath));
byte[] fileData = IOUtils.readFully(FileUtils.openInputStream(libPath)).toByteArray();
boolean valid = (checksums == null) || (checksums.isEmpty()) || (checksums.contains(DigestUtils.sha1Hex(fileData)));
if ((!valid) && (libPath.getName().endsWith(".jar")))
valid = validateJar(libPath, fileData, checksums);

View File

@@ -26,6 +26,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
*
@@ -65,12 +66,7 @@ public class ServerProperties {
HMCLog.warn("Failed to get property in server.properties", ex);
return "";
} finally {
try {
if (is != null)
is.close();
} catch (IOException ex) {
HMCLog.warn("Failed to close InputStream for server.properties", ex);
}
IOUtils.closeQuietly(is);
}
}
@@ -98,12 +94,7 @@ public class ServerProperties {
} catch (IOException ex) {
HMCLog.warn("Failed to set property in server.properties", ex);
} finally {
try {
if (is != null)
is.close();
} catch (IOException ex) {
HMCLog.warn("Failed to close OutputStream for server.properties", ex);
}
IOUtils.closeQuietly(is);
}
}

View File

@@ -24,6 +24,7 @@ import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.jackhuang.hellominecraft.util.C;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
* 判断mod类型
@@ -68,14 +69,7 @@ public class ModType {
} catch (Exception e) {
} finally {
try {
if (zipFile != null)
zipFile.close();
} catch (IOException ex) {
Logger.getLogger(ModType.class.getName()).log(Level.SEVERE, null, ex);
} catch (Throwable t) {
}
IOUtils.closeQuietly(zipFile);
}
if (isModLoader)
return MODLOADER_MOD;

View File

@@ -27,6 +27,7 @@ import java.util.StringTokenizer;
import com.sun.management.OperatingSystemMXBean;
import org.jackhuang.hellominecraft.util.StrUtils;
import org.jackhuang.hellominecraft.util.logging.HMCLog;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
*
@@ -39,7 +40,7 @@ public class MonitorServiceImpl implements IMonitorService {
private static final int CPUTIME = 30;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
private static final String linuxVersion = null;
private static final String LINUX_VERSION = null;
/**
* 获得当前的监控对象.
@@ -77,7 +78,7 @@ public class MonitorServiceImpl implements IMonitorService {
else if (osName.toLowerCase().startsWith("mac"))
cpuRatio = this.getCpuRatioForMac();
else
cpuRatio = this.getCpuRatioForLinux();
cpuRatio = getCpuRatioForLinux();
// 构造返回对象
MonitorInfoBean infoBean = new MonitorInfoBean();
infoBean.setFreeMemory(freeMemory);
@@ -100,20 +101,22 @@ public class MonitorServiceImpl implements IMonitorService {
String command = "cat /proc/stat";
long startTime = System.currentTimeMillis();
pro1 = r.exec(command);
BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream()));
String line = null;
long idleCpuTime1 = 0, totalCpuTime1 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while ((line = in1.readLine()) != null)
if (line.startsWith("cpu")) {
line = line.trim();
String[] temp = line.split("\\s+");
idleCpuTime1 = Long.parseLong(temp[4]);
for (String s : temp)
if (!s.equals("cpu"))
totalCpuTime1 += Long.parseLong(s);
break;
}
in1.close();
String line;
long idleCpuTime1, totalCpuTime1; //分别为系统启动后空闲的CPU时间和总的CPU时间
try (BufferedReader in1 = new BufferedReader(new InputStreamReader(pro1.getInputStream()))) {
idleCpuTime1 = 0;
totalCpuTime1 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while ((line = in1.readLine()) != null)
if (line.startsWith("cpu")) {
line = line.trim();
String[] temp = line.split("\\s+");
idleCpuTime1 = Long.parseLong(temp[4]);
for (String s : temp)
if (!s.equals("cpu"))
totalCpuTime1 += Long.parseLong(s);
break;
}
}
pro1.destroy();
try {
Thread.sleep(100);
@@ -123,21 +126,21 @@ public class MonitorServiceImpl implements IMonitorService {
//第二次采集CPU时间
long endTime = System.currentTimeMillis();
pro2 = r.exec(command);
BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream()));
long idleCpuTime2 = 0, totalCpuTime2 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while ((line = in2.readLine()) != null)
if (line.startsWith("cpu")) {
line = line.trim();
String[] temp = line.split("\\s+");
idleCpuTime2 = Long.parseLong(temp[4]);
for (String s : temp)
if (!s.equals("cpu"))
totalCpuTime2 += Long.parseLong(s);
break;
}
if (idleCpuTime1 != 0 && totalCpuTime1 != 0 && idleCpuTime2 != 0 && totalCpuTime2 != 0)
cpuUsage = 1 - (float) (idleCpuTime2 - idleCpuTime1) / (float) (totalCpuTime2 - totalCpuTime1);
in2.close();
try (BufferedReader in2 = new BufferedReader(new InputStreamReader(pro2.getInputStream()))) {
long idleCpuTime2 = 0, totalCpuTime2 = 0; //分别为系统启动后空闲的CPU时间和总的CPU时间
while ((line = in2.readLine()) != null)
if (line.startsWith("cpu")) {
line = line.trim();
String[] temp = line.split("\\s+");
idleCpuTime2 = Long.parseLong(temp[4]);
for (String s : temp)
if (!s.equals("cpu"))
totalCpuTime2 += Long.parseLong(s);
break;
}
if (idleCpuTime1 != 0 && totalCpuTime1 != 0 && idleCpuTime2 != 0 && totalCpuTime2 != 0)
cpuUsage = 1 - (float) (idleCpuTime2 - idleCpuTime1) / (float) (totalCpuTime2 - totalCpuTime1);
}
pro2.destroy();
} catch (IOException e) {
HMCLog.err("Failed to catch sysout", e);
@@ -174,21 +177,9 @@ public class MonitorServiceImpl implements IMonitorService {
System.out.println(ioe.getMessage());
return 1;
} finally {
freeResource(is, isr, brStat);
}
}
private static void freeResource(InputStream is, InputStreamReader isr,
BufferedReader br) {
try {
if (is != null)
is.close();
if (isr != null)
isr.close();
if (br != null)
br.close();
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(isr);
IOUtils.closeQuietly(brStat);
}
}
@@ -217,6 +208,14 @@ public class MonitorServiceImpl implements IMonitorService {
return 0.0;
}
}
public static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++)
tgt += (char) b[i];
return tgt;
}
/**
* 读取CPU信息.
@@ -250,12 +249,12 @@ public class MonitorServiceImpl implements IMonitorService {
continue;
// 字段出现顺序Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = StrUtils.substring(line, capidx, cmdidx - 1).trim();
String cmd = StrUtils.substring(line, cmdidx, kmtidx - 1).trim();
String caption = substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.contains("wmic.exe"))
continue;
String s1 = StrUtils.substring(line, kmtidx, rocidx - 1).trim();
String s2 = StrUtils.substring(line, umtidx, wocidx - 1).trim();
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process") || caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.parseLong(s1);
@@ -274,11 +273,7 @@ public class MonitorServiceImpl implements IMonitorService {
} catch (IOException | NumberFormatException ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
IOUtils.closeQuietly(proc.getInputStream());
}
return null;
}

View File

@@ -28,7 +28,7 @@ import org.jackhuang.hellominecraft.util.func.Consumer;
*
* @author huangyuhui
*/
public abstract class OverridableSwingWorker<T> extends SwingWorker<Void, T> {
public abstract class AbstractSwingWorker<T> extends SwingWorker<Void, T> {
List<Consumer<T>> processListeners = new ArrayList<>();
List<Runnable> doneListeners = new ArrayList<>();
@@ -47,13 +47,13 @@ public abstract class OverridableSwingWorker<T> extends SwingWorker<Void, T> {
return null;
}
public OverridableSwingWorker reg(Consumer<T> c) {
public AbstractSwingWorker reg(Consumer<T> c) {
Objects.requireNonNull(c);
processListeners.add(c);
return this;
}
public OverridableSwingWorker regDone(Runnable c) {
public AbstractSwingWorker regDone(Runnable c) {
Objects.requireNonNull(c);
doneListeners.add(c);
return this;

View File

@@ -31,7 +31,7 @@ public final class CollectionUtils {
private CollectionUtils() {
}
public static <T> ArrayList<T> map(Collection<T> coll, Predicate<T> p) {
public static <T> ArrayList<T> filter(Collection<T> coll, Predicate<T> p) {
ArrayList<T> newColl = new ArrayList<>();
for (T t : coll)
if (p.apply(t))

View File

@@ -26,47 +26,47 @@ import java.io.OutputStream;
*/
public class DoubleOutputStream extends OutputStream {
private OutputStream a = null;
private OutputStream b = null;
private boolean c = true;
private OutputStream os1 = null;
private OutputStream os2 = null;
private boolean autoFlush = true;
public DoubleOutputStream(OutputStream paramOutputStream1, OutputStream paramOutputStream2) {
this(paramOutputStream1, paramOutputStream2, true);
public DoubleOutputStream(OutputStream os1, OutputStream os2) {
this(os1, os2, true);
}
private DoubleOutputStream(OutputStream paramOutputStream1, OutputStream paramOutputStream2, boolean paramBoolean) {
this.a = paramOutputStream1;
this.b = paramOutputStream2;
this.c = true;
private DoubleOutputStream(OutputStream os1, OutputStream os2, boolean autoFlush) {
this.os1 = os1;
this.os2 = os2;
this.autoFlush = autoFlush;
}
@Override
public final void write(byte[] arr, int off, int len) throws IOException {
if (this.a != null)
this.a.write(arr, off, len);
if (this.b != null)
this.b.write(arr, off, len);
if (this.c)
if (this.os1 != null)
this.os1.write(arr, off, len);
if (this.os2 != null)
this.os2.write(arr, off, len);
if (this.autoFlush)
flush();
}
@Override
public final void write(byte[] arr) throws IOException {
if (this.a != null)
this.a.write(arr);
if (this.b != null)
this.b.write(arr);
if (this.c)
if (this.os1 != null)
this.os1.write(arr);
if (this.os2 != null)
this.os2.write(arr);
if (this.autoFlush)
flush();
}
@Override
public final void write(int i) throws IOException {
if (this.a != null)
this.a.write(i);
if (this.b != null)
this.b.write(i);
if (this.c)
if (this.os1 != null)
this.os1.write(i);
if (this.os2 != null)
this.os2.write(i);
if (this.autoFlush)
flush();
}
@@ -74,17 +74,17 @@ public class DoubleOutputStream extends OutputStream {
public final void close() throws IOException {
flush();
if (this.a != null)
this.a.close();
if (this.b != null)
this.b.close();
if (this.os1 != null)
this.os1.close();
if (this.os2 != null)
this.os2.close();
}
@Override
public final void flush() throws IOException {
if (this.a != null)
this.a.flush();
if (this.b != null)
this.b.flush();
if (this.os1 != null)
this.os1.flush();
if (this.os2 != null)
this.os2.flush();
}
}

View File

@@ -49,13 +49,13 @@ public interface IUpdateChecker {
*
* @return the process observable.
*/
OverridableSwingWorker<VersionNumber> process(boolean showMessage);
AbstractSwingWorker<VersionNumber> process(boolean showMessage);
/**
* Get the download links.
*
* @return a JSON, which contains the server response.
*/
OverridableSwingWorker<Map<String, String>> requestDownloadLink();
AbstractSwingWorker<Map<String, String>> requestDownloadLink();
}

View File

@@ -68,7 +68,7 @@ public class MinecraftVersionRequest implements Serializable {
private static MinecraftVersionRequest getVersionOfOldMinecraft(ZipFile file, ZipEntry entry) throws IOException {
MinecraftVersionRequest r = new MinecraftVersionRequest();
byte[] tmp = IOUtils.getBytesFromStream(file.getInputStream(entry));
byte[] tmp = IOUtils.readFully(file.getInputStream(entry)).toByteArray();
byte[] bytes = "Minecraft Minecraft ".getBytes("ASCII");
int j = ArrayUtils.matchArray(tmp, bytes);
@@ -92,7 +92,7 @@ public class MinecraftVersionRequest implements Serializable {
private static MinecraftVersionRequest getVersionOfNewMinecraft(ZipFile file, ZipEntry entry) throws IOException {
MinecraftVersionRequest r = new MinecraftVersionRequest();
byte[] tmp = IOUtils.getBytesFromStream(file.getInputStream(entry));
byte[] tmp = IOUtils.readFully(file.getInputStream(entry)).toByteArray();
byte[] str = "-server.txt".getBytes("ASCII");
int j = ArrayUtils.matchArray(tmp, str);
@@ -175,12 +175,7 @@ public class MinecraftVersionRequest implements Serializable {
r.type = MinecraftVersionRequest.INVALID_JAR;
return r;
} finally {
if (f != null)
try {
f.close();
} catch (IOException ex) {
HMCLog.warn("Failed to close zip file", ex);
}
IOUtils.closeQuietly(f);
}
}
}

View File

@@ -26,6 +26,7 @@ import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.util.Map;
import org.jackhuang.hellominecraft.util.code.Charsets;
import org.jackhuang.hellominecraft.util.system.IOUtils;
/**
@@ -33,12 +34,22 @@ import org.jackhuang.hellominecraft.util.system.IOUtils;
* @author huang
*/
public final class NetUtils {
private NetUtils() {
}
private static HttpURLConnection createConnection(URL url, Proxy proxy) throws IOException {
HttpURLConnection con = (HttpURLConnection) url.openConnection(proxy);
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setConnectTimeout(15000);
con.setReadTimeout(15000);
return con;
}
public static String get(String url, String encoding) throws IOException {
return IOUtils.getStreamContent(new URL(url).openConnection().getInputStream());
return IOUtils.toString(new URL(url).openConnection().getInputStream());
}
public static String get(String url) throws IOException {
@@ -50,7 +61,7 @@ public final class NetUtils {
}
public static String get(URL url, Proxy proxy) throws IOException {
return IOUtils.getStreamContent(url.openConnection(proxy).getInputStream());
return readData(createConnection(url, proxy));
}
public static String post(URL u, Map<String, String> params) throws IOException {
@@ -72,17 +83,13 @@ public final class NetUtils {
}
public static String post(URL u, String post, String contentType, Proxy proxy) throws IOException {
HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
byte[] bytes = post.getBytes(Charsets.UTF_8);
HttpURLConnection con = createConnection(u, proxy);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setConnectTimeout(30000);
con.setReadTimeout(30000);
con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
byte[] bytes = post.getBytes(IOUtils.DEFAULT_CHARSET);
con.setRequestProperty("Content-Length", "" + bytes.length);
con.connect();
OutputStream os = null;
try {
os = con.getOutputStream();
@@ -90,20 +97,23 @@ public final class NetUtils {
} finally {
IOUtils.closeQuietly(os);
}
return readData(con);
}
String result;
private static String readData(HttpURLConnection con) throws IOException {
InputStream is = null;
try {
is = con.getInputStream();
result = IOUtils.getStreamContent(is);
} catch (IOException ex) {
return IOUtils.toString(is, Charsets.UTF_8);
} catch (IOException e) {
IOUtils.closeQuietly(is);
is = con.getErrorStream();
result = IOUtils.getStreamContent(is);
if (is != null)
return IOUtils.toString(is, Charsets.UTF_8);
throw e;
} finally {
IOUtils.closeQuietly(is);
}
con.disconnect();
return result;
}
public static URL constantURL(String url) {
@@ -117,9 +127,7 @@ public final class NetUtils {
public static URL concatenateURL(URL url, String query) {
try {
if (url.getQuery() != null && url.getQuery().length() > 0)
return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "&" + query);
return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "?" + query);
return new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + (url.getQuery() != null && url.getQuery().length() > 0 ? '&' : '?') + query);
} catch (MalformedURLException ex) {
throw new IllegalArgumentException("Could not concatenate given URL with GET arguments!", ex);
}

View File

@@ -37,14 +37,6 @@ public final class StrUtils {
private StrUtils() {
}
public static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++)
tgt += (char) b[i];
return tgt;
}
public static String makeCommand(List<String> cmd) {
StringBuilder cmdbuf = new StringBuilder(120);
for (int i = 0; i < cmd.size(); i++) {
@@ -189,7 +181,7 @@ public final class StrUtils {
public static boolean equals(String base, String to) {
if (base == null)
return (to == null);
return to == null;
else
return base.equals(to);
}
@@ -220,15 +212,6 @@ public final class StrUtils {
return (String[]) localArrayList.toArray(new String[localArrayList.size()]);
}
public static String trimExtension(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int i = filename.lastIndexOf('.');
if ((i > -1) && (i < (filename.length())))
return filename.substring(0, i);
}
return filename;
}
public static boolean isBlank(String s) {
return s == null || s.trim().length() <= 0;
}

View File

@@ -28,7 +28,7 @@ import java.util.Map;
*/
public final class UpdateChecker implements IUpdateChecker {
public boolean OUT_DATED = false;
private volatile boolean outOfDate = false;
public VersionNumber base;
public String versionString;
public String type;
@@ -41,9 +41,13 @@ public final class UpdateChecker implements IUpdateChecker {
VersionNumber value;
public boolean isOutOfDate() {
return outOfDate;
}
@Override
public OverridableSwingWorker<VersionNumber> process(final boolean showMessage) {
return new OverridableSwingWorker() {
public AbstractSwingWorker<VersionNumber> process(final boolean showMessage) {
return new AbstractSwingWorker() {
@Override
protected void work() throws Exception {
if (value == null) {
@@ -56,8 +60,8 @@ public final class UpdateChecker implements IUpdateChecker {
if (showMessage)
MessageBox.show(C.i18n("update.failed"));
} else if (VersionNumber.isOlder(base, value))
OUT_DATED = true;
if (OUT_DATED)
outOfDate = true;
if (outOfDate)
publish(value);
}
};
@@ -69,8 +73,8 @@ public final class UpdateChecker implements IUpdateChecker {
}
@Override
public synchronized OverridableSwingWorker<Map<String, String>> requestDownloadLink() {
return new OverridableSwingWorker() {
public synchronized AbstractSwingWorker<Map<String, String>> requestDownloadLink() {
return new AbstractSwingWorker() {
@Override
protected void work() throws Exception {
if (download_link == null)
@@ -84,11 +88,11 @@ public final class UpdateChecker implements IUpdateChecker {
};
}
public final EventHandler<VersionNumber> outdated = new EventHandler<>(this);
public final EventHandler<VersionNumber> outOfDateEvent = new EventHandler<>(this);
@Override
public void checkOutdate() {
if (OUT_DATED)
outdated.execute(getNewVersion());
if (outOfDate)
outOfDateEvent.execute(getNewVersion());
}
}

View File

@@ -36,13 +36,17 @@ public final class Localization {
private static final Map<Locale, Localization> INSTANCE = new HashMap<>();
private final Map<String, String> lang;
private static InputStream getStream(String id) {
return Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, id));
}
private Localization(Locale locale) {
InputStream is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, "_" + locale.getLanguage() + "_" + locale.getCountry()));
InputStream is = getStream("_" + locale.getLanguage() + "_" + locale.getCountry());
if (is == null)
is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, "_" + locale.getLanguage()));
is = getStream("_" + locale.getLanguage());
if (is == null)
is = Localization.class.getResourceAsStream(String.format(ROOT_LOCATION, ""));
is = getStream("");
if (is == null)
throw new RuntimeException("LANG FILE MISSING");

View File

@@ -195,12 +195,12 @@ public final class FileUtils {
public static String read(File file)
throws IOException {
return IOUtils.getStreamContent(IOUtils.openInputStream(file));
return IOUtils.toString(IOUtils.openInputStream(file));
}
public static String readQuietly(File file) {
try {
return IOUtils.getStreamContent(IOUtils.openInputStream(file));
return IOUtils.toString(IOUtils.openInputStream(file));
} catch (IOException ex) {
HMCLog.err("Failed to read file: " + file, ex);
return null;
@@ -209,12 +209,12 @@ public final class FileUtils {
public static String read(File file, String charset)
throws IOException {
return IOUtils.getStreamContent(IOUtils.openInputStream(file), charset);
return IOUtils.toString(IOUtils.openInputStream(file), charset);
}
public static String readIgnoreFileNotFound(File file) throws IOException {
try {
return IOUtils.getStreamContent(IOUtils.openInputStream(file));
return IOUtils.toString(IOUtils.openInputStream(file));
} catch (FileNotFoundException ex) {
return "";
}
@@ -263,14 +263,6 @@ public final class FileUtils {
return Math.max(lastUnixPos, lastWindowsPos);
}
public static int indexOfExtension(String filename) {
if (filename == null)
return -1;
int extensionPos = filename.lastIndexOf(46);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? -1 : extensionPos;
}
public static String getName(String filename) {
if (filename == null)
return null;
@@ -289,6 +281,14 @@ public final class FileUtils {
return removeExtension(getName(filename));
}
public static int indexOfExtension(String filename) {
if (filename == null)
return -1;
int extensionPos = filename.lastIndexOf(46);
int lastSeparator = indexOfLastSeparator(filename);
return lastSeparator > extensionPos ? -1 : extensionPos;
}
public static String getExtension(String filename) {
if (filename == null)
return null;
@@ -333,7 +333,6 @@ public final class FileUtils {
try {
out = openOutputStream(file, append);
IOUtils.write(data, out, encoding);
out.close();
} finally {
IOUtils.closeQuietly(out);
}

View File

@@ -202,6 +202,22 @@ public final class IOUtils {
return entryBuffer;
}
public static byte[] toByteArray(InputStream stream) throws IOException {
return readFully(stream).toByteArray();
}
public static String toString(InputStream is) throws IOException {
return readFully(is).toString();
}
public static String toString(InputStream is, String charset) throws IOException {
return readFully(is).toString(charset);
}
public static String toString(InputStream is, Charset charset) throws IOException {
return readFully(is).toString(charset.name());
}
public static void closeQuietly(Closeable closeable) {
try {
@@ -302,31 +318,6 @@ public final class IOUtils {
output.write(buf, 0, length);
}
public static byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copyStream(is, out);
is.close();
return out.toByteArray();
}
public static String getStreamContent(InputStream is) throws IOException {
return getStreamContent(is, DEFAULT_CHARSET);
}
public static String getStreamContent(InputStream is, String encoding)
throws IOException {
if (is == null)
return null;
StringBuilder sb = new StringBuilder();
try (InputStreamReader br = new InputStreamReader(is, encoding)) {
int len;
char[] buf = new char[16384];
while ((len = br.read(buf)) != -1)
sb.append(buf, 0, len);
}
return sb.toString();
}
public static final String DEFAULT_CHARSET = "UTF-8";
public static PrintStream createPrintStream(OutputStream out, Charset charset) {

View File

@@ -78,9 +78,8 @@ public class JavaProcessMonitor {
al.add(a);
}
void processThreadStopped(ProcessThread t, boolean forceTermintate) {
al.remove(t);
al.removeAll(CollectionUtils.map(al, t1 -> !t1.isAlive()));
void processThreadStopped(ProcessThread t1, boolean forceTermintate) {
CollectionUtils.removeIf(al, t -> t == t1 || !t.isAlive());
if (al.isEmpty() || forceTermintate) {
for (Thread a : al)
a.interrupt();

View File

@@ -17,6 +17,7 @@
*/
package org.jackhuang.hellominecraft.util.system;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -46,35 +47,29 @@ public class ProcessThread extends Thread {
@Override
public void run() {
setName("ProcessMonitor");
InputStreamReader br = null;
BufferedReader br = null;
try {
InputStream in = p.getRawProcess().getInputStream();
try {
br = new InputStreamReader(in, System.getProperty("sun.jnu.encoding", "UTF-8"));
br = new BufferedReader(new InputStreamReader(in, System.getProperty("sun.jnu.encoding", "UTF-8")));
} catch (UnsupportedEncodingException ex) {
HMCLog.warn("Unsupported encoding: " + System.getProperty("sun.jnu.encoding", "UTF-8"), ex);
br = new InputStreamReader(in);
br = new BufferedReader(new InputStreamReader(in));
}
int ch;
String line = "";
while (p.isRunning())
while ((ch = br.read()) != -1)
if (ch == '\n') {
printlnEvent.execute(line);
System.out.println("Minecraft: " + line);
p.getStdOutLines().add(line);
line = "";
} else
line += (char) ch;
while ((ch = br.read()) != -1)
if (ch == '\n') {
while ((line = br.readLine()) != null) {
printlnEvent.execute(line);
System.out.println("Minecraft: " + line);
p.getStdOutLines().add(line);
line = "";
} else
line += (char) ch;
}
while ((line = br.readLine()) != null) {
printlnEvent.execute(line);
System.out.println("Minecraft: " + line);
p.getStdOutLines().add(line);
}
stopEvent.execute(p);
} catch (IOException e) {
HMCLog.err("An error occured when reading process stdout/stderr.", e);

View File

@@ -19,6 +19,7 @@ package org.jackhuang.hellominecraft.util.system;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -34,7 +35,7 @@ import org.jackhuang.hellominecraft.util.func.BiFunction;
*
* @author huangyuhui
*/
public class ZipEngine {
public class ZipEngine implements Closeable {
byte[] buf = new byte[1024];
ZipOutputStream zos;
@@ -44,7 +45,8 @@ public class ZipEngine {
zos = new ZipOutputStream(new BufferedOutputStream(os));
}
public void closeFile() throws IOException {
@Override
public void close() throws IOException {
zos.closeEntry();
zos.close();
}

View File

@@ -36,6 +36,7 @@ public class DoubleTask extends TaskInfo {
super(info);
this.a = a;
this.b = b;
hidden = true;
}
@Override

View File

@@ -28,6 +28,10 @@ public class ParallelTask extends Task {
Collection<Task> dependsTask = new HashSet<>();
public ParallelTask() {
hidden = true;
}
@Override
public void executeTask(boolean areDependTasksSucceeded) {
}

View File

@@ -50,6 +50,12 @@ public abstract class Task {
public boolean isAborted() {
return aborted;
}
protected boolean hidden = false;
public boolean isHidden() {
return hidden;
}
public Throwable getFailReason() {
return failReason;

View File

@@ -218,7 +218,7 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void onDoing(Task task, Collection<Task> taskCollection) {
if (task == null)
if (task == null || task.isHidden())
return;
task.setProgressProviderListener(this);
@@ -238,8 +238,10 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void onDone(Task task, Collection<Task> taskCollection) {
if (task == null || task.isHidden())
return;
SwingUtilities.invokeLater(() -> {
if (taskList == null || task == null)
if (taskList == null)
return;
pgsTotal.setMaximum(taskList.taskCount());
pgsTotal.setValue(pgsTotal.getValue() + 1);
@@ -254,8 +256,10 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void onFailed(Task task) {
if (task == null || task.isHidden())
return;
SwingUtilities.invokeLater(() -> {
if (taskList == null || task == null)
if (taskList == null)
return;
String msg = null;
if (task.getFailReason() != null && !(task.getFailReason() instanceof NoShownTaskException))
@@ -283,13 +287,14 @@ public class TaskWindow extends javax.swing.JDialog
@Override
public void setStatus(Task task, String sta) {
if (task == null || task.isHidden())
return;
SwingUtilities.invokeLater(() -> {
if (taskList == null || task == null)
if (taskList == null)
return;
int idx = tasks.indexOf(task);
if (idx == -1)
return;
SwingUtils.setValueAt(lstDownload, task.getInfo() + ": " + sta, idx, 0);
if (idx != -1)
SwingUtils.setValueAt(lstDownload, task.getInfo() + ": " + sta, idx, 0);
});
}

View File

@@ -92,23 +92,10 @@ public class FileDownloadTask extends Task implements PreviousResult<File>, Prev
boolean shouldContinue = true;
private void closeFiles() {
// Close file.
if (file != null)
try {
file.close();
file = null;
} catch (IOException e) {
HMCLog.warn("Failed to close file", e);
}
// Close connection to server.
if (stream != null)
try {
stream.close();
stream = null;
} catch (IOException e) {
HMCLog.warn("Failed to close stream", e);
}
IOUtils.closeQuietly(file);
file = null;
IOUtils.closeQuietly(stream);
stream = null;
}
// Download file.

View File

@@ -187,6 +187,7 @@ message.error=错误
message.cannot_open_explorer=无法打开文件管理器:
message.cancelled=已取消
message.info=提示
message.loading=加载中...
folder.game=游戏文件夹
folder.mod=MOD文件夹

View File

@@ -187,6 +187,7 @@ message.error=\u9519\u8bef
message.cannot_open_explorer=\u65e0\u6cd5\u6253\u5f00\u6587\u4ef6\u7ba1\u7406\u5668:
message.cancelled=\u5df2\u53d6\u6d88
message.info=\u63d0\u793a
message.loading=\u52a0\u8f7d\u4e2d...
folder.game=\u6e38\u620f\u6587\u4ef6\u5939
folder.mod=MOD\u6587\u4ef6\u5939

View File

@@ -187,6 +187,7 @@ message.error=Error
message.cannot_open_explorer=Cannot open explorer:
message.cancelled=Cancelled
message.info=Info
message.loading=Loading...
folder.game=Game Dir
folder.mod=Mod

View File

@@ -187,6 +187,7 @@ message.error=Error
message.cannot_open_explorer=Cannot open explorer:
message.cancelled=Cancelled
message.info=Info
message.loading=Loading...
folder.game=Game Dir
folder.mod=Mod

View File

@@ -187,6 +187,7 @@ message.error=錯誤
message.cannot_open_explorer=無法打開資料管理器:
message.cancelled=已取消
message.info=提示
message.loading=加載中...
folder.game=遊戲資料夾
folder.mod=MOD資料夾

View File

@@ -187,6 +187,7 @@ message.error=\u932f\u8aa4
message.cannot_open_explorer=\u7121\u6cd5\u6253\u958b\u8cc7\u6599\u7ba1\u7406\u5668:
message.cancelled=\u5df2\u53d6\u6d88
message.info=\u63d0\u793a
message.loading=\u52a0\u8f09\u4e2d...
folder.game=\u904a\u6232\u8cc7\u6599\u593e
folder.mod=MOD\u8cc7\u6599\u593e