List of usage examples for java.io StringWriter write
public void write(String str, int off, int len)
From source file:com.aperigeek.dropvault.dav.DropDAVClient.java
private void login() throws InvalidPasswordException, DAVException { String url = MessageFormat.format(LOGIN_URL, URLEncoder.encode(this.username), hash.hash(this.password)); HttpGet get = new HttpGet(url); try {// w w w . j av a2 s . co m HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 401) { throw new InvalidPasswordException(); } if (response.getStatusLine().getStatusCode() != 200) { throw new DAVException("Server error"); } InputStream in = response.getEntity().getContent(); StringWriter writer = new StringWriter(); Reader reader = new InputStreamReader(in); char[] buffer = new char[128]; int readed; while ((readed = reader.read(buffer)) != -1) { writer.write(buffer, 0, readed); } in.close(); baseUri = writer.toString().trim(); } catch (IOException ex) { throw new DAVException("Login URL unavailable", ex); } }
From source file:com.icesoft.applications.faces.auctionMonitor.beans.ReadmeBean.java
/** * Method to load a default readme file (at readme.html) The file will be * read in and converted to a displayable string * * @return boolean true on successful read *///from ww w .ja va2 s . c o m private boolean loadDefaultReadmeFile() { try { Reader readmeReader = new InputStreamReader(this.getClass().getClassLoader() .getResourceAsStream("com/icesoft/applications/faces/auctionMonitor/readme.html")); StringWriter readmeWriter = new StringWriter(); char[] buf = new char[2000]; int len; try { while ((len = readmeReader.read(buf)) > -1) { readmeWriter.write(buf, 0, len); } } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Something went wrong while parsing the readme file, likely because of " + e); } } // clean up the stream reader readmeReader.close(); this.readmeText = readmeWriter.toString(); // clean up the writer readmeWriter.close(); return true; } catch (Exception e) { if (log.isWarnEnabled()) { log.warn("General error while attempting to load the readme file, cause may be " + e); } } return false; }
From source file:com.sg2net.utilities.ListaCAP.ListaComuniCapFromInternet.java
private Collection<String> getListaCAPFromHTMLPage(String url) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); Reader reader = new InputStreamReader(instream, HTML_ENCODING); StringWriter writer = new StringWriter(); int charsRead = 0; char[] buffer = new char[BUFFER]; while ((charsRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, charsRead); }/*from ww w .ja v a2s. co m*/ instream.close(); String content = writer.toString(); Collection<String> capList = new HashSet<>(); Matcher m = pattern.matcher(content); if (m.matches()) { String capMinAsString = m.group(2); String capPrefix = ""; int index = 0; while (capMinAsString.charAt(index) == '0') { capPrefix += "0"; index++; } Integer minCap = Integer.parseInt(m.group(2)); Integer maxCap = Integer.parseInt(m.group(3)); for (int i = minCap; i <= maxCap; i++) { capList.add(capPrefix + i); } logger.trace("trovati cap nella pagina html " + url); } else { logger.info("cap non trovati nella pagina html " + url); logger.info(content); } return capList; }
From source file:communication.WeatherService.java
private JSONObject doQuery(String subUrl) throws JSONException, IOException { String responseBody = null;//from www .ja v a 2 s.c om HttpGet httpget = new HttpGet(this.baseOwmUrl + subUrl); if (this.owmAPPID != null) { httpget.addHeader(WeatherService.APPID_HEADER, this.owmAPPID); } System.out.println("getting..."); HttpResponse response = this.httpClient.execute(httpget); System.out.println("...got"); InputStream contentStream = null; try { StatusLine statusLine = response.getStatusLine(); if (statusLine == null) { throw new IOException(String.format("Unable to get a response from OWM server")); } int statusCode = statusLine.getStatusCode(); if (statusCode < 200 && statusCode >= 300) { throw new IOException( String.format("OWM server responded with status code %d: %s", statusCode, statusLine)); } /* Read the response content */ HttpEntity responseEntity = response.getEntity(); contentStream = responseEntity.getContent(); Reader isReader = new InputStreamReader(contentStream); int contentSize = (int) responseEntity.getContentLength(); if (contentSize < 0) contentSize = 8 * 1024; StringWriter strWriter = new StringWriter(contentSize); char[] buffer = new char[8 * 1024]; int n = 0; while ((n = isReader.read(buffer)) != -1) { strWriter.write(buffer, 0, n); } responseBody = strWriter.toString(); contentStream.close(); } catch (IOException e) { throw e; } catch (RuntimeException re) { httpget.abort(); throw re; } finally { if (contentStream != null) contentStream.close(); } System.out.println(responseBody); //Testing by Brandon return new JSONObject(responseBody); }
From source file:com.github.util.HttpHandler.java
private String streamToString(InputStream is) throws IOException { StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); int n;//w w w. ja va2 s . com while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } is.close(); return writer.toString(); }
From source file:com.autburst.picture.server.HttpRequest.java
private String convertStreamToString(InputStream is) throws IOException { /*// w w w .j a v a 2 s . c o m * To convert the InputStream to String we use the * BufferedReader.readLine() method. We iterate until the BufferedReader * return null which means there's no more data to read. Each line will * appended to a StringBuilder and returned as String. */ // BufferedReader reader = new BufferedReader(new // InputStreamReader(is)); // StringBuilder sb = new StringBuilder(); // // String line = null; // try { // while ((line = reader.readLine()) != null) { // sb.append(line); // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // try { // is.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // return sb.toString(); try { InputStreamReader in = new InputStreamReader(is); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; for (int n; (n = in.read(buffer)) != -1;) sw.write(buffer, 0, n); return sw.toString(); } catch (IOException e) { throw new IOException(e.getMessage()); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.olat.modules.qpool.ui.TextPreviewController.java
protected String readSummary(VFSLeaf leaf) { StringWriter out = new StringWriter(); InputStream in = leaf.getInputStream(); InputStreamReader inr = new InputStreamReader(in); try {// ww w. j a v a 2s . c om char[] buffer = new char[4096]; int count = 0; int n = 0; while (-1 != (n = inr.read(buffer))) { out.write(buffer, 0, n); count += n; if (count >= 10000) { break; } } } catch (Exception e) { logError("", e); } finally { IOUtils.closeQuietly(inr); IOUtils.closeQuietly(in); } return out.toString(); }
From source file:screenieup.MixtapeUpload.java
private String getResponse(HttpURLConnection conn) throws IOException { System.out.println("Waiting for response..."); String response = "No response received, or something has gone wrong."; String charset = "UTF-8"; InputStream gzippedResponse = conn.getInputStream(); InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse); Reader reader = new InputStreamReader(ungzippedResponse, charset); StringWriter writer = new StringWriter(); char[] buffer = new char[10240]; for (int length = 0; (length = reader.read(buffer)) > 0;) { writer.write(buffer, 0, length); }//from w ww . j a va 2s . com response = writer.toString(); writer.close(); reader.close(); reader.close(); return response; }
From source file:org.kuali.kfs.sys.web.servlet.BatchFileUploadServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { checkAuthorization(request);// w w w . j a v a 2s. c o m ServletFileUpload upload = new ServletFileUpload(); String destPath = null; String fileName = null; String tempDir = System.getProperty("java.io.tmpdir"); // Parse the request try { FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); fileName = item.getName(); LOG.info("Processing Item: " + item.getFieldName()); if (item.isFormField()) { if (item.getFieldName().equals("uploadDir")) { Reader str = new InputStreamReader(item.openStream()); StringWriter sw = new StringWriter(); char buf[] = new char[100]; int len; while ((len = str.read(buf)) > 0) { sw.write(buf, 0, len); } destPath = sw.toString(); } } else { InputStream stream = item.openStream(); fileName = item.getName(); LOG.info("Uploading to Directory: " + tempDir); // Process the input stream FileOutputStream fos = new FileOutputStream(new File(tempDir, fileName)); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024 * 1024); byte buf[] = new byte[10240]; int len; while ((len = stream.read(buf)) > 0) { bos.write(buf, 0, len); } bos.close(); stream.close(); } } LOG.info("Copying to Directory: " + destPath); if (!getBatchDirectories().contains(destPath)) { new File(tempDir, fileName).delete(); throw new RuntimeException("Illegal Attempt to upload to an unauthorized path: '" + destPath + "'"); } BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(tempDir, fileName))); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(destPath, fileName)), 1024 * 1024); byte buf[] = new byte[10240]; int len; while ((len = bis.read(buf)) > 0) { bos.write(buf, 0, len); } bos.close(); bis.close(); } catch (FileUploadException ex) { LOG.error("Problem Uploading file", ex); } if (fileName != null) { request.setAttribute("message", "Successfully uploaded " + fileName + " to " + destPath); } else { request.setAttribute("message", "Upload Failed"); } doGet(request, response); }
From source file:screenieup.UguuUpload.java
/** * Get a response from Uguu.// www . j a v a2 s. c om * @param conn the connection to use to listen to response. * @return * @throws IOException during reading GZip response */ private String getResponse(HttpURLConnection conn) throws IOException { System.out.println("Waiting for response..."); String charset = "UTF-8"; // You should determine it based on response header. InputStream gzippedResponse = conn.getInputStream(); InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse); Reader reader = new InputStreamReader(ungzippedResponse, charset); StringWriter writer = new StringWriter(); char[] buffer = new char[10240]; for (int length = 0; (length = reader.read(buffer)) > 0;) { writer.write(buffer, 0, length); } String response = writer.toString(); writer.close(); reader.close(); return response; }