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:EchoServer.java

public void run() {
    System.out.println("Accepted Client : ID - " + clientID + " : Address - "
            + clientSocket.getInetAddress().getHostName());
    try {/* w w w  .j av a  2  s .c  om*/
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
        while (running) {
            String clientCommand = in.readLine();
            System.out.println("Client Says :" + clientCommand);
            if (clientCommand.equalsIgnoreCase("quit")) {
                running = false;
                System.out.print("Stopping client thread for client : " + clientID);
            } else {
                out.println(clientCommand);
                out.flush();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:eionet.cr.web.util.TriplesToOutputStream.java

/**
 *
 * @param out//  ww w .  j ava  2  s.  c o m
 * @param subjectUri
 * @param triples
 */
public static void triplesToHtml(OutputStream out, String subjectUri, List<SubjectDTO> triples) {

    OutputStreamWriter writer = new OutputStreamWriter(out);
    try {
        writer.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-gb\">");
        writer.append("<head>");
        writer.append("<title>").append(subjectUri).append("</title>");
        writer.append("<link rel=\"alternate\" type=\"application/rdf+xml\" href=\"").append(subjectUri)
                .append("\" />");
        writer.append("<style type=\"text/css\">" + "/*<![CDATA[*/ "
                + "table { border: 1px solid black; border-collapse:collapse; } "
                + "td, th { border: 1px solid black; padding: 0.3em; } " + "/*]]>*/" + "</style>");
        writer.append("</head>");
        writer.append("<body>");
        writer.append("<h1>").append(subjectUri).append("</h1>");
        writer.append("<table>");
        writer.append("<tr><th>Subject</th><th>Predicate</th><th>Value</th></tr>");

        if (triples != null) {
            for (SubjectDTO subject : triples) {
                Map<String, Collection<ObjectDTO>> predicates = subject.getPredicates();
                if (predicates != null) {
                    for (String predicateUri : predicates.keySet()) {
                        Collection<ObjectDTO> objects = predicates.get(predicateUri);
                        if (objects != null) {
                            for (ObjectDTO object : objects) {
                                writer.append("<tr>");
                                writer.append("<td>");
                                writer.append("<a href=\"").append(subject.getUri()).append("\">");
                                writer.append(subject.getUri());
                                writer.append("</a>");
                                writer.append("</td>");
                                writer.append("<td>").append(predicateUri).append("</td>");
                                writer.append("<td>");
                                if (object.isLiteral()) {
                                    writer.append(object.getValue());
                                } else {
                                    writer.append("<a href=\"").append(object.getValue()).append("\">")
                                            .append(object.getValue()).append("</a>");
                                }
                                writer.append("</td>");
                                writer.append("</tr>");
                            }
                        }
                    }
                }
            }
            /*
             * for (TripleDTO triple : triples) { writer.append("<tr>");
             * writer.append("<td>").append(triple.getSubjectUri()).append("</td>");
             * writer.append("<td>").append(triple.getPredicateUri()).append("</td>");
             * writer.append("<td>").append(triple.getObject()).append("</td>"); writer.append("</tr>"); }
             */
        }
        writer.append("</table>");
        writer.append("</body>");
        writer.append("</html>");
        writer.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            writer.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor)
        throws Exception {

    Process proc = null;//from   w w  w . j av  a  2  s  . c om
    int exitCode = -1;

    if (runAsRoot)
        proc = Runtime.getRuntime().exec("su");
    else
        proc = Runtime.getRuntime().exec("sh");

    OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream());

    //   TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor);

    out.write(cmd);
    out.write("\n");

    out.flush();
    out.write("exit\n");
    out.flush();

    if (waitFor) {

        final char buf[] = new char[10];

        // Consume the "stdout"
        InputStreamReader reader = new InputStreamReader(proc.getInputStream());
        int read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null)
                log.append(buf, 0, read);
        }

        // Consume the "stderr"
        reader = new InputStreamReader(proc.getErrorStream());
        read = 0;
        while ((read = reader.read(buf)) != -1) {
            if (log != null)
                log.append(buf, 0, read);
        }

        exitCode = proc.waitFor();

    }

    return exitCode;

}

From source file:Main.java

public static String request(String url, Map<String, String> cookies, Map<String, String> parameters)
        throws Exception {
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
    if (cookies != null && !cookies.isEmpty()) {
        StringBuilder cookieHeader = new StringBuilder();
        for (String cookie : cookies.values()) {
            if (cookieHeader.length() > 0) {
                cookieHeader.append(";");
            }/*from ww w.ja v a2 s .  c o m*/
            cookieHeader.append(cookie);
        }
        connection.setRequestProperty("Cookie", cookieHeader.toString());
    }
    connection.setDoInput(true);
    if (parameters != null && !parameters.isEmpty()) {
        connection.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
        osw.write(parametersToWWWFormURLEncoded(parameters));
        osw.flush();
        osw.close();
    }
    if (cookies != null) {
        for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) {
            if (headerEntry != null && headerEntry.getKey() != null
                    && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) {
                for (String header : headerEntry.getValue()) {
                    for (HttpCookie httpCookie : HttpCookie.parse(header)) {
                        cookies.put(httpCookie.getName(), httpCookie.toString());
                    }
                }
            }
        }
    }
    Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    StringWriter w = new StringWriter();
    char[] buffer = new char[1024];
    int n = 0;
    while ((n = r.read(buffer)) != -1) {
        w.write(buffer, 0, n);
    }
    r.close();
    return w.toString();
}

From source file:core.Web.java

