Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static String simplePost(String url, Bundle params, String method)
        throws MalformedURLException, IOException {
    OutputStream os;/*from  w w  w . j  a  va  2 s . c o  m*/

    System.setProperty("http.keepAlive", "false");
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setRequestProperty("User-Agent", System.getProperties().getProperty("http.agent") + " agent");

    conn.setRequestMethod(method);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    //conn.setRequestProperty("Connection", "Keep-Alive");

    conn.connect();

    os = new BufferedOutputStream(conn.getOutputStream());
    os.write(encodePostParams(params).getBytes());
    os.flush();

    String response = "";
    try {
        response = read(conn.getInputStream());
    } catch (FileNotFoundException e) {
        // Error Stream contains JSON that we can parse to a FB error
        response = read(conn.getErrorStream());
    }
    return response;
}

From source file:Main.java

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {//from  ww w  . java 2 s.co  m
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from   ww w . j  a  v a 2  s . c  o m*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:Main.java

public static void save(String filename, Document document) throws IOException {
    PrintStream out = null;/*from  w  w  w.java2 s  .  c  om*/
    try {
        out = new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)), true, "UTF-8");
        // traceNode(document, "");
        print(out, document);
    } catch (Exception ex) {
        throw new IOException(ex.getLocalizedMessage());
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
    }
}

From source file:Main.java

/**
 * This method serializes an object to an output stream.
 *
 * @param file The output file/*from   www . jav  a 2 s  . com*/
 * @param object The object to be serialized
 * @exception IOException IOError
        
 */
public static void serializeObject(File file, Object object) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    try {
        ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
        oos.writeObject(object);
        oos.flush();
    } finally {
        fos.close();
    }
}

From source file:Main.java

/**
 * Writes an object to an XML file./*from   w w  w .  j  av  a  2 s.  c  o  m*/
 * 
 * @param bean the bean
 * @param filename the filename
 * 
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void writeBeanToFile(Object bean, String filename) throws IOException {
    XMLEncoder encoder = null;
    try {
        encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
        encoder.writeObject(bean);
    } finally {
        if (encoder != null) {
            encoder.close();
        }
    }
}

From source file:Main.java

private static boolean copyDex(Context ctx, String dexAssertPath, String dexPath) {
    AssetManager assets = ctx.getAssets();
    InputStream inStream = null;//from  w  w  w  .j  a  va2s  . c om
    OutputStream dexWriter = null;
    boolean suc = true;
    try {
        inStream = assets.open(dexAssertPath);
        FileOutputStream outStream = new FileOutputStream(dexPath);
        dexWriter = new BufferedOutputStream(outStream);
        byte[] buf = new byte[1024 * 8];
        int len;
        while ((len = inStream.read(buf)) > 0) {
            dexWriter.write(buf, 0, len);
        }
        dexWriter.close();
        inStream.close();
    } catch (Exception e) {
        e.printStackTrace();
        suc = false;
    } finally {
        try {
            if (null != inStream) {
                inStream.close();
            }
            if (null != dexWriter) {
                dexWriter.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return suc;
}

From source file:com.pactera.edg.am.metamanager.extractor.util.AntZip.java

public static void zip(String sourceDir, String zipFile) {

    OutputStream os;/*from   w  ww .  ja va2 s . c  om*/

    try {

        os = new FileOutputStream(zipFile);

        BufferedOutputStream bos = new BufferedOutputStream(os);

        ZipOutputStream zos = new ZipOutputStream(bos);

        File file = new File(sourceDir);

        String basePath = null;

        if (file.isDirectory()) {

            basePath = file.getPath();

        } else {// ??

            basePath = file.getParent();

        }

        zipFile(file, basePath, zos);

        zos.closeEntry();

        zos.close();

    } catch (Exception e) {

        e.printStackTrace();

    }

}

From source file:com.sangupta.jerry.unsafe.UnsafeMemoryUtils.java

public static void writeToFile(UnsafeMemory memory, File file) throws IOException {
    FileOutputStream stream = null;
    BufferedOutputStream boss = null;
    try {//  w  w w  .j a  v a 2 s  . com
        final byte[] bytes = memory.getBuffer();
        final int length = memory.getPosition();

        stream = new FileOutputStream(file);
        boss = new BufferedOutputStream(stream);

        int CHUNK_SIZE = 1 << 20;
        int chunks = length / CHUNK_SIZE;
        int extra = length % CHUNK_SIZE;
        if (extra > 0) {
            chunks++;
        }

        for (int ki = 0; ki < chunks; ki++) {
            int delta = CHUNK_SIZE;
            if ((ki + 1) == chunks) {
                delta = extra;
            }
            boss.write(bytes, ki * CHUNK_SIZE, delta);
        }
    } catch (IOException e) {
        throw new IOException("Unable to write bytes to disk", e);
    } finally {
        IOUtils.closeQuietly(boss);
        IOUtils.closeQuietly(stream);
    }
}

From source file:Main.java

/**
 * author: liuxu// w  ww. jav  a2s  .  c  o m
 * save  bitmap into file
 * @param bitmap the bitmap
 * @param path full path of the file
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 * @return true if success
 */
public static boolean saveBitmap(Bitmap bitmap, String path, int quality) {
    if (bitmap == null) {
        return false;
    }
    File file = new File(path);
    File parent = file.getParentFile();
    if (parent != null && !parent.exists()) {
        if (!parent.mkdirs()) {
            //Log.e(TAG, "saveBitmap, mkdir for parent fail");
            return false;
        }
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
    } catch (IOException e) {
        //Log.d(TAG, "saveBitmap fail", e);
        return false;
    }
    return true;
}