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:fr.gcastel.freeboxV6GeekIncDownloader.services.GeekIncLogoDownloadService.java

private void saveAndResizeFile(File tmpFile, File outFile, int requiredSize)
        throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream(tmpFile);
    BufferedInputStream bfis = new BufferedInputStream(fis);

    // Recherche de la taille sans charger entirement l'image
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;/*from ww  w. j ava2s  .c o  m*/
    BitmapFactory.decodeStream(bfis, null, o);

    // Calcule la bonne mise  l'chelle en cherchant la puissance de 2 la
    // plus proche
    int scale = 1;
    while (o.outWidth / scale / 2 >= requiredSize && o.outHeight / scale / 2 >= requiredSize)
        scale *= 2;

    // Mise  l'chelle !
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;

    bfis.close();
    fis.close();

    fis = new FileInputStream(tmpFile);
    bfis = new BufferedInputStream(fis);
    Bitmap bmp = BitmapFactory.decodeStream(bfis, null, o2);
    bfis.close();

    FileOutputStream fout = new FileOutputStream(outFile);
    BufferedOutputStream bout = new BufferedOutputStream(fout);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, bout);
    bout.close();
    fout.close();
}

From source file:org.canova.api.records.reader.impl.SVMRecordWriterTest.java

@Test
public void testWriter() throws Exception {
    InputStream is = new ClassPathResource("iris.dat").getInputStream();
    assumeNotNull(is);/*  ww w .j  ava 2s.c  o  m*/
    File tmp = new File("iris.txt");
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmp));
    IOUtils.copy(is, bos);
    bos.flush();
    bos.close();
    InputSplit split = new FileSplit(tmp);
    tmp.deleteOnExit();
    RecordReader reader = new CSVRecordReader();
    List<Collection<Writable>> records = new ArrayList<>();
    reader.initialize(split);
    while (reader.hasNext()) {
        Collection<Writable> record = reader.next();
        assertEquals(5, record.size());
        records.add(record);
    }

    assertEquals(150, records.size());
    File out = new File("iris_out.txt");
    out.deleteOnExit();
    RecordWriter writer = new SVMLightRecordWriter(out, true);
    for (Collection<Writable> record : records)
        writer.write(record);

    writer.close();
    records.clear();

    RecordReader svmReader = new SVMLightRecordReader();
    InputSplit svmSplit = new FileSplit(out);
    svmReader.initialize(svmSplit);
    assertTrue(svmReader.hasNext());
    while (svmReader.hasNext()) {
        Collection<Writable> record = svmReader.next();
        assertEquals(5, record.size());
        records.add(record);
    }
    assertEquals(150, records.size());
}

From source file:de.thb.ue.backend.service.AnswerImageService.java

@Override
public void addAnswerImage(Vote vote, MultipartFile answerImage, String evaluationID) {
    File imageFolder = new File(
            (workingDirectoryPath.isEmpty() ? "" : (workingDirectoryPath + File.separatorChar)) + evaluationID
                    + File.separatorChar + "images");
    try {//from www .jav a 2 s .c  o  m
        if (!imageFolder.exists()) {
            FileUtils.forceMkdir(imageFolder);
        }
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(imageFolder, vote.getId() + ".zip")));
        stream.write(answerImage.getBytes());
        stream.close();
    } catch (IOException e) {
        //TODO
        e.printStackTrace();
    }
}

From source file:org.cloudfoundry.client.lib.rest.UploadApplicationPayloadHttpMessageConverter.java

private void writeApplicationZipToFile(InputStream inputStream) {
    // for testing/debugging purposes, write the zip file being uploaded to a path specified
    // in the following environment variable
    String uploadFilePath = System.getenv("CF_APP_UPLOAD_FILE");
    if (uploadFilePath != null) {
        try {/*ww  w  .j  a  v  a  2  s.c  o  m*/
            File outputFile = new File(uploadFilePath);
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
            FileCopyUtils.copy(inputStream, outputStream);
            outputStream.close();
        } catch (IOException e) {
            System.err.println("Error writing application upload to file: " + e);
        }
    }
}

From source file:com.camel.action.base.MerchantAction.java

