Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:Main.java

public static String createExternalStoragePrivateFile(Context appCtx, InputStream is, String fileName) {

    File file = new File(appCtx.getExternalFilesDir(null), fileName);

    try {/*from   ww w . j a v  a 2  s.c o  m*/
        //InputStream is = appCtx.getResources().openRawResource(R.raw.calipso);
        OutputStream os = new FileOutputStream(file);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    } catch (IOException e) {
        // Unable to create file, likely because external storage is
        // not currently mounted.
        Log.w("ExternalStorage", "Error writing " + file, e);
    }

    return file.getPath();
}

From source file:Main.java

public static void copyAndClose(InputStream in, OutputStream out) throws IOException {
    byte[] cache = new byte[4096];
    int size;/*from w w  w.ja  v  a 2 s  . co m*/
    while ((size = in.read(cache)) > 0) {
        out.write(cache, 0, size);
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void writeStringAndClose(OutputStream destination, String data) throws IOException {
    try {//from   w w w  .  j a  v  a2  s  .c  o m
        writeString(destination, data, UTF8_ENCODING);
    } finally {
        destination.close();
    }
}

From source file:MD5.java

public static String getRecoveryMD5() {
    String MD5string = "";
    String recoveryFilename = "/dev/mtd/mtd1";
    try {/*from  w  w  w . java 2 s  .co  m*/
        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:Main.java

public static boolean bitmap2File(Bitmap bitmap, File imageFile) {
    OutputStream os;
    try {/* www.  j av  a 2  s. c om*/
        os = new FileOutputStream(imageFile);
        boolean isOK = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
        os.flush();
        os.close();
        return isOK;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void logXMLNode(Document doc, OutputStream output) {
    String nodeStr = retrieveDocumentAsString(doc);

    try {/*from   w  w  w .  j a v a  2s.  co  m*/
        output.write(nodeStr.getBytes());
        output.close();
    } catch (IOException ioe) {
        System.out.println("There happens some error when reading the xml fragment");
    }
}

From source file:Authentication.DinserverAuth.java

public static boolean Login(String nick, String pass, Socket socket) throws IOException {
    boolean logged = false;

    JSONObject authRequest = new JSONObject();
    authRequest.put("username", nick);
    authRequest.put("auth_id", pass);

    String query = "http://127.0.0.1:5000/token";

    URL url = new URL(query);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5000);//  w w w  .  ja  v  a2 s  .  com
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");

    OutputStream os = conn.getOutputStream();
    os.write(authRequest.toString().getBytes(Charset.forName("UTF-8")));
    os.close();
    String output = new String();
    StringBuilder sb = new StringBuilder();
    int HttpResult = conn.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK) {
        output = IOUtils.toString(new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8")));
    } else {
        System.out.println(conn.getResponseMessage());
    }

    JSONObject jsonObject = new JSONObject(output);
    logged = jsonObject.getBoolean("ok");

    conn.disconnect();

    if (logged) {
        if (DinserverMaster.addUser(nick, socket)) {
            System.out.println("User " + nick + " logged in");
        } else {
            logged = false;
        }
    }
    return logged;
}

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

/**
 * Compiles the graph to dotty.//from w  ww  .j  av a  2 s  . com
 */
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:com.codelanx.playtime.callable.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, String body) throws Exception {
    OutputStream stream = connection.getOutputStream();
    stream.write(body.getBytes());/*from  w ww  .  j a  v a2s.c om*/
    stream.flush();
    stream.close();
}

From source file:com.simiacryptus.util.io.IOUtil.java

/**
 * Write string./*from w w  w .  ja va  2  s.c om*/
 *
 * @param obj  the obj
 * @param file the file
 */
public static void writeString(String obj, OutputStream file) {
    try {
        IOUtils.write(obj.getBytes("UTF-8"), file);
        file.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}