List of usage examples for java.io OutputStreamWriter flush
public void flush() throws IOException
From source file:org.gbif.portal.web.view.ZippedVelocityView.java
/** * Write out the supplied template./*from www. j a va2 s . c om*/ * * @param templatePath * @param outputStream * @throws Exception */ protected void writeTemplate(VelocityContext velocityContext, HttpServletRequest request, String templatePath, OutputStream outputStream) throws Exception { if (StringUtils.isNotEmpty(templatePath)) { Template template = Velocity.getTemplate(templatePath); template.initDocument(); TemplateUtils tu = new TemplateUtils(); OutputStreamWriter writer = new OutputStreamWriter(outputStream); tu.merge(template, velocityContext, writer); writer.flush(); } }
From source file:net.noday.cat.web.admin.DwzManager.java
@RequestMapping(method = RequestMethod.POST) public Model gen(@RequestParam("url") String url, @RequestParam("alias") String alias, Model m) { try {// ww w . ja v a 2 s . c o m String urlstr = "http://dwz.cn/create.php"; URL requrl = new URL(urlstr); URLConnection conn = requrl.openConnection(); conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); String param = String.format(Locale.CHINA, "url=%s&alias=%s", url, alias); out.write(param); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = reader.readLine(); //{"longurl":"http:\/\/www.hao123.com\/","status":0,"tinyurl":"http:\/\/dwz.cn\/1RIKG"} ObjectMapper mapper = new ObjectMapper(); Dwz dwz = mapper.readValue(line, Dwz.class); if (dwz.getStatus() == 0) { responseData(m, dwz.getTinyurl()); } else { responseMsg(m, false, dwz.getErr_msg()); } } catch (MalformedURLException e) { log.error(e.getMessage(), e); responseMsg(m, false, e.getMessage()); } catch (IOException e) { log.error(e.getMessage(), e); responseMsg(m, false, e.getMessage()); } return m; }
From source file:org.talend.components.webtest.JsonIo2HttpMessageConverter.java
/** * Serializes specified Object and writes JSON to HTTP message body * /* w w w .jav a 2s . c o m*/ * @param t Object to serialize * @param outputMessage the HTTP output message to write to */ @Override protected void writeInternal(Object t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputMessage.getBody()); String objectToJson = JsonWriter.objectToJson(t); outputStreamWriter.write(objectToJson); outputStreamWriter.flush(); outputStreamWriter.close(); }
From source file:com.aoyetech.fee.commons.utils.IOUtils.java
/** * Copy chars from a <code>Reader</code> to bytes on an * <code>OutputStream</code> using the specified character encoding, and * calling flush./*from ww w.j ava 2s.c om*/ * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * <p> * Character encoding names can be found at <a * href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * Due to the implementation of OutputStreamWriter, this method performs a * flush. * <p> * This method uses {@link OutputStreamWriter}. * * @param input the <code>Reader</code> to read from * @param output the <code>OutputStream</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static void copy(final Reader input, final OutputStream output, final String encoding) throws IOException { if (encoding == null) { copy(input, output); } else { final OutputStreamWriter out = new OutputStreamWriter(output, encoding); copy(input, out); // XXX Unless anyone is planning on rewriting OutputStreamWriter, // we have to flush here. out.flush(); } }
From source file:com.google.wave.api.oauth.impl.OpenSocialHttpClient.java
/** * Executes a request and writes all data in the request's body to the * output stream./*from w w w. j a v a 2s . c o m*/ * * @param method * @param url * @param body * @return Response message */ private OpenSocialHttpResponseMessage send(String method, OpenSocialUrl url, String contentType, String body) throws IOException { HttpURLConnection connection = null; try { connection = getConnection(method, url, contentType); if (body != null) { OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(body); out.flush(); out.close(); } return new OpenSocialHttpResponseMessage(method, url, connection.getInputStream(), connection.getResponseCode()); } catch (IOException e) { throw new IOException( "Container returned status " + connection.getResponseCode() + " \"" + e.getMessage() + "\""); } }
From source file:com.EconnectTMS.CustomerInformation.java
public void Run(String IncomingMessage, String intid) throws MalformedURLException, UnsupportedEncodingException, IOException { try {//w w w . java 2 s . c om strRecievedData = IncomingMessage.split("#"); processingcode = strRecievedData[0]; switch (processingcode) { case "800000": AccountNumber = strRecievedData[1]; URL url = new URL(CUSTOMER_DETAILS_URL); Map<String, String> params = new LinkedHashMap<>(); params.put("username", CUSTOMER_DETAILS_USERNAME); params.put("password", CUSTOMER_DETAILS_PASSWORD); params.put("source", CUSTOMER_DETAILS_SOURCE_ID); params.put("account", AccountNumber); postData = new StringBuilder(); for (Map.Entry<String, String> param : params.entrySet()) { if (postData.length() != 0) { postData.append('&'); } postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); postData.append('='); postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } urlParameters = postData.toString(); conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(urlParameters); writer.flush(); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); while ((line = reader.readLine()) != null) { result += line; } writer.close(); reader.close(); JSONObject respobj = new JSONObject(result); if (respobj.has("FSTNAME") || respobj.has("MIDNAME") || respobj.has("LSTNAME")) { if (respobj.has("FSTNAME")) { fname = respobj.get("FSTNAME").toString().toUpperCase() + ' '; } if (respobj.has("MIDNAME")) { mname = respobj.get("MIDNAME").toString().toUpperCase() + ' '; } if (respobj.has("LSTNAME")) { lname = respobj.get("LSTNAME").toString().toUpperCase() + ' '; } strCustomerName = fname + mname + lname; } else { strCustomerName = " "; } System.out.println("result:" + result); func.SendPOSResponse(strCustomerName + "#", intid); return; default: break; } } catch (Exception ex) { strCustomerName = ""; func.log("\nSEVERE CustomerInformation() :: " + ex.getMessage() + "\n" + func.StackTraceWriter(ex), "ERROR"); } }
From source file:nl.ru.ai.projects.parrot.tools.TwitterAccess.java
/** * Uploads an image//from ww w .j a v a 2 s .c o m * @param image The image to upload * @param tweet The text of the tweet that needs to be posted with the image */ public void uploadImage(byte[] image, String tweet) { if (!enabled) return; ByteArrayOutputStream baos = new ByteArrayOutputStream(); RenderedImage img = getImageFromCamera(image); try { ImageIO.write(img, "jpg", baos); URL url = new URL("http://api.imgur.com/2/upload.json"); String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()), "UTF-8"); data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode("f9c4861fc0aec595e4a64dd185751d28", "UTF-8"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); StringBuffer buffer = new StringBuffer(); InputStreamReader isr = new InputStreamReader(conn.getInputStream()); Reader in = new BufferedReader(isr); int ch; while ((ch = in.read()) > -1) buffer.append((char) ch); String imgURL = processJSON(buffer.toString()); setStatus(tweet + " " + imgURL, 100); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:IOUtils.java
/** * Copy chars from a <code>Reader</code> to bytes on an * <code>OutputStream</code> using the specified character encoding, and * calling flush./*ww w . j a va 2s .c o m*/ * <p> * This method buffers the input internally, so there is no need to use a * <code>BufferedReader</code>. * <p> * Character encoding names can be found at * <a href="http://www.iana.org/assignments/character-sets">IANA</a>. * <p> * Due to the implementation of OutputStreamWriter, this method performs a * flush. * <p> * This method uses {@link OutputStreamWriter}. * * @param input the <code>Reader</code> to read from * @param output the <code>OutputStream</code> to write to * @param encoding the encoding to use, null means platform default * @throws NullPointerException if the input or output is null * @throws IOException if an I/O error occurs * @since Commons IO 1.1 */ public static void copy(Reader input, OutputStream output, String encoding) throws IOException { if (encoding == null) { copy(input, output); } else { OutputStreamWriter out = new OutputStreamWriter(output, encoding); copy(input, out); // XXX Unless anyone is planning on rewriting OutputStreamWriter, // we have to flush here. out.flush(); } }
From source file:com.domsplace.DomsCommandsXenforoAddon.Threads.XenforoUpdateThread.java
@Override public void run() { debug("THREAD STARTED"); long start = getNow(); while (start + TIMEOUT <= getNow() && player == null) { }/*from w ww .ja v a 2 s.c om*/ this.stopThread(); this.deregister(); if (player == null) return; boolean alreadyActivated = false; boolean isActivated = false; try { String x = player.getSavedVariable("xenforo"); alreadyActivated = x.equalsIgnoreCase("yes"); } catch (Exception e) { } try { String url = XenforoManager.XENFORO_MANAGER.yml.getString("xenforo.root", "") + APPEND; if (url.equals(APPEND)) return; String encodedData = "name=" + encode("username") + "&value=" + encode(this.player.getPlayer()) + "&_xfResponseType=json"; //String rawData = "name=username"; String type = "application/x-www-form-urlencoded; charset=UTF-8"; //String encodedData = URLEncoder.encode(rawData, "ISO-8859-1"); URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", type); conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length())); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setRequestProperty("Referer", XenforoManager.XENFORO_MANAGER.yml.getString("xenforo.root", "")); conn.setUseCaches(false); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(encodedData); wr.flush(); InputStream in = conn.getInputStream(); BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuilder responseStrBuilder = new StringBuilder(); String inputStr; while ((inputStr = streamReader.readLine()) != null) responseStrBuilder.append(inputStr); String out = responseStrBuilder.toString(); JSONObject o = (JSONObject) JSONValue.parse(out); try { isActivated = o.get("_redirectMessage").toString().equalsIgnoreCase("ok"); } catch (NullPointerException e) { isActivated = true; } debug("GOT: " + isActivated); } catch (Exception e) { if (DebugMode) e.printStackTrace(); return; } player.setSavedVariable("xenforo", (isActivated ? "yes" : "no")); player.save(); if (isActivated && !alreadyActivated) { runCommands(XenforoManager.XENFORO_MANAGER.yml.getStringList("commands.onRegistered")); } if (!isActivated && alreadyActivated) { runCommands(XenforoManager.XENFORO_MANAGER.yml.getStringList("commands.onDeRegistered")); } }
From source file:com.example.user.rideshareapp1.MyGcmListenerService.java
/** * Called when message is received.// w ww. j a v a 2s. c o m * * @param from SenderID of the sender. * @param data Data bundle containing message data as key/value pairs. * For Set of keys use data.keySet(). */ // [START receive_message] @Override public void onMessageReceived(String from, Bundle data) { //TODO find a way to find if it is chat and store it in the file, create a notification if (android.os.Debug.isDebuggerConnected()) android.os.Debug.waitForDebugger(); String message = data.getString("message"); String id; if (data.containsKey("topic")) { id = data.getString("ride"); try (FileOutputStream fOut = openFileOutput("chat_" + id + ".txt", MODE_WORLD_READABLE | MODE_APPEND)) { OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write(data.getString("name") + ":" + data.getString("message") + "`"); osw.flush(); osw.close(); } catch (IOException e) { Toast.makeText(this, "Failed to open chat file from notification", Toast.LENGTH_LONG).show(); } } else { sendNotification( data.getString("title") + "`" + data.getString("name") + "`" + data.getString("message")); } Log.i(TAG, "From: " + from); Log.i(TAG, "Message: " + message); // [START_EXCLUDE] /** * Production applications would usually process the message here. * Eg: - Syncing with server. * - Store message in local database. * - Update UI. */ /** * In some cases it may be useful to show a notification indicating to the user * that a message was received. */ // [END_EXCLUDE] }