Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap/*from  ww  w.  j a  v a  2  s. com*/
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:edu.wfu.inotado.WinProfileServiceImplTest.java

public void testGetRemotePhotoAsByteArray() throws IOException {
    byte[] image = super.winProfileService
            .getRemotePhotoAsByteArray("https://win.wfu.edu/images/dir/JGBFBGK.JPG");
    assertNotNull(image);//www  .  j  a v  a 2  s .c  o  m
    FileOutputStream output = new FileOutputStream(new File(outPutPhotoName));
    BufferedOutputStream bos = new BufferedOutputStream(output);
    bos.write(image);
    bos.close();

}

From source file:br.edu.unidavi.restapp.FileUploadController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {// www  .  j  av  a 2 s  .  co m
            String name = file.getOriginalFilename();
            System.out.println(name);
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded !";
        } catch (Exception e) {
            return "You failed to upload => " + e.getMessage();
        }
    } else {
        return "You failed to upload because the file was empty.";
    }
}

From source file:it.geosolutions.tools.compress.file.Extractor.java

public static void unZip(File inputZipFile, File outputDir) throws IOException, CompressorException {
    if (inputZipFile == null || outputDir == null) {
        throw new CompressorException("Unzip: with null parameters");
    }/*from  www.j a  v  a  2  s .c om*/

    if (!outputDir.exists()) {
        if (!outputDir.mkdirs()) {
            throw new CompressorException("Unzip: Unable to create directory structure: " + outputDir);
        }
    }

    final int BUFFER = Conf.getBufferSize();

    ZipInputStream zipInputStream = null;
    try {
        // Open Zip file for reading
        zipInputStream = new ZipInputStream(new FileInputStream(inputZipFile));
    } catch (FileNotFoundException fnf) {
        throw new CompressorException("Unzip: Unable to find the input zip file named: " + inputZipFile);
    }

    // extract file if not a directory
    BufferedInputStream bis = new BufferedInputStream(zipInputStream);
    // grab a zip file entry
    ZipEntry entry = null;
    while ((entry = zipInputStream.getNextEntry()) != null) {
        // Process each entry

        File currentFile = new File(outputDir, entry.getName());

        FileOutputStream fos = null;
        BufferedOutputStream dest = null;
        try {
            int currentByte;
            // establish buffer for writing file
            byte data[] = new byte[BUFFER];

            // write the current file to disk
            fos = new FileOutputStream(currentFile);
            dest = new BufferedOutputStream(fos, BUFFER);

            // read and write until last byte is encountered
            while ((currentByte = bis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
        } catch (IOException ioe) {

        } finally {
            try {
                if (dest != null) {
                    dest.flush();
                    dest.close();
                }
                if (fos != null)
                    fos.close();
            } catch (IOException ioe) {
                throw new CompressorException(
                        "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
            }
        }
    }
    try {
        if (zipInputStream != null)
            zipInputStream.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the zipInputStream: " + ioe.getLocalizedMessage());
    }
    try {
        if (bis != null)
            bis.close();
    } catch (IOException ioe) {
        throw new CompressorException(
                "Unzip: unable to close the Buffered zipInputStream: " + ioe.getLocalizedMessage());
    }
}

From source file:com.vtxii.smallstuff.etl.fileuploader.FileUploaderController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (false == file.isEmpty()) {
        try {//from w  w w .  ja  v a2 s .  c  om
            // See if the file exists. If it does then another user
            // has uploaded a file of the same name at the same time
            // and it has not yet been processed.
            File newFile = new File(name);
            if (true == newFile.exists()) {
                String msg = "fail: file \"" + name + "\" exists and hasn't been processed";
                logger.error(msg);
                return msg;
            }

            // Write the file (this should be to the landing directory)
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile));
            stream.write(bytes);
            stream.close();

            // Process the file
            LandingManager.process(newFile, processor, null);

            return "success: loaded " + name;
        } catch (Exception e) {
            String msg = "fail: file \"" + name + "\" with error " + e.getMessage();
            logger.error(msg);
            return msg;
        }
    } else {
        return "fail: file \"" + name + "\" empty";
    }
}

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

/**
 * Uncompress *.zip files./*from  ww  w. j a  v a  2s  .co m*/
 * @param fileName
 *        : file to be uncompress
 */
