List of usage examples for java.io InputStreamReader read
public int read() throws IOException
From source file:com.instantme.api.SimpleAPIResponse.java
public boolean Response20X(HttpConnection connection, Hashtable cookies) { boolean result = false; StringBuffer strf = null;/*from ww w . j a v a 2 s .com*/ response = null; try { 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; int code = new JSONObject(response).getJSONObject("meta").getInt("code"); result = code == 200; response = null; isr.close(); is.close(); } catch (IOException ex) { ex.printStackTrace(); } catch (JSONException ex) { ex.printStackTrace(); } return result; }
From source file:com.pactodigital.apiconnect.api.APIResponse.java
public boolean Response20X(HttpConnection connection) { boolean result = false; StringBuffer strf = null;//www . j a v a2s. c o m response = null; try { 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; try { JSONArray objArray = new JSONArray(response); for (int i = 0; i < objArray.length(); i++) { JSONObject jobj = objArray.getJSONObject(i); data.fromJSONObject(jobj); } result = true; } catch (JSONException ex) { ex.printStackTrace(); result = false; } response = null; isr.close(); is.close(); } catch (IOException ex) { ex.printStackTrace(); } return result; }
From source file:com.instantme.api.APIResponse.java
public boolean Response20X(HttpConnection connection, Hashtable cookies) { boolean result = false; StringBuffer strf = null;//from ww w . j a v a 2s .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; }
From source file:com.googlecode.xmlzen.XmlBuilderStreamOutputTest.java
@Test public void testBuild() throws Exception { File myXml = File.createTempFile("xmlzentest", ".xml"); OutputStream myXmlStream = new FileOutputStream(myXml); String xml = XmlBuilder.newXml(new XmlBuilderStreamOutput(myXmlStream, "UTF-8"), "UTF-8", false) .openTag("xml").withAttribute("id", 1).openTag("thisishow").withValue("you can build").closeTag() .openTag("your").withAttribute("xml", "nicely").withAttributeIf(1 < 0, "shouldnot", "happen") .toString(true);/* ww w . j a va2 s . c om*/ log.debug(xml); myXmlStream.close(); InputStreamReader reader = new InputStreamReader(new FileInputStream(myXml), "UTF-8"); ByteArrayOutputStream bout = new ByteArrayOutputStream(); int c = 0; while ((c = reader.read()) != -1) { bout.write(c); } reader.close(); assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<xml id=\"1\"><thisishow>you can build</thisishow>" + "<your xml=\"nicely\"/></xml>", bout.toString("UTF-8")); }
From source file:net.sf.ginp.commands.UploadConfig.java
/** * Handle admin request to upload an new configuration file. * * @param model Description of the Parameter * @param params Description of the Parameter *//*w ww . j a va 2 s.co m*/ public final void action(final GinpModel model, final Vector params) { // Check we do not have a state of bad config. if (Configuration.configOK()) { return; } File confFile = new File(Configuration.getConfigfilelocation()); // check that file is not there. if (confFile.exists()) { log.error("Config File there but NOT OK. Fix it or remove it."); return; } for (int i = 0; i < params.size(); i++) { CommandParameter param = (CommandParameter) params.get(i); log.debug(param.getName() + " = " + param.getValue()); if (param.getName().equals("configfile")) { try { InputStream is = (InputStream) param.getObject(); InputStreamReader isr = new InputStreamReader(is); FileWriter fw = new FileWriter(confFile); int b = isr.read(); while (b != -1) { fw.write(b); b = isr.read(); } fw.flush(); } catch (Exception ex) { log.error("Error writing Config File from upload stream.", ex); } } } if (Configuration.configOK()) { log.info("Uploaded valid Config"); } model.setUserName("admin"); model.setCurrentPage("setup2.jsp"); }
From source file:com.uisteps.utils.api.rest.RestApiRequest.java
private String readStreamToString(InputStream in, String encoding) throws IOException { StringBuilder builder = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, encoding); int c;//from w w w .j a va2s . c o m while ((c = reader.read()) != -1) { builder.append((char) c); } return builder.toString(); }
From source file:org.pentaho.di.core.HTTPProtocol.java
/** * Performs a get on urlAsString using username and password as credentials. * * If the status code returned not -1 and 401 then the contents are returned. If the status code is 401 an * AuthenticationException is thrown./*from w ww .j ava2 s. co m*/ * * All other values of status code are not dealt with but logic can be added as needed. * * @param urlAsString * @param username * @param password * @param encoding * @return * @throws MalformedURLException * @throws IOException */ public String get(String urlAsString, String username, String password) throws MalformedURLException, IOException, AuthenticationException { HttpClient httpClient = SlaveConnectionManager.getInstance().createHttpClient(); GetMethod getMethod = new GetMethod(urlAsString); if (!Const.isEmpty(username)) { httpClient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(username, password); httpClient.getState().setCredentials(AuthScope.ANY, defaultcreds); } int statusCode = httpClient.executeMethod(getMethod); StringBuffer bodyBuffer = new StringBuffer(); if (statusCode != -1) { if (statusCode != 401) { // the response InputStreamReader inputStreamReader = new InputStreamReader(getMethod.getResponseBodyAsStream()); int c; while ((c = inputStreamReader.read()) != -1) { bodyBuffer.append((char) c); } inputStreamReader.close(); } else { throw new AuthenticationException(); } } // Display response return bodyBuffer.toString(); }
From source file:HttpPOSTMIDlet.java
private String requestUsingGET(String URLString) throws IOException { HttpConnection hpc = null;/*from w w w.ja va 2 s . c o m*/ DataInputStream dis = null; DataOutputStream dos = null; boolean newline = false; String content = ""; try { hpc = (HttpConnection) Connector.open(defaultURL); hpc.setRequestMethod(HttpConnection.POST); hpc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); hpc.setRequestProperty("Content-Language", "zh-tw"); hpc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); hpc.setRequestProperty("Content-Length", String.valueOf(URLString.length())); dos = new DataOutputStream(hpc.openOutputStream()); dos.write(URLString.getBytes()); dos.flush(); InputStreamReader xdis = new InputStreamReader(hpc.openInputStream()); int character; while ((character = xdis.read()) != -1) { if ((char) character == '\\') { newline = true; continue; } else { if ((char) character == 'n' && newline) { content += "\n"; newline = false; } else if (newline) { content += "\\" + (char) character; newline = false; } else { content += (char) character; newline = false; } } } if (hpc != null) hpc.close(); if (dis != null) dis.close(); } catch (IOException e2) { } return content; }
From source file:org.pentaho.di.trans.steps.fileinput.text.TextFileInputUtils.java
public static final String getLine(LogChannelInterface log, InputStreamReader reader, EncodingType encodingType, int formatNr, StringBuilder line) throws KettleFileException { int c = 0;//from w w w. ja va 2 s .co m line.setLength(0); try { switch (formatNr) { case TextFileInputMeta.FILE_FORMAT_DOS: while (c >= 0) { c = reader.read(); if (encodingType.isReturn(c) || encodingType.isLinefeed(c)) { c = reader.read(); // skip \n and \r if (!encodingType.isReturn(c) && !encodingType.isLinefeed(c)) { // make sure its really a linefeed or cariage return // raise an error this is not a DOS file // so we have pulled a character from the next line throw new KettleFileException( BaseMessages.getString(PKG, "TextFileInput.Log.SingleLineFound")); } return line.toString(); } if (c >= 0) { line.append((char) c); } } break; case TextFileInputMeta.FILE_FORMAT_UNIX: while (c >= 0) { c = reader.read(); if (encodingType.isLinefeed(c) || encodingType.isReturn(c)) { return line.toString(); } if (c >= 0) { line.append((char) c); } } break; case TextFileInputMeta.FILE_FORMAT_MIXED: // in mixed mode we suppose the LF is the last char and CR is ignored // not for MAC OS 9 but works for Mac OS X. Mac OS 9 can use UNIX-Format while (c >= 0) { c = reader.read(); if (encodingType.isLinefeed(c)) { return line.toString(); } else if (!encodingType.isReturn(c)) { if (c >= 0) { line.append((char) c); } } } break; default: break; } } catch (KettleFileException e) { throw e; } catch (Exception e) { if (line.length() == 0) { throw new KettleFileException( BaseMessages.getString(PKG, "TextFileInput.Log.Error.ExceptionReadingLine", e.toString()), e); } return line.toString(); } if (line.length() > 0) { return line.toString(); } return null; }
From source file:com.ikon.util.MailUtils.java
/** * User tinyurl service as url shorter Depends on commons-httpclient:commons-httpclient:jar:3.0 because of * org.apache.jackrabbit:jackrabbit-webdav:jar:1.6.4 *///from ww w . j a v a 2s .co m public static String getTinyUrl(String fullUrl) throws HttpException, IOException { HttpClient httpclient = new HttpClient(); // Prepare a request object HttpMethod method = new GetMethod("http://tinyurl.com/api-create.php"); method.setQueryString(new NameValuePair[] { new NameValuePair("url", fullUrl) }); httpclient.executeMethod(method); InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"); StringWriter sw = new StringWriter(); int c; while ((c = isr.read()) != -1) sw.write(c); isr.close(); method.releaseConnection(); return sw.toString(); }