Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte to this buffered output stream.

Usage

From source file:com.almarsoft.GroundhogReader.lib.FSUtils.java

public static long writeByteArrayToDiskFileAndGetSize(byte[] data, String directory, String fileName)
        throws IOException {

    File outDir = new File(directory);
    if (!outDir.exists())
        outDir.mkdirs();//from   w ww . j a  va2 s .  com

    File outFile = new File(directory, fileName);

    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(outFile));

    try {
        fos.write(data);
    } finally {
        fos.close();
    }

    return outFile.length();
}

From source file:com.kcs.core.utilities.FtpUtil.java

public static boolean getFileByStream(String hostname, int port, String username, String password,
        String remoteFileName, String localFileName) throws Exception {
    FTPClient ftp = null;/*from   w  w w.  j a v  a  2s. c om*/
    boolean result = false;
    try {
        ftp = FtpUtil.openFtpConnect(hostname, port, username, password);
        if (null != ftp && ftp.isConnected()) {
            System.out.println("File has been start downloaded..............");
            System.out.println("downloading file " + remoteFileName + " to " + localFileName + ".");
            InputStream inputStream = ftp.retrieveFileStream(remoteFileName);
            if (null == inputStream) {
                throw new Exception("File not found in server.");
            } else {
                File localFile = new File(localFileName);
                BufferedOutputStream bOut = new BufferedOutputStream(new FileOutputStream(localFile));
                int b;
                long count = 0;
                while ((b = inputStream.read()) > -1) {
                    bOut.write(b);
                    count++;
                }
                bOut.close();
                inputStream.close();
                System.out.println("download file " + remoteFileName + "(" + count + ")bytes to "
                        + localFileName + " done.");
                if (count > 0) {
                    result = true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error : File not found in server.");
    } finally {
        FtpUtil.closeFtpServer(ftp);
    }
    return result;
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static File unzip(byte[] zipData, File directory) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
    ZipInputStream zis = new ZipInputStream(bais);
    ZipEntry entry = zis.getNextEntry();
    File root = null;/*w w  w .j  a  va 2s. co  m*/

    while (entry != null) {
        if (entry.isDirectory()) {
            File f = new File(directory, entry.getName());
            f.mkdir();

            if (root == null) {
                root = f;
            }
        } else {
            BufferedOutputStream out;
            out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())),
                    BUFFER_SIZE);

            // ZipInputStream can only give us one byte at a time...
            for (int data = zis.read(); data != -1; data = zis.read()) {
                out.write(data);
            }
            out.close();
        }

        zis.closeEntry();
        entry = zis.getNextEntry();
    }
    zis.close();

    return root;
}

From source file:Main.java

public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {// w ww .  j  av  a 2 s .  c om
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

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;/*from   ww w.j a  va  2 s .  com*/
    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;
}

From source file:ebay.dts.client.FileTransferActions.java

private static String compressFileToGzip(String inFilename) throws EbayConnectorException {

    // compress the xml file into gz file in the save folder
    String outFilename = null;//ww  w  .j a  va2  s . c o  m
    String usingPath = inFilename.substring(0, inFilename.lastIndexOf(File.separator) + 1);
    String fileName = inFilename.substring(inFilename.lastIndexOf(File.separator) + 1);
    outFilename = usingPath + fileName + ".gz";

    try {

        BufferedReader in = new BufferedReader(new FileReader(inFilename));
        BufferedOutputStream out = new BufferedOutputStream(
                new GZIPOutputStream(new FileOutputStream(outFilename)));
        logger.trace("Writing gz file to: " + Utility.getFileLabel(outFilename));

        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.close();

    } catch (FileNotFoundException e) {
        logger.error("Cannot gzip file: " + inFilename);
        throw new EbayConnectorException("Cannot gzip file: " + inFilename, e);
    } catch (IOException e) {
        logger.error("Cannot gzip file: " + inFilename);
        throw new EbayConnectorException("Cannot gzip file: " + inFilename, e);
    }

    logger.info("The compressed file has been saved to " + outFilename);
    return outFilename;
}

From source file:Main.java

/**
 * Base64 to File//www .j  a  va2 s .  c  o  m
 * @param base64Str
 * @param filePath
 * @param fileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveBase64StringToFile(String base64Str, String filePath, String fileName)
        throws FileNotFoundException, IOException {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    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);
        byte[] bfile = Base64.decode(base64Str, Base64.DEFAULT);
        bos.write(bfile);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java

/**
 * <p><em>Title: Create a File from a Base64 encoded String</em></p>
 * <p>Description: Method creates a file from the bytestream 
 * representing the original PDF, that should be converted</p>
 * /* w  w w  . jav a  2 s.com*/
 * @param stream <code>String</code> 
 * @return <code>String</code> Filename of newly created temporary File
 */
public static String saveBase64ByteStringToFile(File outputFile, String stream) {
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    try {
        fos = new FileOutputStream(outputFile);
        bos = new BufferedOutputStream(fos);
        bos.write(Base64.decodeBase64(stream.getBytes("UTF-8")));
        bos.flush();
        bos.close();

    } catch (IOException ioExc) {
        log.error(ioExc);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioExc) {
                log.error(ioExc);
            }
        }
    }
    return outputFile.getName();
}

From source file:Main.java

public static boolean bitmapToOutPutStream(final Bitmap bitmap, OutputStream outputStream) {

    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*from   w  ww .  j ava 2  s.com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

        in = new BufferedInputStream(inputStream, 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);

        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:oneDrive.OneDriveAPI.java

public static void uploadFile(String filePath) {
    URLConnection urlconnection = null;
    try {//  w  ww  . j  a v a  2 s .  co m
        File file = new File(UPLOAD_PATH + filePath);
        URL url = new URL(DRIVE_PATH + file.getName() + ":/content");
        urlconnection = url.openConnection();
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            try {
                ((HttpURLConnection) urlconnection).setRequestMethod("PUT");
                ((HttpURLConnection) urlconnection).setRequestProperty("Content-type",
                        "application/octet-stream");
                ((HttpURLConnection) urlconnection).addRequestProperty("Authorization",
                        "Bearer " + ACCESS_TOKEN);
                ((HttpURLConnection) urlconnection).connect();

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

        BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) >= 0) {
            bos.write(i);
        }
        bos.flush();
        bos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}