Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

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

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this output stream.

Usage

From source file:eu.eubrazilcc.lvl.core.io.FastaWriter.java

public static void writeFasta(final OutputStream os, final GBSeq gbSeq) throws IOException {
    final String header = fastaHeader(gbSeq);
    os.write(FASTA_BEGIN);
    os.write(header.getBytes());//from   w w  w.  j a v a 2 s . c o  m
    os.write(LINE_SEPARATOR);

    int compoundCount = 0;
    final String sequence = trimToEmpty(gbSeq.getGBSeqSequence()).toUpperCase();
    for (int i = 0; i < sequence.length(); i++) {
        os.write(sequence.charAt(i));
        compoundCount++;
        if (compoundCount == LINE_LENGTH) {
            os.write(LINE_SEPARATOR);
            compoundCount = 0;
        }
    }

    if ((sequence.length() % LINE_LENGTH) != 0) {
        os.write(LINE_SEPARATOR);
    }
}

From source file:edu.cwru.jpdg.Dotty.java

/**
 * Compiles the graph to dotty.//  w w  w  .jav a 2  s  . c om
 */
public static String dotty(String graph) {

    byte[] bytes = graph.getBytes();

    try {
        ProcessBuilder pb = new ProcessBuilder("dotty");
        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
        stdin.write(bytes);
        stdin.close();

        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        List<String> stdout_lines = new ArrayList<String>();
        line = stdout.readLine();
        while (line != null) {
            stdout_lines.add(line);
            line = stdout.readLine();
        }
        if (p.waitFor() != 0) {
            throw new RuntimeException("javac failed");
        }

        return StringUtils.join(stdout_lines, "\n");
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static boolean symlink(File inFile, File outFile) {
    int exitCode = -1;
    try {//from w  ww  .  j av  a2  s  .  c o m
        Process sh = Runtime.getRuntime().exec("sh");
        OutputStream out = sh.getOutputStream();
        String command = "/system/bin/ln -s " + inFile + " " + outFile + "\nexit\n";
        out.write(command.getBytes("ASCII"));

        final char buf[] = new char[40];
        InputStreamReader reader = new InputStreamReader(sh.getInputStream());
        while (reader.read(buf) != -1)
            throw new IOException("stdout: " + new String(buf));
        reader = new InputStreamReader(sh.getErrorStream());
        while (reader.read(buf) != -1)
            throw new IOException("stderr: " + new String(buf));

        exitCode = sh.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }
    return exitCode == 0;
}

From source file:com.hunantv.fw.utils.StreamUtils.java

/**
 * Copy the contents of the given byte array to the given OutputStream.
 * Leaves the stream open when done.//from w w  w .  ja  va2s  .c o  m
 * @param in the byte array to copy from
 * @param out the OutputStream to copy to
 * @throws IOException in case of I/O errors
 */
public static void copy(byte[] in, OutputStream out) throws IOException {
    out.write(in);
}

From source file:edu.cwru.jpdg.Dotty.java

public static void graphviz(String name, String graph) {

    byte[] bytes = graph.getBytes();
    try {/*from ww w . j a  v a 2  s .c  om*/
        Path p = Paths.get("./reports/" + name + ".dot");
        Files.write(p, bytes);
    } catch (IOException e) {
        System.out.println(graph);
    }

    try {
        ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "-o", "reports/" + name + ".png");
        pb.redirectError(ProcessBuilder.Redirect.INHERIT);
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
        stdin.write(bytes);
        stdin.close();

        String line;
        BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
        List<String> stdout_lines = new ArrayList<String>();
        line = stdout.readLine();
        while (line != null) {
            stdout_lines.add(line);
            line = stdout.readLine();
        }
        if (p.waitFor() != 0) {
            throw new RuntimeException("javac failed");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *//*  w w w  . j  a v  a2s.  c om*/
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        urlConnection.connect();
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } catch (IOException e) {
        String details;
        if (urlConnection != null) {
            details = "; code=" + urlConnection.getResponseCode() + " (" + urlConnection.getResponseMessage()
                    + ")";
        } else {
            details = "";
        }
        Log.e("ExoplayerUtil", "executePost: Request failed" + details, e);
        throw e;
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:Main.java

/**
 * Executes a post request using {@link HttpURLConnection}.
 *
 * @param url The request URL./*from   w w  w  . j  a  v  a  2s .  co  m*/
 * @param data The request body, or null.
 * @param requestProperties Request properties, or null.
 * @return The response body.
 * @throws IOException If an error occurred making the request.
 */
// TODO: Remove this and use HttpDataSource once DataSpec supports inclusion of a POST body.
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        // Write the request body, if there is one.
        if (data != null) {
            OutputStream out = urlConnection.getOutputStream();
            try {
                out.write(data);
            } finally {
                out.close();
            }
        }
        // Read and return the response body.
        InputStream inputStream = urlConnection.getInputStream();
        try {
            return toByteArray(inputStream);
        } finally {
            inputStream.close();
        }
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:io.undertow.server.handlers.ChunkedResponseTransferCodingTestCase.java

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {
        @Override// ww  w .ja  v a  2s.c  o  m
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = exchange.getConnection();
                } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy()
                        && connection != exchange.getConnection()) {
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, List<String> names) throws IOException {
    OutputStream stream = connection.getOutputStream();
    String body = JSONArray.toJSONString(names);
    stream.write(body.getBytes());
    stream.flush();/*ww w .  j a  v a2s .c  o  m*/
    stream.close();
}

From source file:Main.java

public static String executePost(String targetURL, String urlParameters) {
    try {/*from   ww  w  .  j  ava 2 s. com*/
        HttpURLConnection connection = (HttpURLConnection) new URL(targetURL).openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        //connection.setRequestProperty("Accept-Charset", charset);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStream output = null;
        try {
            output = connection.getOutputStream();
            output.write(urlParameters.getBytes());
        } finally {
            if (output != null)
                try {
                    output.flush();
                    output.close();
                } catch (IOException logOrIgnore) {
                }
        }
        InputStream response = connection.getInputStream();
        String contentType = connection.getHeaderField("Content-Type");
        String responseStr = "";
        if (true) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(new InputStreamReader(response));
                for (String line; (line = reader.readLine()) != null;) {
                    //System.out.println(line);
                    responseStr = responseStr + line;
                    Thread.sleep(2);
                }
            } finally {
                if (reader != null)
                    try {
                        reader.close();
                    } catch (IOException logOrIgnore) {
                    }
            }
        } else {
            // It's likely binary content, use InputStream/OutputStream.
            System.out.println("Binary content");
        }
        return responseStr;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}