Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out) 

Source Link

Document

Creates an OutputStreamWriter that uses the default character encoding.

Usage

From source file:io.mingle.v1.Connection.java

public Response run(String comprehension) {
    String expr = "{ \"query\": \"" + comprehension + "\", \"limit\": 10000 }";
    try {//  ww w  . jav  a 2s.co  m
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        conn.connect();

        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(expr, 0, expr.length());
        out.flush();
        out.close();

        InputStream in = conn.getInputStream();
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        byte[] chunk = new byte[4096];
        int read = 0;
        while ((read = in.read(chunk)) > 0) {
            buf.write(chunk, 0, read);
        }
        in.close();

        String str = buf.toString();
        System.out.println("GOT JSON: " + str);
        return new Response(JSONValue.parse(str));
    } catch (Exception e) {
        System.err.printf("failed to execute: %s\n", expr);
        e.printStackTrace();
    }

    return null;
}

From source file:com.boundlessgeo.geoserver.json.JSONWrapper.java

/**
 * Encodes a wrapper as JSON.// www  .j av  a  2 s. c  om
 *
 * @param obj The wrapper.
 * @param output Target output stream.
 *
 */
public static void write(JSONWrapper<? extends JSONStreamAware> obj, OutputStream output) throws IOException {
    write(obj, new OutputStreamWriter(output));
}

From source file:com.amalto.core.storage.ItemPKCriteriaResultsWriter.java

@Override
public void write(DataRecord record, OutputStream output) throws IOException {
    doWrite(record, new OutputStreamWriter(output));
}

From source file:Streams.java

public static void drain(Reader r, OutputStream os) throws IOException {
    Writer w = new OutputStreamWriter(os);
    drain(r, w);/*from  w  ww .  j ava 2  s .c  o  m*/
    w.flush();
}

From source file:com.healthcit.cacure.test.RecreateDBUnitDTDTestCase.java

@Test
public void doRecreate() throws DatabaseUnitException, SQLException, FileNotFoundException {
    IDatabaseConnection dconnection = new DatabaseConnection(dataSource.getConnection());
    IDataSet dataSet = dconnection.createDataSet();
    File file = new File("formbuilder-dataset.dtd");

    Assert.assertTrue("DTD file can not be deleted.", !file.exists() || file.delete());

    Writer out = new OutputStreamWriter(new FileOutputStream(file));
    FlatDtdWriter datasetWriter = new FlatDtdWriter(out);
    datasetWriter.setContentModel(FlatDtdWriter.CHOICE);
    // You could also use the sequence model which is the default
    // datasetWriter.setContentModel(FlatDtdWriter.SEQUENCE);
    datasetWriter.write(dataSet);/* w  ww.j a v a 2  s  .  c o m*/

    Assert.assertTrue("DTD file did not created.", file.exists());
}

From source file:brugerautorisation.server.BrugerProtocol.java

private void cmd(List<String> msg) {
    OutputStreamWriter out = null;
    Writer writer = null;//ww  w.ja  v  a  2s . co  m
    try {
        out = new OutputStreamWriter(server.getOutputStream());
        writer = new BufferedWriter(out);
        switch (msg.get(0).toLowerCase().replace("\n", "")) {
        case ("hello"):
            break;
        case ("login"):
            System.out.println("v");
            JSONObject json = new JSONObject(msg.get(1));
            JSONObject json_return = new JSONObject();
            try {
                Bruger b = ba.hentBruger(json.getString("username"), json.getString("password"));
                json_return.put("username", b.brugernavn);
            } catch (SOAPFaultException e) {
                json_return.put("error", e.getMessage());
            }
            writer.write(json_return.toString());
            writer.flush();
            //out.flush();
            break;
        case ("forgot_pass"):
            break;
        default:
            System.out.println("ikke fundet");
            break;
        }
    } catch (IOException ex) {
        Logger.getLogger(BrugerProtocol.class.getName()).log(Level.SEVERE, null, ex);
    } catch (JSONException ex) {
        Logger.getLogger(BrugerProtocol.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            writer.close();
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(BrugerProtocol.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

From source file:jetbrains.exodus.util.Streamer.java

public Streamer(@NotNull Socket socket) throws IOException {
    socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()), BUFFER_SIZE);
    socketOutput = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
}

From source file:com.msopentech.odatajclient.engine.data.Serializer.java

/**
 * Writes <tt>FeedResource</tt> object onto the given stream.
 *
 * @param <T> feed resource type./*from  w w  w.j  ava2s.co m*/
 * @param obj object to be streamed.
 * @param out output stream.
 */
public static <T extends FeedResource> void feed(final T obj, final OutputStream out) {
    feed(obj, new OutputStreamWriter(out));
}

From source file:com.jamesgiang.aussnowcam.Utils.java

public static void WriteSettings(Context context, String data, String file) throws IOException {
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    fOut = context.openFileOutput(file, Context.MODE_PRIVATE);
    osw = new OutputStreamWriter(fOut);
    osw.write(data);// w w  w  .  j  a va  2  s.  co  m
    osw.close();
    fOut.close();
}

From source file:com.company.project.core.connector.HttpClientHelper.java

public static String getResponseStringFromConn(HttpURLConnection conn, String payLoad) throws IOException {

    // Send the http message payload to the server.
    if (payLoad != null) {
        conn.setDoOutput(true);//from ww  w .  ja  v a2  s. co m
        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream());
        osw.write(payLoad);
        osw.flush();
        osw.close();
    }

    // Get the message response from the server.
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line = "";
    StringBuffer stringBuffer = new StringBuffer();
    while ((line = br.readLine()) != null) {
        stringBuffer.append(line);
    }

    br.close();

    return stringBuffer.toString();
}