public static String httpRequest(URL url, Map<String, String> properties, JSONObject message) {
    //        System.out.println(url);
    try {//from ww w .j a  va 2 s.  c o m
        // Create connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //connection.addRequestProperty("Authorization", API_KEY);
        // Set properties if needed
        if (properties != null && !properties.isEmpty()) {
            properties.forEach(connection::setRequestProperty);
        }
        // Post message
        if (message != null) {
            // Maybe somewhere
            connection.setDoOutput(true);
            //                connection.setRequestProperty("Accept", "application/json");
            try (BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(connection.getOutputStream()))) {
                writer.write(message.toString());
            }
        }
        // Establish connection
        connection.connect();
        int responseCode = connection.getResponseCode();

        if (responseCode != 200) {
            System.err.println("Error " + responseCode + ":" + url);
            return null;
        }

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            String line;
            String content = "";
            while ((line = reader.readLine()) != null) {
                content += line;
            }
            return content;
        }

    } catch (IOException ex) {
        Logger.getLogger(Web.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:matrix.CreateUserList.java

public void tweetsToUserList()
        throws FileNotFoundException, UnsupportedEncodingException, IOException, ParseException {

    File fout = new File(userListPathOutput);
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    BufferedReader inputTW = new BufferedReader(
            new InputStreamReader(new FileInputStream(tweetsJsonInput), "ISO-8859-9"));
    ArrayList userList = new ArrayList();
    JSONParser jsonParser = new JSONParser();
    JSONArray jsonArray = (JSONArray) jsonParser.parse(inputTW);
    int sayac = 0;
    for (Object obj : jsonArray) {

        JSONObject tweet = (JSONObject) obj;
        JSONObject user = (JSONObject) tweet.get("user");
        //            String userID = user.get("id").toString();
        //            String userName = user.get("name").toString();
        String userID = user.get("id").toString();
        String userName = user.get("name").toString();

        if (userList.contains(userID) == false) {
            userList.add(userID);/* w  w w  . jav  a  2 s.  co  m*/
            bw.write(userID + "," + userName);
            bw.newLine();
            sayac++;
        }

    }
    System.out.println(sayac);
}

From source file:musite.io.xml.XMLEscapeWriter.java

public void write(OutputStream os, Object fieldValue) throws IOException {
    if (os == null || fieldValue == null)
        return;/*from w  w  w .j  a v a2s  .  c om*/

    OutputStreamWriter osw = new OutputStreamWriter(os);

    String str = StringEscapeUtils.escapeXml(fieldValue.toString());

    for (int i = 0; i < getIndent(); i++)
        osw.write("\t");

    osw.write(str);
    osw.flush();
}

From source file:com.intropro.prairie.format.sv.SvFormatWriter.java

SvFormatWriter(OutputStream outputStream, char delimiter) {
    this.writer = new OutputStreamWriter(outputStream);
    this.delimiter = delimiter;
}

From source file:com.alibaba.otter.shared.common.utils.cmd.StreamAppender.java

public StreamAppender(OutputStream output) {
    this.output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)));
}

From source file:coolmap.application.io.internal.cmatrix.DefaultCMatrixExporter.java

@Override
public void dumpData(CMatrix matrix, TFile zipFolder) throws Exception {
    TFile outputFile = new TFile(zipFolder.getAbsolutePath() + File.separator + IOTerm.FILE_DATA);
    TFile propertyFile = new TFile(zipFolder.getAbsolutePath() + File.separator + IOTerm.FILE_PROPERTY);

    BufferedWriter propertyWriter = new BufferedWriter(
            new OutputStreamWriter(new TFileOutputStream(propertyFile)));

    JSONObject cmatrixPropertyEntry = new JSONObject();
    cmatrixPropertyEntry.put(IOTerm.ATTR_ID, matrix.getID());
    cmatrixPropertyEntry.put(IOTerm.ATTR_NAME, matrix.getName());
    cmatrixPropertyEntry.put(IOTerm.ATTR_CMATRIX_NUMROW, matrix.getNumRows());
    cmatrixPropertyEntry.put(IOTerm.ATTR_CMATRIX_NUMCOLUMN, matrix.getNumColumns());
    cmatrixPropertyEntry.put(IOTerm.ATTR_CLASS, matrix.getClass().getName());
    cmatrixPropertyEntry.put(IOTerm.ATTR_CMATRIX_MEMBERCLASS, matrix.getMemberClass().getName());

    //        System.out.println(cmatrixPropertyEntry);

    propertyWriter.write(cmatrixPropertyEntry.toString());

    propertyWriter.flush();//from www .j av  a  2  s.c  o  m
    propertyWriter.close();

    BufferedWriter dataWriter = new BufferedWriter(new OutputStreamWriter(new TFileOutputStream(outputFile)));

    //        dataWriter.write("This is where cmatrix will be dumped");
    //        part of the following code can be extracted for other use
    //        dataWriter properties
    dataWriter.write("Row/Column");
    for (int i = 0; i < matrix.getNumColumns(); i++) {
        dataWriter.write("\t");
        String colLabelString = matrix.getColLabel(i);
        if (colLabelString == null)
            colLabelString = "";
        dataWriter.write(colLabelString);
    }
    dataWriter.write("\n");

    for (int i = 0; i < matrix.getNumRows(); i++) {

        String rowLabelString = matrix.getRowLabel(i);
        if (rowLabelString == null)
            rowLabelString = "";
        dataWriter.write(rowLabelString);

        for (int j = 0; j < matrix.getNumColumns(); j++) {
            dataWriter.write("\t");
            Object value = matrix.getValue(i, j);

            if (value != null) {
                dataWriter.write(value.toString());
            }
        }
        dataWriter.write("\n");

    }

    dataWriter.flush();
    dataWriter.close();

    //        System.out.println("Dumping successful");
}