public void handleFileUpload(FileUploadEvent event) {
    try {//  w w  w .j  a va2  s .  c o m
        String fileName = event.getFile().getFileName();
        UploadedFile source = event.getFile();

        String folderPath = "/opt/merchant/" + Helper.getCurrentUserMerchant().getId();
        File folder = new File(folderPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String filePath = folderPath + "/" + fileName;
        byte[] bytes = null;
        if (null != source) {
            bytes = source.getContents();

            File newFile = new File(filePath);
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile));
            stream.write(bytes);
            stream.close();
            System.out.println("FilenameUtils.getExtension(filePath).toUpperCase()..:"
                    + FilenameUtils.getExtension(filePath).toUpperCase());
            System.out.println("source.getContentType()...:" + source.getContentType());

            MerchantFile mfile = new MerchantFile();
            mfile.setMerchantFileType(MerchantFileType.CUSTOMEROFFERTEMPLATE);
            mfile.setMerchant(getInstance());
            mfile.setFileName(fileName);
            mfile.setFilePath(filePath);
            mfile.setFileType(FilenameUtils.getExtension(filePath).toUpperCase());
            mfile.setFileMimeType(source.getContentType());
            mfile.setStatus(Status.ACTIVE);
            getCrud().createObject(mfile);

            merchantFiles = new ArrayList<MerchantFile>();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
    FacesContext.getCurrentInstance().addMessage(null, message);
}

From source file:com.pax.pay.trans.receipt.paperless.AReceiptEmail.java

private File Convert(Bitmap bm, String fileName) throws IOException {
    String path = ContextUtils.getFilesDir() + "/temp/";
    File dirFile = new File(path);
    if (!dirFile.exists()) {
        dirFile.mkdir();//from   www.  jav  a 2  s. c om
    }
    File myCaptureFile = new File(path + fileName);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
    bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    bos.flush();
    bos.close();
    return myCaptureFile;
}

From source file:net.dfs.remote.filestorage.impl.StorageManagerImpl.java

/**
 * fileStorage will be responsible in storing the File object in the local
 * storage device. A notification indicating the name of the File object and the 
 * remote machine's address will be sent to the {@link FileLocationTracker}. 
 * <p>//  w w  w.jav  a 2 s  . c om
 * An OutPutStream of the FileObject will be saved in the local storage device.
 * IOException will be thrown on a failure. It returns no value.
 * 
 * @param storeFile an object of the type {@link FileStorageModel}
 */
public void fileStorage(FileStorageModel storeFile) {

    try {

        String savePath = path + storeFile.fileName;

        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(savePath));
        outputStream.write(storeFile.bytes, 0, storeFile.bytesRead);
        outputStream.flush();
        outputStream.close();

        log.debug("File " + storeFile.fileName + " saved to the disk");

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.facerecog.rest.controller.RecognitionController.java

@RequestMapping(value = ApiUrls.URL_RECOG_UPLOAD_IMAGE, method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
    logger.info("File upload");

    if (!file.isEmpty()) {
        try {/* w w w.j  a va2 s  .  c om*/
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(file.getOriginalFilename())));
            stream.write(bytes);
            stream.close();
            return "File uploaded: " + file.getOriginalFilename();
        } catch (Exception e) {
            return "Failed to upload image!";
        }
    } else {
        return "Failed to upload file because the file was empty.";
    }
}

From source file:com.walmart.gatling.endpoint.v1.FileUploadController.java

@RequestMapping(method = RequestMethod.POST, value = "/upload")
public String handleFileUpload(@RequestParam("name") String name, @RequestParam("role") String role,
        @RequestParam("type") String type, @RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {

    if (!file.isEmpty()) {
        try {//from   w w w . jav a2  s.  c  o  m
            String path = tempFileDir + "/" + name;
            System.out.println(path);
            FileUtils.touch(new File(path));
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(path)));
            FileCopyUtils.copy(file.getInputStream(), stream);
            stream.close();
            Optional<String> trackingId = serverRepository.uploadFile(path, name, role, type);
            redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + name + "! ");

            redirectAttributes.addFlashAttribute("link", "/#/file/" + trackingId.get());
        } catch (Exception e) {
            redirectAttributes.addFlashAttribute("message",
                    "You failed to upload " + name + " => " + e.getMessage());
        }
    } else {
        redirectAttributes.addFlashAttribute("message",
                "You failed to upload " + name + " because the file was empty");
    }

    return "redirect:upload";
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

/**
 * Creates a tarball from the source directory and writes it into the target directory.
 * @param srcDir - directory whose files will be added to the tarball
 * @param targetName - directory where tarball will be written to
 * @throws IOException when an exception occurs on creating the tarball
 *//*from  w  w w .j  a  va2  s  . com*/
public static void tarGzipDir(final String srcDir, final String targetName) throws IOException {

    FileOutputStream fileOutputStream = null;
    BufferedOutputStream bufferedOutputStream = null;
    GzipCompressorOutputStream gzipOutputStream = null;
    TarArchiveOutputStream tarArchiveOutputStream = null;

    try {
        forceMkdir(new File(getFullPath(targetName)));
        fileOutputStream = new FileOutputStream(new File(targetName));
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
        tarArchiveOutputStream = new TarArchiveOutputStream(gzipOutputStream);

        addFilesInDirectory(tarArchiveOutputStream, srcDir);
    } finally {
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.finish();
        }
        if (tarArchiveOutputStream != null) {
            tarArchiveOutputStream.close();
        }
        if (gzipOutputStream != null) {
            gzipOutputStream.close();
        }
        if (bufferedOutputStream != null) {
            bufferedOutputStream.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }
}