clean
This commit is contained in:
@@ -33,7 +33,7 @@ public final class CollectionUtils {
|
||||
p.accept(t);
|
||||
}
|
||||
|
||||
public static <T> Collection<T> sortOut(Collection<T> coll, Predicate<T> p) {
|
||||
public static <T> ArrayList<T> map(Collection<T> coll, Predicate<T> p) {
|
||||
ArrayList<T> newColl = new ArrayList<>();
|
||||
forEach(coll, t -> {
|
||||
if (p.apply(t))
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -26,7 +25,6 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.Proxy;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Map;
|
||||
import org.jackhuang.hellominecraft.HMCLog;
|
||||
import org.jackhuang.hellominecraft.utils.system.IOUtils;
|
||||
@@ -63,64 +61,23 @@ public final class NetUtils {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String doGet(String url, String encoding) throws IOException {
|
||||
public static String get(String url, String encoding) throws IOException {
|
||||
return getStreamContent(new URL(url).openConnection().getInputStream());
|
||||
}
|
||||
|
||||
public static String doGet(String url) throws IOException {
|
||||
return doGet(url, DEFAULT_CHARSET);
|
||||
public static String get(String url) throws IOException {
|
||||
return get(url, DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
public static String doGet(URL url) throws IOException {
|
||||
return doGet(url, Proxy.NO_PROXY);
|
||||
public static String get(URL url) throws IOException {
|
||||
return get(url, Proxy.NO_PROXY);
|
||||
}
|
||||
|
||||
public static String doGet(URL url, Proxy proxy) throws IOException {
|
||||
public static String get(URL url, Proxy proxy) throws IOException {
|
||||
return getStreamContent(url.openConnection(proxy).getInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an HTTP GET request to a url
|
||||
*
|
||||
* @param endpoint - The URL of the server. (Example: "
|
||||
* http://www.yahoo.com/search")
|
||||
* @param requestParameters - all the request parameters (Example:
|
||||
* "param1=val1¶m2=val2"). Note: This method will add the question mark
|
||||
* (?) to the request - DO NOT add it yourself
|
||||
*
|
||||
* @return - The response from the end point
|
||||
*/
|
||||
public static String sendGetRequest(String endpoint,
|
||||
String requestParameters) {
|
||||
String result = null;
|
||||
if (endpoint.startsWith("http://"))
|
||||
// Send a GET request to the servlet
|
||||
try {
|
||||
// Construct data
|
||||
StringBuilder data = new StringBuilder();
|
||||
// Send data
|
||||
String urlStr = endpoint;
|
||||
if (requestParameters != null && requestParameters.length() > 0)
|
||||
urlStr += "?" + requestParameters;
|
||||
URL url = new URL(urlStr);
|
||||
URLConnection conn = url.openConnection();
|
||||
|
||||
// Get the response
|
||||
InputStreamReader r = new InputStreamReader(conn.getInputStream());
|
||||
StringBuffer sb;
|
||||
BufferedReader rd = new BufferedReader(r);
|
||||
sb = new StringBuffer();
|
||||
String line;
|
||||
while ((line = rd.readLine()) != null)
|
||||
sb.append(line);
|
||||
result = sb.toString();
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to send get request.", e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String post(URL u, Map<String, String> params) {
|
||||
public static String post(URL u, Map<String, String> params) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (params != null) {
|
||||
for (Map.Entry<String, String> e : params.entrySet()) {
|
||||
@@ -134,54 +91,49 @@ public final class NetUtils {
|
||||
return post(u, sb.toString());
|
||||
}
|
||||
|
||||
public static String post(URL u, String post) {
|
||||
public static String post(URL u, String post) throws IOException {
|
||||
return post(u, post, "application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
public static String post(URL u, String post, String contentType) {
|
||||
public static String post(URL u, String post, String contentType) throws IOException {
|
||||
return post(u, post, contentType, Proxy.NO_PROXY);
|
||||
}
|
||||
|
||||
public static String post(URL u, String post, String contentType, Proxy proxy) {
|
||||
public static String post(URL u, String post, String contentType, Proxy proxy) throws IOException {
|
||||
HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setConnectTimeout(15000);
|
||||
con.setReadTimeout(15000);
|
||||
con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
|
||||
con.setRequestProperty("Content-Length", "" + post.getBytes(DEFAULT_CHARSET).length);
|
||||
OutputStream os = null;
|
||||
try {
|
||||
HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
|
||||
con.setRequestMethod(METHOD_POST);
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setConnectTimeout(15000);
|
||||
con.setReadTimeout(15000);
|
||||
con.setRequestProperty("Content-Type", contentType + "; charset=utf-8");
|
||||
con.setRequestProperty("Content-Length", "" + post.getBytes(DEFAULT_CHARSET).length);
|
||||
OutputStream os = null;
|
||||
try {
|
||||
os = con.getOutputStream();
|
||||
IOUtils.write(post, os, DEFAULT_CHARSET);
|
||||
} finally {
|
||||
if (os != null)
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
|
||||
String result = null;
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = con.getInputStream();
|
||||
result = getStreamContent(is);
|
||||
} catch (IOException ex) {
|
||||
if (is != null)
|
||||
IOUtils.closeQuietly(is);
|
||||
is = con.getErrorStream();
|
||||
result = getStreamContent(is);
|
||||
}
|
||||
|
||||
con.disconnect();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to post.", e);
|
||||
return null;
|
||||
os = con.getOutputStream();
|
||||
IOUtils.write(post, os, DEFAULT_CHARSET);
|
||||
} finally {
|
||||
if (os != null)
|
||||
IOUtils.closeQuietly(os);
|
||||
}
|
||||
|
||||
String result;
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = con.getInputStream();
|
||||
result = getStreamContent(is);
|
||||
} catch (IOException ex) {
|
||||
if (is != null)
|
||||
IOUtils.closeQuietly(is);
|
||||
is = con.getErrorStream();
|
||||
result = getStreamContent(is);
|
||||
}
|
||||
|
||||
con.disconnect();
|
||||
return result;
|
||||
}
|
||||
private static final String METHOD_POST = "POST";
|
||||
|
||||
private static final String DEFAULT_CHARSET = "UTF-8";
|
||||
|
||||
public static URL constantURL(String url) {
|
||||
|
||||
@@ -47,7 +47,7 @@ public final class UpdateChecker extends Thread {
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
versionString = NetUtils.doGet("http://huangyuhui.duapp.com/info.php?type=" + type);
|
||||
versionString = NetUtils.get("http://huangyuhui.duapp.com/info.php?type=" + type);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update url.", e);
|
||||
return;
|
||||
@@ -75,7 +75,7 @@ public final class UpdateChecker extends Thread {
|
||||
new Thread(() -> {
|
||||
if (download_link == null)
|
||||
try {
|
||||
download_link = C.gson.fromJson(NetUtils.doGet("http://huangyuhui.duapp.com/update_link.php?type=" + type), Map.class);
|
||||
download_link = C.gson.fromJson(NetUtils.get("http://huangyuhui.duapp.com/update_link.php?type=" + type), Map.class);
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to get update link.", e);
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class JavaProcessMonitor {
|
||||
|
||||
void processThreadStopped(ProcessThread t, boolean forceTermintate) {
|
||||
al.remove(t);
|
||||
al.removeAll(CollectionUtils.sortOut(al, t1 -> !t1.isAlive()));
|
||||
al.removeAll(CollectionUtils.map(al, t1 -> !t1.isAlive()));
|
||||
if (al.isEmpty() || forceTermintate) {
|
||||
for (Thread a : al)
|
||||
a.interrupt();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<Properties>
|
||||
<Property name="defaultCloseOperation" type="int" value="2"/>
|
||||
<Property name="title" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.title" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.title" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<SyntheticProperties>
|
||||
@@ -83,7 +83,7 @@
|
||||
<Component class="javax.swing.JButton" name="btnClear">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.clear" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.clear" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
@@ -93,7 +93,7 @@
|
||||
<Component class="javax.swing.JButton" name="btnClose">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.close" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.close" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
@@ -103,7 +103,7 @@
|
||||
<Component class="javax.swing.JButton" name="btnCopy">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.copy" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="ui.button.copy" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
@@ -128,7 +128,7 @@
|
||||
<Component class="javax.swing.JButton" name="btnTieBa">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.tieba" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.tieba" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
@@ -146,7 +146,7 @@
|
||||
<Component class="javax.swing.JButton" name="btnTerminateGame">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.terminate_game" replaceFormat="C.I18N.getString("{key}")"/>
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="logwindow.terminate_game" replaceFormat="C.i18n("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.views;
|
||||
|
||||
import java.awt.Color;
|
||||
import javax.swing.text.BadLocationException;
|
||||
import javax.swing.text.Document;
|
||||
import javax.swing.text.SimpleAttributeSet;
|
||||
@@ -80,28 +79,28 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
txtLog = new javax.swing.JTextPane();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
|
||||
setTitle(C.I18N.getString("logwindow.title")); // NOI18N
|
||||
setTitle(C.i18n("logwindow.title")); // NOI18N
|
||||
addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
public void windowClosed(java.awt.event.WindowEvent evt) {
|
||||
formWindowClosed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
btnClear.setText(C.I18N.getString("ui.button.clear")); // NOI18N
|
||||
btnClear.setText(C.i18n("ui.button.clear")); // NOI18N
|
||||
btnClear.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
btnClearActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
btnClose.setText(C.I18N.getString("ui.button.close")); // NOI18N
|
||||
btnClose.setText(C.i18n("ui.button.close")); // NOI18N
|
||||
btnClose.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
btnCloseActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
btnCopy.setText(C.I18N.getString("ui.button.copy")); // NOI18N
|
||||
btnCopy.setText(C.i18n("ui.button.copy")); // NOI18N
|
||||
btnCopy.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
btnCopyActionPerformed(evt);
|
||||
@@ -117,7 +116,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
}
|
||||
});
|
||||
|
||||
btnTieBa.setText(C.I18N.getString("logwindow.tieba")); // NOI18N
|
||||
btnTieBa.setText(C.i18n("logwindow.tieba")); // NOI18N
|
||||
btnTieBa.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
btnTieBaActionPerformed(evt);
|
||||
@@ -131,7 +130,7 @@ public class LogWindow extends javax.swing.JFrame {
|
||||
}
|
||||
});
|
||||
|
||||
btnTerminateGame.setText(C.I18N.getString("logwindow.terminate_game")); // NOI18N
|
||||
btnTerminateGame.setText(C.i18n("logwindow.terminate_game")); // NOI18N
|
||||
btnTerminateGame.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
btnTerminateGameActionPerformed(evt);
|
||||
|
||||
@@ -40,7 +40,7 @@
|
||||
<EmptySpace pref="212" max="32767" attributes="0"/>
|
||||
<Component id="btnCancel" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
<Component id="jButton1" min="-2" max="-2" attributes="0"/>
|
||||
<Component id="btnOK" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
</Group>
|
||||
</Group>
|
||||
@@ -54,7 +54,7 @@
|
||||
<Component id="jComboBox1" min="-2" max="-2" attributes="0"/>
|
||||
<EmptySpace max="32767" attributes="0"/>
|
||||
<Group type="103" groupAlignment="3" attributes="0">
|
||||
<Component id="jButton1" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
<Component id="btnOK" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
<Component id="btnCancel" alignment="3" min="-2" max="-2" attributes="0"/>
|
||||
</Group>
|
||||
<EmptySpace max="-2" attributes="0"/>
|
||||
@@ -77,14 +77,14 @@
|
||||
</Property>
|
||||
</Properties>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="jButton1">
|
||||
<Component class="javax.swing.JButton" name="btnOK">
|
||||
<Properties>
|
||||
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
|
||||
<ResourceString bundle="org/jackhuang/hellominecraft/launcher/I18N.properties" key="button.ok" replaceFormat="java.util.ResourceBundle.getBundle("{bundleNameSlashes}").getString("{key}")"/>
|
||||
</Property>
|
||||
</Properties>
|
||||
<Events>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
|
||||
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btnOKActionPerformed"/>
|
||||
</Events>
|
||||
</Component>
|
||||
<Component class="javax.swing.JButton" name="btnCancel">
|
||||
|
||||
@@ -49,6 +49,11 @@ public class Selector extends javax.swing.JDialog {
|
||||
for (String s : selList)
|
||||
jComboBox1.addItem(s);
|
||||
}
|
||||
|
||||
public int getChoice() {
|
||||
setVisible(true);
|
||||
return sel;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is called from within the constructor to initialize the form.
|
||||
@@ -61,7 +66,7 @@ public class Selector extends javax.swing.JDialog {
|
||||
|
||||
jLabel1 = new javax.swing.JLabel();
|
||||
jComboBox1 = new javax.swing.JComboBox();
|
||||
jButton1 = new javax.swing.JButton();
|
||||
btnOK = new javax.swing.JButton();
|
||||
btnCancel = new javax.swing.JButton();
|
||||
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
|
||||
@@ -69,10 +74,10 @@ public class Selector extends javax.swing.JDialog {
|
||||
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/jackhuang/hellominecraft/launcher/I18N"); // NOI18N
|
||||
jLabel1.setText(bundle.getString("selector.choose")); // NOI18N
|
||||
|
||||
jButton1.setText(bundle.getString("button.ok")); // NOI18N
|
||||
jButton1.addActionListener(new java.awt.event.ActionListener() {
|
||||
btnOK.setText(bundle.getString("button.ok")); // NOI18N
|
||||
btnOK.addActionListener(new java.awt.event.ActionListener() {
|
||||
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
||||
jButton1ActionPerformed(evt);
|
||||
btnOKActionPerformed(evt);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -100,7 +105,7 @@ public class Selector extends javax.swing.JDialog {
|
||||
.addContainerGap(212, Short.MAX_VALUE)
|
||||
.addComponent(btnCancel)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
|
||||
.addComponent(jButton1)
|
||||
.addComponent(btnOK)
|
||||
.addContainerGap())
|
||||
);
|
||||
layout.setVerticalGroup(
|
||||
@@ -112,7 +117,7 @@ public class Selector extends javax.swing.JDialog {
|
||||
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
|
||||
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
||||
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
||||
.addComponent(jButton1)
|
||||
.addComponent(btnOK)
|
||||
.addComponent(btnCancel))
|
||||
.addContainerGap())
|
||||
);
|
||||
@@ -125,14 +130,14 @@ public class Selector extends javax.swing.JDialog {
|
||||
this.dispose();
|
||||
}//GEN-LAST:event_btnCancelActionPerformed
|
||||
|
||||
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
|
||||
private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
|
||||
sel = jComboBox1.getSelectedIndex();
|
||||
this.dispose();
|
||||
}//GEN-LAST:event_jButton1ActionPerformed
|
||||
}//GEN-LAST:event_btnOKActionPerformed
|
||||
|
||||
// Variables declaration - do not modify//GEN-BEGIN:variables
|
||||
private javax.swing.JButton btnCancel;
|
||||
private javax.swing.JButton jButton1;
|
||||
private javax.swing.JButton btnOK;
|
||||
private javax.swing.JComboBox jComboBox1;
|
||||
private javax.swing.JLabel jLabel1;
|
||||
// End of variables declaration//GEN-END:variables
|
||||
|
||||
Reference in New Issue
Block a user