ソースを参照

优化 readResponseString

James 4 年 前
コミット
cb329bcf13
1 ファイル変更5 行追加23 行削除
  1. 5 23
      src/main/java/com/jfinal/kit/HttpKit.java

+ 5 - 23
src/main/java/com/jfinal/kit/HttpKit.java

@@ -202,35 +202,17 @@ public class HttpKit {
 	}
 	}
 	
 	
 	private static String readResponseString(HttpURLConnection conn) {
 	private static String readResponseString(HttpURLConnection conn) {
-		BufferedReader reader = null;
-		try {
-			StringBuilder ret;
-			reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), CHARSET));
-			String line = reader.readLine();
-			if (line != null) {
-				ret = new StringBuilder();
-				ret.append(line);
-			} else {
-				return "";
-			}
-			
-			while ((line = reader.readLine()) != null) {
-				ret.append('\n').append(line);
+		StringBuilder ret = new StringBuilder();
+		char[] buf = new char[1024];
+		try (InputStreamReader isr = new InputStreamReader(conn.getInputStream(), CHARSET)) {
+			for (int num; (num = isr.read(buf, 0, buf.length)) != -1;) {
+				ret.append(buf, 0, num);
 			}
 			}
 			return ret.toString();
 			return ret.toString();
 		}
 		}
 		catch (Exception e) {
 		catch (Exception e) {
 			throw new RuntimeException(e);
 			throw new RuntimeException(e);
 		}
 		}
-		finally {
-			if (reader != null) {
-				try {
-					reader.close();
-				} catch (IOException e) {
-					LogKit.error(e.getMessage(), e);
-				}
-			}
-		}
 	}
 	}
 	
 	
 	/**
 	/**