List of usage examples for java.io StringWriter close
public void close() throws IOException
From source file:org.apache.hadoop.lib.service.instrumentation.TestInstrumentationService.java
@Test public void sampler() throws Exception { final long value[] = new long[1]; Instrumentation.Variable<Long> var = new Instrumentation.Variable<Long>() { @Override//from ww w . j ava2 s . c o m public Long getValue() { return value[0]; } }; InstrumentationService.Sampler sampler = new InstrumentationService.Sampler(); sampler.init(4, var); assertEquals(sampler.getRate(), 0f, 0.0001); sampler.sample(); assertEquals(sampler.getRate(), 0f, 0.0001); value[0] = 1; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1) / 2, 0.0001); value[0] = 2; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1 + 2) / 3, 0.0001); value[0] = 3; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1 + 2 + 3) / 4, 0.0001); value[0] = 4; sampler.sample(); assertEquals(sampler.getRate(), (4d + 1 + 2 + 3) / 4, 0.0001); JSONObject json = (JSONObject) new JSONParser().parse(sampler.toJSONString()); assertEquals(json.size(), 2); assertEquals(json.get("sampler"), sampler.getRate()); assertEquals(json.get("size"), 4L); StringWriter writer = new StringWriter(); sampler.writeJSONString(writer); writer.close(); json = (JSONObject) new JSONParser().parse(writer.toString()); assertEquals(json.size(), 2); assertEquals(json.get("sampler"), sampler.getRate()); assertEquals(json.get("size"), 4L); }
From source file:com.webcohesion.enunciate.modules.gwt_json_overlay.GWTJSONOverlayModule.java
/** * Processes the specified template with the given model. * * @param templateURL The template URL.//w w w . j av a2 s . co m * @param model The root model. */ public String processTemplate(URL templateURL, Object model) throws IOException, TemplateException { debug("Processing template %s.", templateURL); Configuration configuration = new Configuration(Configuration.VERSION_2_3_22); configuration.setTemplateLoader(new URLTemplateLoader() { protected URL getURL(String name) { try { return new URL(name); } catch (MalformedURLException e) { return null; } } }); configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() { public void handleTemplateException(TemplateException templateException, Environment environment, Writer writer) throws TemplateException { throw templateException; } }); configuration.setLocalizedLookup(false); configuration.setDefaultEncoding("UTF-8"); configuration.setObjectWrapper(new GWTJSONOverlayObjectWrapper()); Template template = configuration.getTemplate(templateURL.toString()); StringWriter unhandledOutput = new StringWriter(); template.process(model, unhandledOutput); unhandledOutput.close(); return unhandledOutput.toString(); }
From source file:org.hawkular.apm.server.elasticsearch.SpanServiceElasticsearch.java
private String serialize(Object object) throws IOException { StringWriter out = new StringWriter(); JsonGenerator gen = mapper.getFactory().createGenerator(out); gen.writeObject(object);/* w ww .j ava2s . c o m*/ gen.close(); out.close(); return out.toString(); }
From source file:org.i3xx.step.uno.impl.ScriptCacheImpl.java
/** * Reads a script from the cache//from w w w . j a va 2 s. com * * @param index The index of the cached element * @return The String read * @throws IOException */ public String read(int index) throws IOException { ByteArrayInputStream bin = new ByteArrayInputStream(buffer[index]); InputStream in = compress ? new GZIPInputStream(bin) : bin; InputStreamReader r = new InputStreamReader(in); StringWriter w = new StringWriter(); char[] cbuf = new char[1024]; int c = 0; while ((c = r.read(cbuf)) > -1) w.write(cbuf, 0, c); r.close(); w.close(); return w.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); }/*w w w. ja va 2 s . c om*/ response = writer.toString(); writer.close(); reader.close(); reader.close(); return response; }
From source file:org.bibsonomy.scraper.url.kde.blackwell.BlackwellSynergyScraper.java
/** FIXME: refactor * Extract the content of a scitation.aip.org page. * (changed code from ScrapingContext.getContentAsString) * @param urlConn Connection to api page (from url.openConnection()) * @param cookie Cookie for auth./*ww w . j a v a2 s .co m*/ * @return Content of aip page. * @throws IOException */ private String getPageContent(HttpURLConnection urlConn, String cookie) throws IOException { urlConn.setAllowUserInteraction(true); urlConn.setDoInput(true); urlConn.setDoOutput(false); urlConn.setUseCaches(false); urlConn.setFollowRedirects(true); urlConn.setInstanceFollowRedirects(false); urlConn.setRequestProperty("Cookie", cookie); urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); urlConn.connect(); // build content StringWriter out = new StringWriter(); InputStream in = new BufferedInputStream(urlConn.getInputStream()); int b; while ((b = in.read()) >= 0) { out.write(b); } urlConn.disconnect(); in.close(); out.flush(); out.close(); return out.toString(); }
From source file:org.apache.ambari.view.hive.resources.uploads.TableDataReader.java
@Override public int read(char[] cbuf, int off, int len) throws IOException { int totalLen = len; int count = 0; do {/*www. j a v a 2 s.c o m*/ int n = stringReader.read(cbuf, off, len); if (n != -1) { // n were read len = len - n; // len more to be read off = off + n; // off now shifted to n more count += n; } if (count == totalLen) return count; // all totalLen characters were read if (iterator.hasNext()) { // keep reading as long as we keep getting rows StringWriter stringWriter = new StringWriter(CAPACITY); CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSV_FORMAT); Row row = iterator.next(); csvPrinter.printRecord(row.getRow()); stringReader.close(); // close the old string reader stringReader = new StringReader(stringWriter.getBuffer().toString()); csvPrinter.close(); stringWriter.close(); } else { return count == 0 ? -1 : count; } } while (count < totalLen); return count; }
From source file:org.eclipse.scada.utils.pkg.deb.DebianPackageWriter.java
protected ContentProvider createControlContent() throws IOException { this.packageControlFile.set(BinaryPackageControlFile.Fields.INSTALLED_SIZE, "" + this.installedSize); final StringWriter sw = new StringWriter(); try (ControlFileWriter writer = new ControlFileWriter(sw)) { this.packageControlFile.write(writer); }/*from www .ja v a 2 s .c o m*/ sw.close(); return new StaticContentProvider(sw.toString()); }
From source file:nl.ru.cmbi.vase.web.rest.JobRestResource.java
@MethodMapping(value = "/hsspresult/{id}", httpMethod = HttpMethod.GET, produces = RestMimeTypes.TEXT_PLAIN) public String hsspResult(String id) { if (Config.isXmlOnly()) { log.warn("rest/hsspresult was requested, but not enabled"); // hssp job submission is not allowed if hssp is turned off throw new AbortWithHttpErrorCodeException(HttpURLConnection.HTTP_NOT_FOUND); }/*from w ww . j a v a 2s. c om*/ File hsspFile = new File(Config.getHSSPCacheDir(), id + ".hssp.bz2"); String jobStatus = this.status(id); try { if (jobStatus.equals("SUCCESS") && hsspFile.isFile()) { StringWriter sw = new StringWriter(); InputStream hsspIn = new BZip2CompressorInputStream(new FileInputStream(hsspFile)); IOUtils.copy(hsspIn, sw); hsspIn.close(); sw.close(); return sw.toString(); } URL url = new URL(hsspRestURL + "/result/pdb_file/hssp_stockholm/" + id + "/"); Writer writer = new StringWriter(); IOUtils.copy(url.openStream(), writer); writer.close(); JSONObject output = new JSONObject(writer.toString()); String result = output.getString("result"); if (jobStatus.equals("SUCCESS") && Config.hsspPdbCacheEnabled()) { // Write it to the cache: OutputStream fileOut = new BZip2CompressorOutputStream(new FileOutputStream(hsspFile)); IOUtils.write(result, fileOut); fileOut.close(); } else return ""; return result; } catch (Exception e) { log.error(e.getMessage(), e); throw new AbortWithHttpErrorCodeException(HttpURLConnection.HTTP_INTERNAL_ERROR); } }
From source file:screenieup.UguuUpload.java
/** * Get a response from Uguu.//from ww w .ja v a 2s.co m * @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; }