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:com.milaboratory.util.CompressionType.java

private static OutputStream createOutputStream(CompressionType ct, OutputStream os) throws IOException {
    switch (ct) {
    case None:/*from   w ww.  j  av a  2 s . co m*/
        return os;
    case GZIP:
        return new GZIPOutputStream(os, 2048);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorOutputStream(CompressorStreamFactory.BZIP2,
                    new BufferedOutputStream(os));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:com.gmt2001.HttpRequest.java

public static HttpResponse getData(RequestType type, String url, String post, HashMap<String, String> headers) {
    Thread.setDefaultUncaughtExceptionHandler(com.gmt2001.UncaughtExceptionHandler.instance());

    HttpResponse r = new HttpResponse();

    r.type = type;/*from w  ww  .  java  2s.c o  m*/
    r.url = url;
    r.post = post;
    r.headers = headers;

    try {
        URL u = new URL(url);

        HttpURLConnection h = (HttpURLConnection) u.openConnection();

        for (Entry<String, String> e : headers.entrySet()) {
            h.addRequestProperty(e.getKey(), e.getValue());
        }

        h.setRequestMethod(type.name());
        h.setUseCaches(false);
        h.setDefaultUseCaches(false);
        h.setConnectTimeout(timeout);
        h.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36 PhantomBotJ/2015");
        if (!post.isEmpty()) {
            h.setDoOutput(true);
        }

        h.connect();

        if (!post.isEmpty()) {
            BufferedOutputStream stream = new BufferedOutputStream(h.getOutputStream());
            stream.write(post.getBytes());
            stream.flush();
            stream.close();
        }

        if (h.getResponseCode() < 400) {
            r.content = IOUtils.toString(new BufferedInputStream(h.getInputStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = true;
        } else {
            r.content = IOUtils.toString(new BufferedInputStream(h.getErrorStream()), h.getContentEncoding());
            r.httpCode = h.getResponseCode();
            r.success = false;
        }
    } catch (IOException ex) {
        r.success = false;
        r.httpCode = 0;
        r.exception = ex.getMessage();

        com.gmt2001.Console.err.printStackTrace(ex);
    }

    return r;
}

From source file:com.web.controller.ToolController.java

@ResponseBody
@RequestMapping(value = "/tool/verifyapk", method = RequestMethod.POST)
public String verifyApk(@RequestParam("apkfile") MultipartFile file) {

    //keytool -list -printcert -jarfile d:\weixin653android980.apk
    //keytool -printcert -file D:\testapp\META-INF\CERT.RSA
    //System.out.println("12345");
    try {//  w  w w . java  2s  .  c  om
        OutputStream stream = new FileOutputStream(new File(file.getOriginalFilename()));
        BufferedOutputStream outputStream = new BufferedOutputStream(stream);
        outputStream.write(file.getBytes());
        outputStream.flush();
        outputStream.close();

        Runtime runtime = Runtime.getRuntime();
        String ccString = "keytool -list -printcert -jarfile C:\\Users\\Administrator\\Desktop\\zju1.1.8.2.apk";
        Process p = runtime.exec(ccString);

        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

        StringBuilder sb = new StringBuilder();
        while (br.readLine() != null) {
            sb.append(br.readLine() + "<br/>");
        }
        p.destroy();
        p = null;
        return sb.toString();
    } catch (FileNotFoundException fe) {
        return fe.getMessage();
    } catch (IOException ioe) {
        return ioe.getMessage();
    }

}

From source file:edu.vt.middleware.crypt.io.HexFilterOutputStreamTest.java

/** @throws  Exception  On test failure. */
@Test(groups = { "functest", "io", "encodeHex" })
public void testEncodeHex() throws Exception {
    logger.info("Writing encoded hex file");

    final String outPath = "target/test-output/encoded-hex.txt";
    new File(outPath).getParentFile().mkdir();

    final InputStream in = getClass().getResourceAsStream(TEXT_FILE_PATH);
    final OutputStream out = new HexFilterOutputStream(
            new BufferedOutputStream(new FileOutputStream(new File(outPath))));
    try {/*from w  ww. j  a  v a  2 s . co m*/
        int count = 0;
        final int bufsize = 2048;
        final byte[] buffer = new byte[bufsize];
        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

    final InputStream inRef = getClass().getResourceAsStream("/edu/vt/middleware/crypt/io/hex.txt");
    final InputStream inTest = new FileInputStream(new File(outPath));
    try {
        AssertJUnit.assertTrue(FileHelper.equal(inRef, inTest));
    } finally {
        if (inRef != null) {
            inRef.close();
        }
        if (inTest != null) {
            inTest.close();
        }
    }
}

From source file:com.github.khandroid.http.misc.FileDownloader.java

public static void download(HttpClient httpClient, URI source, File destination)
        throws ClientProtocolException, IOException {
    byte[] content = download(httpClient, source);
    ByteArrayInputStream input = new ByteArrayInputStream(content);
    BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(destination));

    IOUtils.copy(input, output);/*from   w ww.  j a va 2  s.co  m*/
    input.close();
    output.close();
}

From source file:com.littcore.io.util.ZipUtils.java

public static void zip(File srcFileOrPath, File targetFileNamePath) throws IOException {
    //?//ww w  . j av  a 2 s.  co m
    CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32());
    //zip?
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs));
    out.setEncoding("GBK");
    zip(out, srcFileOrPath, "");
    out.close();
}

From source file:at.asitplus.regkassen.core.base.util.CashBoxUtils.java

/**
 * Helper method for storing printed PDF receipts to files
 *
 * @param printedReceipts binary representation of receipts to be stored
 * @param prefix          prefix for file names
 * @param baseDir         base directory, where files should be written
 *///from   www.ja v  a2s  . c om
public static void writeReceiptsToFiles(List<byte[]> printedReceipts, String prefix, File baseDir) {
    try {
        int index = 1;
        for (byte[] printedReceipt : printedReceipts) {
            ByteArrayInputStream bIn = new ByteArrayInputStream(printedReceipt);
            File receiptFile = new File(baseDir, prefix + "Receipt " + index + ".pdf");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    new FileOutputStream(receiptFile));
            IOUtils.copy(bIn, bufferedOutputStream);
            bufferedOutputStream.close();
            index++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:fr.insalyon.creatis.vip.applicationimporter.server.business.TargzUtils.java

public static void createTargz(List<File> pathIn, String pathOut) throws BusinessException {
    try {/*from   w w w .  j  a va 2  s  .  c  om*/

        FileOutputStream fos = new FileOutputStream(pathOut);
        TarArchiveOutputStream tos = new TarArchiveOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(fos)));
        for (File entry : pathIn) {
            addFileToTarGz(tos, entry, null);
        }
        tos.finish();
        tos.close();
    } catch (IOException ex) {
        throw new BusinessException(ex);
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip.//w ww  . j  a v  a2s.  c  om
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}

From source file:me.daququ.common.core.utils.FileUtils.java

public static boolean byte2File(byte[] buff, String filePath, String fileName) throws Exception {
    boolean retFlag = true;
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;//  www.ja  v a2 s.  c o  m
    File file = null;
    try {
        File dir = new File(filePath);
        if (!(dir.exists() && dir.isDirectory())) {
            dir.mkdirs();
        }
        file = new File(filePath + fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        bos.write(buff);
    } catch (Exception e) {
        throw new Exception(e.getMessage(), e);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                throw new Exception(e.getMessage(), e);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                throw new Exception(e.getMessage(), e);
            }
        }
    }
    return retFlag;
}