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:hr.softwarecity.osijek.utility.Multimedia.java

/**
 * Method for uploading files//  w ww  .  j  a  v  a2  s  . co m
 * @param path path to put file
 * @param file file to upload
 * @return path to file or Failed keyword on fail
 * @throws java.nio.file.FileSystemException
 * @throws java.io.FileNotFoundException
 */
public static String handleFileUpload(String path, MultipartFile file) throws IOException {
    Logger.getLogger("Multimedia.java").log(Logger.Level.INFO, "Initiating file upload");
    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File(path, file.getOriginalFilename())));
        stream.write(bytes);
        stream.close();
        Logger.getLogger("Multimedia.java").log(Logger.Level.INFO, "File uploaded");
        return path;
    } else {
        Logger.getLogger("Multimedia.java").log(Logger.Level.ERROR, "File size 0! ");
        return null;
    }
}

From source file:librarymanagementsystem.Base64Converter.java

/**
 * This method writes byte array content into a file.
 *///from  ww  w.  j  av a  2 s .co  m
public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {

    File file = new File(fileName);
    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
    writer.write(content);
    writer.flush();
    writer.close();

}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();//from   w ww .j a v  a2  s.  c o  m
        }
        file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//from w  w  w.ja  v a  2 s.c o  m

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

From source file:Main.java

private static void saveBitmap(Bitmap bitmap, String fileName) {
    File myCaptureFile = new File(fileName);
    BufferedOutputStream bos;
    try {/* ww  w  .  jav a  2 s . co m*/
        bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveFile(Bitmap bm, String path) {
    if (bm == null || path == null)
        return false;
    File myCaptureFile = new File(path);
    if (myCaptureFile.exists()) {
        myCaptureFile.delete();//from  www  . j  a  va  2 s .  c o m
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * author: liuxu/*w w  w . j  av  a  2  s .  co  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;
}

From source file:com.cloudhopper.commons.io.demo.FileServerMain.java

private static void saveFileFromUrl(URL url, String path) throws Exception {
    URLConnection urlc = url.openConnection();
    BufferedInputStream bis = new BufferedInputStream(urlc.getInputStream());
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int i;//from  ww w.j a va2  s. co  m
    while ((i = bis.read()) != -1) {
        bos.write(i);
    }
    bis.close();
    bos.close();
}

From source file:Main.java

public static boolean saveBitmapToSDCard(Bitmap bitmap, String filePath, String fileName) {
    boolean flag = false;
    if (null != bitmap) {
        try {/*from w  w w  .j a v a  2s .  co m*/
            fileName = fileName + ".jpg";
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            File f = new File(filePath + fileName);
            if (f.exists()) {
                f.delete();
            }
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(f));
            bitmap.compress(CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
            flag = true;
        } catch (FileNotFoundException e) {
            flag = false;
        } catch (IOException e) {
            flag = false;
        }
    }
    return flag;

}

From source file:com.useekm.types.AbstractGeo.java

public static byte[] gzip(byte[] bytes) {
    try {// w w  w.ja  va  2  s  . co m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
        bufos.write(bytes);
        bufos.close();
        byte[] retval = bos.toByteArray();
        bos.close();
        return retval;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gzip", e);
    }
}