List of usage examples for java.io InputStreamReader close
public void close() throws IOException
From source file:com.iloomo.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {/*from w ww. j a va2 s . c om*/ if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); Set<String> keys = parameter.keySet(); for (String key : keys) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(EncodeUtils.encode(key)); bulider.append("="); bulider.append(EncodeUtils.encode(String.valueOf(parameter.get(key)))); } url += "?" + bulider.toString(); Log.i("request", url); } for (int i = 0; i < HttpConstant.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (HttpConstant.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); // ByteArrayOutputStream content = new // ByteArrayOutputStream(); // response.getEntity().writeTo(content); // ret = new String(content.toByteArray()).trim(); // content.close(); } else { ret = ErrorUtil.errorJson("" + statusCode, "??,??" + statusCode); continue; } break; } catch (Exception e) { if (i == HttpConstant.CONNECTION_COUNT - 1) { ret = ErrorUtil.errorJson("999", ""); } else { Log.d("connection url", "" + i); continue; } } } } catch (IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, HttpConstant.ERROR_MESSAGE); ret = ErrorUtil.errorJson("999", exception.getMessage()); } finally { if (!HttpConstant.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } } super.run(); }
From source file:com.changxiang.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {//from w w w . ja v a 2s.c o m if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Util.encode(p.getName())); bulider.append("="); bulider.append(Util.encode(p.getValue())); } // url += "?" + bulider.toString(); url += "?" + bulider.toString() + "&sign=" + MD5Util.MD5(bulider.toString() + "1234" + CXGameConfig.SERVERKEY); System.out.println("===" + url); } LogUtil.d("AsyncHttpGet : ", url); for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (CXGameConfig.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); LogUtil.d("connection url", "" + i); continue; } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:ca.ualberta.cmput301f13t13.storyhoard.serverClasses.ESUpdates.java
/** * Inserts a story object into the server. The story must be a complete * story, in other words, have all its chapters, choices, and media. It is * all stored as one story, not individual components like in the database. * </br> The story is inserted using HttpPost, not HttpGet. * /*from ww w .j av a2s . c o m*/ * @param story * The complete story to post to server. It is first converted to * a Json string, and then is posted onto the server. */ protected void insertStory(Story story, String server) { HttpPost httpPost = new HttpPost(server + story.getId().toString()); StringEntity stringentity = null; try { stringentity = new StringEntity(gson.toJson(story)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } httpPost.setHeader("Accept", "application/json"); httpPost.setEntity(stringentity); HttpResponse response = null; try { response = httpclient.execute(httpPost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String status = response.getStatusLine().toString(); System.out.println(status); HttpEntity entity = response.getEntity(); InputStreamReader is = null; try { is = new InputStreamReader(entity.getContent()); } catch (IllegalStateException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } BufferedReader br = new BufferedReader(is); String output; System.err.println("Output from ESUpdates -> "); try { while ((output = br.readLine()) != null) { System.err.println(output); } entity.consumeContent(); is.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.baofeng.game.sdk.net.AsyncHttpGet.java
@Override public void run() { String ret = ""; try {//w ww . ja v a2 s. c o m if (parameter != null && parameter.size() > 0) { StringBuilder bulider = new StringBuilder(); for (RequestParameter p : parameter) { if (bulider.length() != 0) { bulider.append("&"); } bulider.append(Util.encode(p.getName())); bulider.append("="); bulider.append(Util.encode(p.getValue())); } // url += "?" + bulider.toString(); url += "?" + bulider.toString() + "&sign=" + MD5Util.MD5(bulider.toString() + "1234" + BFGameConfig.SERVERKEY); // System.out.println("@@@"+bulider.toString()); } LogUtil.d("AsyncHttpGet : ", url); for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpGet(url); if (BFGameConfig.isGzip) { request.addHeader("Accept-Encoding", "gzip"); } else { request.addHeader("Accept-Encoding", "default"); } // httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); // ? httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); LogUtil.d("connection url", "" + i); continue; } break; } catch (Exception e) { if (i == BFGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, BFGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!BFGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:com.changxiang.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {/* ww w . java2s. c o m*/ for (int i = 0; i < CXGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); System.out.println( "POST_SIGN:" + sb + "&sign=" + MD5Util.MD5(sb + "1234" + CXGameConfig.SERVERKEY)); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } System.out.println("====" + url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == CXGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, CXGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!CXGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:com.baofeng.game.sdk.net.AsyncHttpPost.java
@Override public void run() { String ret = ""; try {//w w w . j a va 2s .co m for (int i = 0; i < BFGameConfig.CONNECTION_COUNT; i++) { try { request = new HttpPost(url); request.addHeader("Accept-Encoding", "default"); if (parameter != null && parameter.size() > 0) { List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); for (RequestParameter p : parameter) { list.add(new BasicNameValuePair(Util.encode(p.getName()), Util.encode(p.getValue()))); LogUtil.d("AsyncHttpPost Param ", p.getName() + " , " + p.getValue()); } StringBuffer sb = new StringBuffer(); for (int j = 0; j < list.size(); j++) { sb.append(list.get(j)); if (!(j == list.size() - 1)) { sb.append("&"); } } BasicNameValuePair bn = new BasicNameValuePair("sign", MD5Util.MD5(sb + "1234" + BFGameConfig.SERVERKEY)); // System.out.println("@@@" + sb + "1234" // + BFGameConfig.SERVERKEY); list.add(bn); ((HttpPost) request).setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); } LogUtil.d("AsyncHttpPost : ", url); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeout); httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout); HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { // HttpManager.saveCookies(response); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); bis.mark(2); // ?? byte[] header = new byte[2]; int result = bis.read(header); // reset?? bis.reset(); // ?GZIP? int headerData = getShort(header); // Gzip ? ? 0x1f8b if (result != -1 && headerData == 0x1f8b) { is = new GZIPInputStream(bis); } else { is = bis; } InputStreamReader reader = new InputStreamReader(is, "utf-8"); char[] data = new char[100]; int readSize; StringBuffer sb = new StringBuffer(); while ((readSize = reader.read(data)) > 0) { sb.append(data, 0, readSize); } ret = sb.toString(); bis.close(); reader.close(); } else { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, "??,??" + statusCode); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } break; } catch (Exception e) { if (i == BFGameConfig.CONNECTION_COUNT - 1) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, ""); ret = ErrorUtil.errorJson("-1", exception.getMessage()); } else { LogUtil.d("connection url", "" + i); continue; } } } } catch (java.lang.IllegalArgumentException e) { RequestException exception = new RequestException(RequestException.IO_EXCEPTION, BFGameConfig.ERROR_MESSAGE); ret = ErrorUtil.errorJson("-2", exception.getMessage()); } finally { if (!BFGameConfig.IS_STOP_REQUEST) { Message msg = new Message(); msg.obj = ret; msg.getData().putSerializable("callback", callBack); resultHandler.sendMessage(msg); } if (customLoadingDialog != null && customLoadingDialog.isShowing()) { customLoadingDialog.dismiss(); customLoadingDialog = null; } } super.run(); }
From source file:colectordedatos.Generador.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed try {/* www. j ava 2s . c om*/ // TODO add your handling code here: File snapshotsFile = new File(archivo.getText()); FileInputStream fis = new FileInputStream(snapshotsFile); InputStreamReader isr = new InputStreamReader(fis); BufferedReader bf = new BufferedReader(isr); String linea = bf.readLine(); StringBuffer sb = new StringBuffer(); while (linea != null) { sb.append(linea.trim()); linea = bf.readLine(); } bf.close(); isr.close(); fis.close(); dashboard = new JSONObject(sb.toString()); arrJSON = dashboard.getJSONArray("widgetTemplates"); paneles.removeAllItems(); for (int i = 0; i < arrJSON.length(); i++) { JSONObject jsonObject = arrJSON.getJSONObject(i); paneles.addItem(jsonObject.getString("title")); } } catch (Exception ex) { Logger.getLogger(Generador.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:at.tugraz.sss.serv.util.SSFileU.java
public static List<String[]> readAllFromCSV(final String filePath) throws SSErr { FileInputStream in = null;//from w w w . j a v a2 s.c o m InputStreamReader reader = null; CSVReader csvReader = null; try { try { in = SSFileU.openFileForRead(filePath); } catch (Exception error) { SSServErrReg.regErrThrow(error); return null; } reader = new InputStreamReader(in, Charset.forName(SSEncodingU.utf8.toString())); csvReader = new CSVReader(reader, SSStrU.semiColon.charAt(0)); return csvReader.readAll(); } catch (Exception error) { SSServErrReg.regErrThrow(error); return null; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { SSLogU.err(ex); } } if (reader != null) { try { reader.close(); } catch (IOException ex) { SSLogU.err(ex); } } if (csvReader != null) { try { csvReader.close(); } catch (IOException ex) { SSLogU.err(ex); } } } }
From source file:com.michelin.cio.hudson.plugins.qc.QualityCenter.java
/** * Runs the given TestSet ({@code testSetName}) through VBScript. *//* ww w . j a v a 2 s . c o m*/ private String runVBScript(String testSetName, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, FilePath file, boolean isRunOnce) throws IOException, InterruptedException { ArgumentListBuilder args = new ArgumentListBuilder(); EnvVars env = build.getEnvironment(listener); VariableResolver<String> varResolver = build.getBuildVariableResolver(); PrintStream out = listener.getLogger(); // Add the qc specific env vars pushEnvVars(env); // Parse the report file name using env vars this.parsedQcTSLogFile = Util.replaceMacro(env.expand(this.qcTSLogFile), varResolver); if (!parsedQcTSLogFile.endsWith(".xml")) { parsedQcTSLogFile = parsedQcTSLogFile + ".xml"; } if (!isRunOnce && !parsedQcTSLogFile.contains(testSetName)) { // JENKINS-12384: One file must be generated per test set. As such we must // ensure that the name of each file is unigue. We consider it is the case // if the name of the file contains the name of the test set. Otherwise, // we add it. parsedQcTSLogFile = parsedQcTSLogFile.substring(0, parsedQcTSLogFile.length() - 4) + '_' + testSetName + ".xml"; } testSetLogFiles.add(parsedQcTSLogFile); // Use cscript to run the vbscript and get the console output args.add("cscript"); args.add("/nologo"); args.add(file); args.add(Util.replaceMacro(env.expand(this.qcServerURL), varResolver)); args.add(Util.replaceMacro(env.expand(this.qcLogin), varResolver)); // If no password, then replace by "" if (StringUtils.isNotBlank(this.qcPass)) { args.addMasked(Util.replaceMacro(env.expand(this.qcPass), varResolver)); } else { args.addMasked("\"\""); } args.add(Util.replaceMacro(env.expand(this.qcDomain), varResolver)); args.add(Util.replaceMacro(env.expand(this.qcProject), varResolver)); args.add(Util.replaceMacro(env.expand(this.qcTSFolder), varResolver)); args.add(Util.replaceMacro(env.expand(testSetName), varResolver)); args.add(this.parsedQcTSLogFile); args.add(this.qcTimeOut); args.add(runMode); if (runMode.equals(RUN_MODE_REMOTE)) { args.add(runHost); } // Remove qc specific environment variables removeEnvVars(env); // Run the script on node // Execution result should be 0 if (launcher.launch().cmds(args).stdout(out).pwd(file.getParent()).join() != 0) { listener.fatalError(Messages.QualityCenter_TSSchedulerFailed()); // log file is in UTF-16 InputStream is = new FileInputStream(this.parsedQcTSLogFile); InputStreamReader in = new InputStreamReader(is, "UTF-16"); try { // Copy the console output to our logger IOUtils.copy(in, new OutputStreamWriter(out)); } finally { in.close(); is.close(); } throw new AbortException(); } return this.parsedQcTSLogFile; }
From source file:com.instantme.api.APIResponse.java
public boolean Response20X(HttpConnection connection, Hashtable cookies) { boolean result = false; StringBuffer strf = null;//from www .j a v a2s.c om response = null; try { updateProgress(Locale.getInst().getStr(Locale.LOADING)); InputStream is = connection.openInputStream(); InputStreamReader isr = new InputStreamReader(is, "UTF-8"); int ch; strf = new StringBuffer(); while ((ch = isr.read()) != -1) { strf.append((char) ch); } response = strf.toString(); strf = null; updateProgress(Locale.getInst().getStr(Locale.ANALYZING)); // TODO do not decode before checking reponse code try { JSONObject jobj = new JSONObject(response); result = data.fromJSONObject(jobj, animation); } catch (JSONException ex) { ex.printStackTrace(); result = false; } response = null; isr.close(); is.close(); } catch (IOException ex) { ex.printStackTrace(); } return result; }