List of usage examples for java.io BufferedReader close
public void close() throws IOException
From source file:Main.java
public static String getInputStream(InputStream is) throws IOException { if (is == null) throw new IllegalArgumentException("stream mustn't be null!"); BufferedReader bufReader = createBuffReader(is); StringBuilder sb = new StringBuilder(); String line;/*from w ww. j av a2s . c om*/ while ((line = bufReader.readLine()) != null) { sb.append(line); sb.append('\n'); } bufReader.close(); return sb.toString(); }
From source file:cloudworker.RemoteWorker.java
private static void cleanUpInstance() { /*try {// w w w. j ava 2s . c om CancelSpotInstanceRequestsRequest cancelRequest = new CancelSpotInstanceRequestsRequest(spotInstanceRequestIds); ec2.cancelSpotInstanceRequests(cancelRequest); } catch (AmazonServiceException e) { // Write out any exceptions that may have occurred. System.out.println("Error cancelling instances"); System.out.println("Caught Exception: " + e.getMessage()); System.out.println("Reponse Status Code: " + e.getStatusCode()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Request ID: " + e.getRequestId()); }*/ List<String> instanceId = new ArrayList<String>(); String inputLine; URL EC2MetaData; try { EC2MetaData = new URL("http://169.254.169.254/latest/meta-data/instance-id"); URLConnection EC2MD = EC2MetaData.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(EC2MD.getInputStream())); while ((inputLine = in.readLine()) != null) { instanceId.add(inputLine); } in.close(); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { // Terminate instances. TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceId); ec2.terminateInstances(terminateRequest); } catch (AmazonServiceException e) { // Write out any exceptions that may have occurred. System.out.println("Error terminating instances"); System.out.println("Caught Exception: " + e.getMessage()); System.out.println("Reponse Status Code: " + e.getStatusCode()); System.out.println("Error Code: " + e.getErrorCode()); System.out.println("Request ID: " + e.getRequestId()); } }
From source file:javaphpmysql.JavaPHPMySQL.java
public static void sendGet() { //Creamos un objeto JSON JSONObject jsonObj = new JSONObject(); //Aadimos el nombre, apellidos y email del usuario jsonObj.put("nombre", nombre); jsonObj.put("apellidos", apellidos); jsonObj.put("email", email); //Creamos una lista para almacenar el JSON List l = new LinkedList(); l.addAll(Arrays.asList(jsonObj)); //Generamos el String JSON String jsonString = JSONValue.toJSONString(l); System.out.println("JSON GENERADO:"); System.out.println(jsonString); System.out.println(""); try {/* w ww. j a v a 2 s . co m*/ //Codificar el json a URL jsonString = URLEncoder.encode(jsonString, "UTF-8"); //Generar la URL String url = SERVER_PATH + "listenGet.php?json=" + jsonString; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //print result System.out.println(response.toString()); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String readFile(File file) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); try {/*from w w w . ja v a 2 s. co m*/ StringBuilder ret = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { ret.append(line); ret.append("\n"); } return ret.toString(); } finally { reader.close(); } }
From source file:com.wikonos.network.HttpLogSender.java
public static String readStream(InputStream is) { StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try {//from ww w . j av a 2s . c o m String line; while ((line = reader.readLine()) != null) { builder.append(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); ErrorLog.e(e); } finally { } return builder.toString(); }
From source file:Main.java
private static String getSystemProperty() { String line = ""; BufferedReader input = null; try {/*w ww . j a v a 2 s . c o m*/ Process p = Runtime.getRuntime().exec("getprop"); input = new BufferedReader(new InputStreamReader(p.getInputStream()), 2048); String ret = input.readLine(); while (ret != null) { line += ret + "\n"; ret = input.readLine(); } input.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read sysprop", ex); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { Log.e(TAG, "Exception while closing InputStream", e); } } } return line; }
From source file:Main.java
/** * Issue a POST request to the server./*from www . j a v a 2s .c o m*/ * * @param endpoint * POST address. * @param params * request parameters. * * @throws java.io.IOException * propagated from POST. */ public static String post(String endpoint, Map<String, String> params) throws IOException { URL url; try { url = new URL(endpoint); } catch (MalformedURLException e) { throw new IllegalArgumentException("invalid url: " + endpoint); } StringBuilder bodyBuilder = new StringBuilder(); Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); // constructs the POST body using the parameters while (iterator.hasNext()) { Entry<String, String> param = iterator.next(); bodyBuilder.append(param.getKey()).append('=').append(param.getValue()); if (iterator.hasNext()) { bodyBuilder.append('&'); } } String body = bodyBuilder.toString(); byte[] bytes = body.getBytes(); HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(bytes.length); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); // post the request OutputStream out = conn.getOutputStream(); out.write(bytes); out.close(); // handle the response int status = conn.getResponseCode(); if (status != 200) { throw new IOException("Post failed with error code " + status); } // Get Response InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\n'); } rd.close(); return response.toString(); } finally { if (conn != null) { conn.disconnect(); } } }
From source file:Main.java
public static long totalMemorySize() { String memInfoPath = "/proc/meminfo"; String str2;/*ww w . j a v a 2 s .c o m*/ String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(memInfoPath); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (String num : arrayOfString) { Log.i(str2, num + "\t"); } //total Memory initial_memory = Integer.valueOf(arrayOfString[1]) * 1024; localBufferedReader.close(); return initial_memory; } catch (IOException e) { return -1; } }
From source file:com.isol.app.tracker.Utilita.java
public static String getHttpData(String url) throws IOException, MalformedURLException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); InputStream in = conn.getInputStream(); try {//from ww w .ja v a 2 s .c o m StringBuilder sb = new StringBuilder(); BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in))); for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line); } r.close(); return sb.toString(); } finally { in.close(); } }