Clean redundancy codes.
This commit is contained in:
@@ -34,10 +34,18 @@ public class HMCLog {
|
||||
logger.warn(message);
|
||||
}
|
||||
|
||||
public static void debug(String message) {
|
||||
logger.debug(message);
|
||||
}
|
||||
|
||||
public static void warn(String msg, Throwable t) {
|
||||
logger.warn(msg, t);
|
||||
}
|
||||
|
||||
public static void debug(String msg, Throwable t) {
|
||||
logger.debug(msg, t);
|
||||
}
|
||||
|
||||
public static void err(String msg) {
|
||||
logger.error(msg);
|
||||
}
|
||||
|
||||
@@ -21,13 +21,15 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.OutputStream;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -69,6 +71,14 @@ public final class NetUtils {
|
||||
return doGet(url, DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
public static String doGet(URL url) throws IOException {
|
||||
return doGet(url, Proxy.NO_PROXY);
|
||||
}
|
||||
|
||||
public static String doGet(URL url, Proxy proxy) throws IOException {
|
||||
return getStreamContent(url.openConnection(proxy).getInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an HTTP GET request to a url
|
||||
*
|
||||
@@ -108,10 +118,8 @@ public final class NetUtils {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String post(String url, Map<String, String> params) {
|
||||
URL u;
|
||||
HttpURLConnection con;
|
||||
|
||||
public static String post(URL u, Map<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (params != null) {
|
||||
for (Map.Entry<String, String> e : params.entrySet()) {
|
||||
@@ -120,36 +128,51 @@ public final class NetUtils {
|
||||
sb.append(e.getValue());
|
||||
sb.append("&");
|
||||
}
|
||||
sb = new StringBuilder(sb.substring(0, sb.length() - 1));
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
return post(u, sb.toString());
|
||||
}
|
||||
|
||||
public static String post(URL u, String post) {
|
||||
return post(u, post, "application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
public static String post(URL u, String post, String contentType) {
|
||||
return post(u, post, contentType, Proxy.NO_PROXY);
|
||||
}
|
||||
|
||||
public static String post(URL u, String post, String contentType, Proxy proxy) {
|
||||
try {
|
||||
u = new URL(url);
|
||||
con = (HttpURLConnection) u.openConnection();
|
||||
HttpURLConnection con = (HttpURLConnection) u.openConnection(proxy);
|
||||
con.setRequestMethod(METHOD_POST);
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(false);
|
||||
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
try (OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream(), DEFAULT_CHARSET)) {
|
||||
osw.write(sb.toString());
|
||||
osw.flush();
|
||||
}
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
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 {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(con
|
||||
.getInputStream(), DEFAULT_CHARSET));
|
||||
String temp;
|
||||
while ((temp = br.readLine()) != null) {
|
||||
buffer.append(temp);
|
||||
buffer.append("\n");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
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 buffer.toString();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
HMCLog.warn("Failed to post.", e);
|
||||
return null;
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
package org.jackhuang.hellominecraft.utils;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.text.JTextComponent;
|
||||
|
||||
@@ -25,11 +27,38 @@ import javax.swing.text.JTextComponent;
|
||||
* @author huangyuhui
|
||||
*/
|
||||
public class TextComponentOutputStream extends OutputStream {
|
||||
|
||||
private static final Timer TIMER = new Timer();
|
||||
|
||||
private final JTextComponent txt;
|
||||
private final CacheTask t = new CacheTask();
|
||||
private class CacheTask extends TimerTask {
|
||||
private final Object lock = new Object();
|
||||
private StringBuilder cachedString = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized(lock) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
String t = txt.getText() + cachedString.toString().replace("\t", " ");
|
||||
txt.setText(t);
|
||||
txt.setCaretPosition(t.length());
|
||||
cachedString = new StringBuilder();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void cache(String s) {
|
||||
synchronized(lock) {
|
||||
cachedString.append(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public TextComponentOutputStream(JTextComponent paramJTextComponent) {
|
||||
txt = paramJTextComponent;
|
||||
|
||||
//TIMER.schedule(t, 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,6 +72,7 @@ public class TextComponentOutputStream extends OutputStream {
|
||||
}
|
||||
|
||||
private void append(final String newString) {
|
||||
//t.cache(newString);
|
||||
try {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
String t = txt.getText() + newString.replace("\t", " ");
|
||||
@@ -58,4 +88,8 @@ public class TextComponentOutputStream extends OutputStream {
|
||||
public final void write(int paramInt) {
|
||||
append(new String(new byte[]{(byte) paramInt}));
|
||||
}
|
||||
|
||||
public static void dispose() {
|
||||
TIMER.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
*/
|
||||
package org.jackhuang.hellominecraft.views;
|
||||
|
||||
import java.util.Timer;
|
||||
import org.jackhuang.hellominecraft.C;
|
||||
import org.jackhuang.hellominecraft.utils.functions.NonFunction;
|
||||
import org.jackhuang.hellominecraft.utils.DoubleOutputStream;
|
||||
|
||||
Reference in New Issue
Block a user