List of usage examples for java.io OutputStream flush
public void flush() throws IOException
From source file:edu.toronto.cs.phenotips.hpoa.PhenotypeMappingScriptService.java
public File getInputFileHandler(String inputLocation, boolean forceUpdate) { try {/*from www. j a v a2 s. c o m*/ File result = new File(inputLocation); if (!result.exists() || result.length() == 0) { String name = inputLocation.substring(inputLocation.lastIndexOf('/') + 1); result = getTemporaryFile(name); if (!result.exists()) { BufferedInputStream in = new BufferedInputStream((new URL(inputLocation)).openStream()); result.createNewFile(); OutputStream out = new FileOutputStream(result); IOUtils.copy(in, out); out.flush(); out.close(); } } return result; } catch (IOException ex) { this.logger.error("Mapping file [{}] not found", inputLocation); return null; } }
From source file:it.bz.tis.integreen.carsharingbzit.api.ApiClient.java
public <T> T callWebService(ServiceRequest request, Class<T> clazz) throws IOException { request.request.technicalUser.username = this.user; request.request.technicalUser.password = this.password; ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE) .setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY) .setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY) .setVisibility(PropertyAccessor.SETTER, Visibility.PUBLIC_ONLY); mapper.enable(SerializationFeature.INDENT_OUTPUT); StringWriter sw = new StringWriter(); mapper.writeValue(sw, request);//ww w . jav a2 s. c o m String requestJson = sw.getBuffer().toString(); logger.debug("callWebService(): jsonRequest:" + requestJson); URL url = new URL(this.endpoint); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); out.write(requestJson.getBytes("UTF-8")); out.flush(); int responseCode = conn.getResponseCode(); InputStream input = conn.getInputStream(); ByteArrayOutputStream data = new ByteArrayOutputStream(); int len; byte[] buf = new byte[50000]; while ((len = input.read(buf)) > 0) { data.write(buf, 0, len); } conn.disconnect(); String jsonResponse = new String(data.toByteArray(), "UTF-8"); if (responseCode != 200) { throw new IOException(jsonResponse); } logger.debug("callWebService(): jsonResponse:" + jsonResponse); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); T response = mapper.readValue(new StringReader(jsonResponse), clazz); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); sw = new StringWriter(); mapper.writeValue(sw, response); logger.debug( "callWebService(): parsed response into " + response.getClass().getName() + ":" + sw.toString()); return response; }
From source file:net.sf.housekeeper.persistence.PersistenceController.java
/** * Persistently saves domain objects using the default * {@link PersistenceService}.//from w w w . j a v a 2 s. c om * * @param domain The domain which shall be saved. * @throws IOException If the data couldn't be stored. */ public void save(final Household domain) throws IOException { LOG.info("Saving data to: " + dataFile); final OutputStream dataStream = new BufferedOutputStream(new FileOutputStream(dataFile)); jibxPersistence.saveData(domain, dataStream); dataStream.flush(); dataStream.close(); }
From source file:de.root1.logiccollection.offLogicVocESP8266.java
@Override public void init() { this.tt = new TimerTask() { @Override//w w w. ja v a2 s . c o m public void run() { try { Socket s = new Socket("nodemcu1", 44444); OutputStream out = s.getOutputStream(); InputStream in = s.getInputStream(); out.write("\n".getBytes()); out.flush(); InputStreamReader isr = new InputStreamReader(in); JSONObject data = (JSONObject) JSONValue.parse(isr); isr.close(); out.close(); int voc = Integer.parseInt(data.get("voc").toString()); int tvoc = Integer.parseInt(data.get("voc").toString()); int resistance = Integer.parseInt(data.get("resistance").toString()); int status = Integer.parseInt(data.get("status").toString()); log.info("voc={}, tvoc={} resistance={} status={}", new Object[] { voc, tvoc, resistance, status }); // write(ga, String.valueOf(voc)); } catch (IOException ex) { ex.printStackTrace(); } } }; setPA("1.1.203"); t.schedule(tt, 5000, 60000); log.info("VOC ESP8266 reader is running."); }
From source file:org.openmrs.web.controller.customdatatype.CustomValueController.java
/** * Downloads a custom value/*from w w w .j ava2s. c om*/ * * @param response * @param handlerClassname * @param datatypeClassname * @param valueReference * @throws IOException */ @RequestMapping(method = RequestMethod.GET, value = "**/downloadCustomValue.form") public void downloadCustomValue(HttpServletResponse response, @RequestParam("handler") String handlerClassname, @RequestParam(value = "datatype", required = true) String datatypeClassname, @RequestParam(value = "value", required = true) String valueReference) throws IOException { // get the datatype and handler CustomDatatype datatype = CustomDatatypeUtil.getDatatype(datatypeClassname, null); CustomDatatypeHandler<?, ?> handler = CustomDatatypeUtil.getHandler(datatype, handlerClassname, null); if (!(handler instanceof DownloadableDatatypeHandler)) { throw new IllegalArgumentException(handler.getClass().getName() + " does not support downloading"); } // die if not enough information if (datatype == null || StringUtils.isBlank(valueReference)) { throw new IOException("datatype and value are required parameters"); } // render the output DownloadableDatatypeHandler<?> downloadHandler = (DownloadableDatatypeHandler<?>) handler; response.setHeader("Content-Type", downloadHandler.getContentType(datatype, valueReference)); String filename = downloadHandler.getFilename(datatype, valueReference); if (filename == null) { filename = "openmrs-custom-value_" + valueReference + ".txt"; } response.setHeader("Content-Disposition", "attachment; filename=" + filename); // write the resource as a string OutputStream out = response.getOutputStream(); downloadHandler.writeToStream(datatype, valueReference, out); out.flush(); }
From source file:com.nirima.jenkins.webdav.impl.methods.Get.java
@Override protected void writeContent(IDavFile fileItem) throws IOException { InputStream is = null;//from ww w . j a v a2 s . com try { logger.trace("GET {}", fileItem); is = fileItem.getContent(); OutputStream os = this.getResponse().getOutputStream(); long start = System.currentTimeMillis(); long bytes = ByteStreams.copy(is, os); os.flush(); long duration = System.currentTimeMillis() - start; if (duration == 0) duration = 1; // round up. logger.info("Sent {} : {} bytes in {} ms ({}kB/sec", fileItem, bytes, duration, (bytes / duration)); } catch (IOException ex) { logger.error("Error trying to GET item {} ", fileItem); logger.error("Error: ", ex); throw ex; } finally { if (is != null) is.close(); } }
From source file:com.openkm.extension.servlet.StaplingDownloadServlet.java
/** * /*from ww w.j a v a 2 s . c o m*/ */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); int sgId = WebUtils.getInt(request, "sgId"); File tmpZip = File.createTempFile("okm", ".zip"); try { String archive = "stapling.zip"; response.setHeader("Content-disposition", "attachment; filename=\"" + archive + "\""); response.setContentType("application/zip"); OutputStream out = response.getOutputStream(); exportZip(sgId, out); out.flush(); out.close(); } catch (RepositoryException e) { log.warn(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "RepositoryException: " + e.getMessage()); } catch (Exception e) { log.warn(e.getMessage(), e); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); } finally { FileUtils.deleteQuietly(tmpZip); } }
From source file:UnpackedJarFile.java
public static void flush(OutputStream thing) { if (thing != null) { try {//from w ww . j a v a2 s. co m thing.flush(); } catch (Exception ignored) { } } }
From source file:at.ac.tuwien.dsg.quelle.extensions.neo4jPersistenceAdapter.DataAccess.java
public void writeGraphAsGraphVis(String filePath) throws Exception { Transaction t = graphDatabaseService.beginTx(); try {//from w ww . j av a 2s. c o m GraphvizWriter writer = new GraphvizWriter(); OutputStream out = new FileOutputStream(filePath); writer.emit(out, Walker.fullGraph(graphDatabaseService)); out.flush(); out.close(); t.success(); } catch (IOException e) { t.failure(); } t.finish(); }
From source file:fr.qinder.api.APIGetter.java
protected HttpsURLConnection post(String sUrl, APIRequest request) { HttpsURLConnection urlConnection; URL url;//from w w w . j a v a 2 s . c o m try { url = new URL(sUrl); urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); if (request.getPosts().size() != 0) { urlConnection.setRequestMethod("POST"); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(request.getPosts()); OutputStream post = urlConnection.getOutputStream(); entity.writeTo(post); post.flush(); } urlConnection.connect(); } catch (IOException e) { urlConnection = null; } return urlConnection; }