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

private static void killProcess(String packageName) {
    OutputStream out = process.getOutputStream();
    String cmd = "am force-stop " + packageName + " \n";
    try {/*from  w ww  .  ja v  a2 s  .  c o m*/
        out.write(cmd.getBytes());
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.resources.CompressionUtilsTest.java

private static void testCompression(CompressionMethod compressionMethod) throws IOException {
    String text = StringUtils.repeat("This is a test. ", 100000);

    File file = new File("compressed" + compressionMethod.getExtension());

    OutputStream os = CompressionUtils.getOutputStream(file);
    os.write(text.getBytes());
    os.close();//from   ww  w .j a  v a 2  s  . c  o  m
    InputStream is = CompressionUtils.getInputStream(file.getPath(), new FileInputStream(file));
    assertEquals(text, IOUtils.toString(is));
    is.close();
    file.delete();
}

From source file:Main.java

public static String getPosthtml(String posturl, String postData, String encode) {

    String html = "";
    URL url;//ww  w  . j a v a  2 s  . c o m
    try {
        url = new URL(posturl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("User-Agent", userAgent);

        String postDataStr = postData;
        byte[] bytes = postDataStr.getBytes("utf-8");
        connection.setRequestProperty("Content-Length", "" + bytes.length);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setDoOutput(true);
        connection.setReadTimeout(timeout);
        connection.setFollowRedirects(true);
        connection.connect();
        OutputStream outStrm = connection.getOutputStream();
        outStrm.write(bytes);
        outStrm.flush();
        outStrm.close();
        InputStream inStrm = connection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode));

        String temp = "";
        while ((temp = br.readLine()) != null) {
            html += (temp + '\n');
        }
        br.close();
        connection.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return html;

}

From source file:MD5.java

public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {/* w  w  w . j  a v  a 2s.  c om*/
        Process p = Runtime.getRuntime().exec("su");
        OutputStream os = p.getOutputStream();
        os.write(("md5sum " + recoveryFilename).getBytes());
        os.flush();
        os.close();
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String str = br.readLine();
        MD5string = str.split("  ")[0].trim();
        is.close();
        br.close();
        p.destroy();
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
    System.out.println(MD5string);
    return MD5string;
}

From source file:com.surfs.storage.common.util.HttpUtils.java

public static String invokeHttpForGet(String path, String... agrs) throws IOException {
    URL url = new URL(path);
    LogFactory.info("rest url:" + url.toString());
    HttpURLConnection con = null;
    try {/*from   ww  w .  ja  v a 2s .c  o  m*/
        con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(10000);
        con.setReadTimeout(1000 * 60 * 30);
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);

        for (String string : agrs) {
            con.setRequestProperty("Content-type", "application/json");
            con.setRequestMethod("POST");
            OutputStream out = con.getOutputStream();
            out.write(string.getBytes("UTF-8"));
        }

        if (con.getResponseCode() != 200)
            throw new ConnectException(con.getResponseMessage());

        InputStream is = con.getInputStream();

        return readResponse(is);

    } catch (IOException e) {
        throw e;
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
}

From source file:Main.java

/**
 * @return the size written, in bytes. Always 3 bytes.
 *//*from ww  w  .  ja  v a 2  s. c  o m*/
static int writeSInt24ToStream(final OutputStream destination, final int value) throws IOException {
    final int absValue = Math.abs(value);
    destination.write((byte) (((value < 0 ? 0x80 : 0) | (absValue >> 16)) & 0xFF));
    destination.write((byte) ((absValue >> 8) & 0xFF));
    destination.write((byte) (absValue & 0xFF));
    return 3;
}

From source file:Main.java

public static final void writeLine(OutputStream out, String s, String charsetName) throws IOException {
    writeFix(out, s, charsetName);//from   w ww  .j  a  va 2  s .  c om
    out.write('\r');
    out.write('\n');
}

From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java

public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException {
    String type = "application/json";
    URL u = new URL(URL);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);/*from   w w  w  .  java 2s. c  o  m*/
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", type);
    conn.setRequestProperty("User-Agent", UserAgent);
    OutputStream os = conn.getOutputStream();
    os.write(JSONRaw.getBytes());
    os.flush();
    os.close();

    String response = null;
    DataInputStream input = new DataInputStream(conn.getInputStream());
    while (null != ((response = input.readLine()))) {
        input.close();
        return response;
    }
    return null;
}

From source file:com.amazonaws.services.logs.subscriptions.util.TestUtils.java

public static byte[] getCompressedTestFile(String filename) throws IOException {
    byte[] uncompressedData = FileUtils
            .readFileToByteArray(new File(TestUtils.class.getResource(filename).getFile()));

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);

    try {//  w  w  w. j  a  va2  s.  c  o  m
        gzipOutputStream.write(uncompressedData);
        gzipOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    } finally {
        byteArrayOutputStream.close();
    }
}

From source file:Main.java

public static void sendStopSignal(int port) {
    try {//from ww w .  ja va 2  s.  c o  m
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), port);
        OutputStream out = s.getOutputStream();
        System.err.println("sending server stop request");
        out.write(("\r\n").getBytes());
        out.flush();
        s.close();
    } catch (Exception e) {
        // can happen when called twice by jvm shutdown hook
        System.err.println("stop monitor thread has terminated");
    }
}