List of usage examples for java.io StringWriter write
public void write(String str, int off, int len)
From source file:marytts.util.io.FileUtils.java
public static String getReaderAsString(Reader reader) throws IOException { StringWriter sw = new StringWriter(); BufferedReader in = new BufferedReader(reader); char[] buf = new char[8192]; int n;/* ww w .j a v a 2 s . co m*/ while ((n = in.read(buf)) > 0) { sw.write(buf, 0, n); } return sw.toString(); }
From source file:com.example.android.networkconnect.MainActivity.java
public static String getStringFromInputStream(InputStream stream) throws IOException { int n = 0;/*from w w w. ja v a 2 s.co m*/ char[] buffer = new char[1024 * 4]; InputStreamReader reader = new InputStreamReader(stream, "UTF8"); StringWriter writer = new StringWriter(); while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n); return writer.toString(); }
From source file:com.gs.tools.doc.extractor.core.util.HttpUtility.java
/** * Get data from HTTP POST method.//from ww w . j a va 2s . c o m * * This method uses * <ul> * <li>Connection timeout = 300000 (5 minutes)</li> * <li>Socket/Read timeout = 300000 (5 minutes)</li> * <li>Socket Read Buffer = 10485760 (10MB) to provide more space to read</li> * </ul> * -- in case the site is slow * * @return * @throws MalformedURLException * @throws URISyntaxException * @throws UnsupportedEncodingException * @throws IOException * @throws ClientProtocolException * @throws Exception */ public static String getPostData(String sourceUrl, String postString) throws MalformedURLException, URISyntaxException, UnsupportedEncodingException, IOException, ClientProtocolException, Exception { String result = ""; URL targetUrl = new URL(sourceUrl); /* * Create http parameter to set Connection timeout = 300000 Socket/Read * timeout = 300000 Socket Read Buffer = 10485760 */ HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 300000); HttpConnectionParams.setSocketBufferSize(httpParams, 10485760); HttpConnectionParams.setSoTimeout(httpParams, 300000); // set the http param to the DefaultHttpClient HttpClient httpClient = new DefaultHttpClient(httpParams); // create POST method and set the URL and POST data HttpPost post = new HttpPost(targetUrl.toURI()); StringEntity entity = new StringEntity(postString, "UTF-8"); post.setEntity(entity); logger.info("Execute the POST request with all input data"); // Execute the POST request on the http client to get the response HttpResponse response = httpClient.execute(post, new BasicHttpContext()); if (null != response && response.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = response.getEntity(); if (null != httpEntity) { long contentLength = httpEntity.getContentLength(); logger.info("Content length: " + contentLength); // no data, if the content length is insufficient if (contentLength <= 0) { return ""; } // read the response to String InputStream responseStream = httpEntity.getContent(); if (null != responseStream) { BufferedReader reader = null; StringWriter writer = null; try { reader = new BufferedReader(new InputStreamReader(responseStream)); writer = new StringWriter(); int count = 0; int size = 1024 * 1024; char[] chBuff = new char[size]; while ((count = reader.read(chBuff, 0, size)) >= 0) { writer.write(chBuff, 0, count); } result = writer.getBuffer().toString(); } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally { IOUtils.closeQuietly(responseStream); IOUtils.closeQuietly(reader); IOUtils.closeQuietly(writer); } } } } logger.info("data read complete"); return result; }
From source file:com.bumptech.glide.disklrucache.DiskLruCacheTest.java
private static String readFile(File file) throws Exception { Reader reader = new FileReader(file); StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; int count;/*from w ww . j av a 2s. c om*/ while ((count = reader.read(buffer)) != -1) { writer.write(buffer, 0, count); } reader.close(); return writer.toString(); }
From source file:org.pentaho.di.cluster.HttpUtil.java
/** * Base 64 decode, unzip and extract text using {@link Const#XML_ENCODING} predefined charset value for byte-wise * multi-byte character handling./*w ww. j av a2s . com*/ * * @param loggingString64 * base64 zip archive string representation * @return text from zip archive * @throws IOException */ public static String decodeBase64ZippedString(String loggingString64) throws IOException { if (loggingString64 == null || loggingString64.isEmpty()) { return ""; } StringWriter writer = new StringWriter(); // base 64 decode byte[] bytes64 = Base64.decodeBase64(loggingString64.getBytes()); // unzip to string encoding-wise ByteArrayInputStream zip = new ByteArrayInputStream(bytes64); GZIPInputStream unzip = null; InputStreamReader reader = null; BufferedInputStream in = null; try { unzip = new GZIPInputStream(zip, HttpUtil.ZIP_BUFFER_SIZE); in = new BufferedInputStream(unzip, HttpUtil.ZIP_BUFFER_SIZE); // PDI-4325 originally used xml encoding in servlet reader = new InputStreamReader(in, Const.XML_ENCODING); writer = new StringWriter(); // use same buffer size char[] buff = new char[HttpUtil.ZIP_BUFFER_SIZE]; for (int length = 0; (length = reader.read(buff)) > 0;) { writer.write(buff, 0, length); } } finally { // close resources if (reader != null) { try { reader.close(); } catch (IOException e) { // Suppress } } if (in != null) { try { in.close(); } catch (IOException e) { // Suppress } } if (unzip != null) { try { unzip.close(); } catch (IOException e) { // Suppress } } } return writer.toString(); }
From source file:io.openshift.launchpad.ReadmeProcessor.java
private String loadContents(URL url) throws IOException { try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()))) { StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; int c;//w w w . j a va 2s . c o m while ((c = reader.read(buffer)) != -1) { writer.write(buffer, 0, c); } return writer.toString(); } }
From source file:com.zaa.WeatherLoad.java
public String convertStreamToString(InputStream is) throws IOException { InputStreamReader r = new InputStreamReader(is); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; try {// w w w . java 2s . c o m for (int n; (n = r.read(buffer)) != -1;) sw.write(buffer, 0, n); } finally { try { is.close(); } catch (IOException e1) { e1.printStackTrace(); } } return sw.toString(); }
From source file:com.subgraph.vega.internal.analysis.urls.HtmlUrlExtractor.java
private String inputStreamToString(InputStream in) throws IOException { final Reader r = new InputStreamReader(in, "UTF-8"); final StringWriter w = new StringWriter(); final char[] buffer = new char[8192]; while (true) { int n = r.read(buffer, 0, buffer.length); if (n <= 0) return w.toString(); w.write(buffer, 0, n); }//from www . ja va 2 s. com }
From source file:com.easarrive.aws.client.cloudsearch.AmazonCloudSearchClient.java
private String inputStreamToString(InputStream in) throws IOException { StringWriter output = new StringWriter(); InputStreamReader input = new InputStreamReader(in); char[] buffer = new char[1024 * 4]; int n = 0;/*from ww w . j av a2 s . c o m*/ while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } return output.toString(); }
From source file:com.icesoft.faces.renderkit.IncludeRenderer.java
public void encodeBegin(FacesContext context, UIComponent component) throws IOException { if (context == null || component == null) { throw new NullPointerException("Null Faces context or component parameter"); }//from w ww . j a v a 2s. c o m // suppress rendering if "rendered" property on the component is // false. if (!component.isRendered()) { return; } String page = (String) component.getAttributes().get("page"); HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest(); URI absoluteURI = null; try { absoluteURI = new URI(request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI()); URL includedURL = absoluteURI.resolve(page).toURL(); URLConnection includedConnection = includedURL.openConnection(); includedConnection.setRequestProperty("Cookie", "JSESSIONID=" + ((HttpSession) context.getExternalContext().getSession(false)).getId()); Reader contentsReader = new InputStreamReader(includedConnection.getInputStream()); try { StringWriter includedContents = new StringWriter(); char[] buf = new char[2000]; int len = 0; while ((len = contentsReader.read(buf)) > -1) { includedContents.write(buf, 0, len); } ((UIOutput) component).setValue(includedContents.toString()); } finally { contentsReader.close(); } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e.getMessage()); } } super.encodeBegin(context, component); }