@SuppressWarnings("unchecked")
public static void decompress(String fileName) {
    File sourceFile = new File(fileName);
    String filePath = sourceFile.getParent() + "/";
    try {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        ZipFile zipFile = new ZipFile(fileName);
        Enumeration en = zipFile.entries();
        byte[] data = new byte[BUFFER];
        while (en.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) en.nextElement();
            if (entry.isDirectory()) {
                new File(filePath + entry.getName()).mkdirs();
                continue;
            }
            bis = new BufferedInputStream(zipFile.getInputStream(entry));
            File file = new File(filePath + entry.getName());
            File parent = file.getParentFile();
            if (parent != null && (!parent.exists())) {
                parent.mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(file));
            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                bos.write(data, 0, count);
            }
            bis.close();
            bos.close();
        }
        zipFile.close();
    } catch (IOException e) {
        //LOG.error("[compress]", e);
        throw new RuntimeException("[compress]", e);
    }
}

From source file:org.jfree.chart.demo.SuperDemo.java

public static void saveChartAsPDF(File file, JFreeChart jfreechart, int i, int j, FontMapper fontmapper)
        throws IOException {
    BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
    writeChartAsPDF(bufferedoutputstream, jfreechart, i, j, fontmapper);
    bufferedoutputstream.close();
}

From source file:com.turn.griffin.GriffinService.java

@RequestMapping(value = { "/localrepo", "/globalrepo" }, method = { RequestMethod.POST, RequestMethod.PUT })
public @ResponseBody String pushToRepo(@RequestParam(value = "blobname", required = true) String blobname,
        @RequestParam(value = "dest", required = true) String dest,
        @RequestParam(value = "file", required = true) MultipartFile file) {

    if (!StringUtils.isNotBlank(blobname)) {
        return "Blobname cannot be empty\n";
    }/*from w  ww  .j  a v  a2  s  .com*/

    if (!StringUtils.isNotBlank(dest)) {
        return "Dest cannot be empty\n";
    }

    try {
        File tempFile = File.createTempFile("gfn", null, null);
        byte[] bytes = file.getBytes();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(tempFile));
        stream.write(bytes);
        stream.close();
        module.get().syncBlob(blobname, dest, tempFile.getAbsolutePath());
        return String.format("Pushed file as blob %s to destination %s%n", blobname, dest);
    } catch (Exception e) {
        logger.error(String.format("POST request failed%n%s%n", ExceptionUtils.getStackTrace(e)));
        return String.format("Unable to push file as blob %s to destination %s%n", blobname, dest);
    } // TODO: Delete tempFile
}

From source file:Main.java

public static boolean copyAssetFile(Context context, String originFileName, String destFilePath,
        String destFileName) {/*from  ww  w .  j  ava  2s .co  m*/
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = context.getAssets().open(originFileName);
        File destPathFile = new File(destFilePath);
        if (!destPathFile.exists()) {
            destPathFile.mkdirs();
        }

        File destFile = new File(destFilePath + File.separator + destFileName);
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        bos = new BufferedOutputStream(fos);

        byte[] buffer = new byte[256];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            bos.write(buffer, 0, length);
        }
        bos.flush();

        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != bos) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return false;
}

From source file:com.web.mavenproject6.controller.DocumentUploadController.java

@ResponseBody
@RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST)
public Object uploadMultipleFileHandler(@RequestParam("file") MultipartFile[] files) {
    System.out.print("PATH IS A:" + env.getProperty("upload.files.dir"));
    String buf = "";
    List<String> l = new ArrayList<>();
    for (MultipartFile file : files) {
        try {/*www.  j  a v a 2  s.com*/
            byte[] bytes = file.getBytes();

            String rootPath = env.getProperty("upload.files.dir");

            File dir = new File(rootPath);
            if (!dir.exists()) {
                dir.mkdirs();
                dir.setWritable(true);
            }
            File serverFile = new File(rootPath + File.separator + file.getOriginalFilename());
            serverFile.createNewFile();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();
            System.err.println(file.getOriginalFilename());
            buf = XLSParser.parse(env.getProperty("upload.files.dir") + "\\" + file.getOriginalFilename());

            l.add(file.getOriginalFilename());
        } catch (Exception e) {

        }
    }
    // ModelAndView model = new ModelAndView("jsp/uploadedFiles");
    //model.addObject("list", l.toArray());
    // model.addObject("buffer",buf);
    //  return model;
    return buf;
}