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(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:pl.softech.gw.download.ResourceDownloader.java

public void download(String url, File dir, String fileName) throws IOException {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);

    HttpResponse response = httpclient.execute(httpGet);

    HttpEntity entity = null;//  w  w  w. j a  v a  2  s .com

    BufferedInputStream in = null;
    InputStream ins = null;
    BufferedOutputStream out = null;
    try {
        StatusLine statusLine = response.getStatusLine();
        entity = response.getEntity();

        if (statusLine.getStatusCode() >= 300) {
            EntityUtils.consume(entity);
            throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        ins = entity.getContent();
        long all = entity.getContentLength();
        in = new BufferedInputStream(ins);
        out = new BufferedOutputStream(new FileOutputStream(new File(dir, fileName)));

        byte[] buffer = new byte[1024];
        int cnt;
        while ((cnt = in.read(buffer)) >= 0) {
            out.write(buffer, 0, cnt);
            fireEvent(new BytesReceivedEvent(cnt, all, fileName));
        }

    } catch (IOException e) {
        fireEvent(new DownloadErrorEvent(String.format("Error during downloading file %s", fileName), e));
        throw e;
    } finally {

        if (ins != null) {
            ins.close();
        }

        if (in != null) {
            in.close();
        }

        if (out != null) {
            out.close();
        }

        httpGet.releaseConnection();
        httpclient.getConnectionManager().shutdown();

    }

}

From source file:com.vangent.hieos.services.xds.repository.transactions.ProvideAndRegisterDocumentSet.java

/**
 *
 * @param m//from  w  w w.  ja v  a  2  s. c  o m
 * @param doc
 * @param is
 * @throws com.vangent.hieos.xutil.exception.MetadataException
 * @throws com.vangent.hieos.xutil.exception.XdsIOException
 * @throws com.vangent.hieos.xutil.exception.XdsInternalException
 * @throws com.vangent.hieos.xutil.exception.XdsConfigurationException
 * @throws com.vangent.hieos.xutil.exception.XdsException
 */
private void store_document_swa_xop(Metadata m, XDSDocument doc, InputStream is) throws MetadataException,
        XdsIOException, XdsInternalException, XdsConfigurationException, XdsException {
    this.validateDocumentMetadata(doc, m); // Validate that all is present.

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);
    int length = 8192; // 8K chunks.
    byte[] buf = new byte[length];
    int size = 0;
    byte[] bytes = null;
    try {
        do {
            size = is.read(buf, 0, length);
            if (size > 0) {
                bos.write(buf, 0, size);
            }
        } while (size > 0);
        bos.flush();
        bytes = os.toByteArray();
    } catch (IOException e) {
        throw new XdsIOException("Error reading from input stream: " + e.getMessage());
    } finally {
        try {
            is.close(); // A bit of a side effect, but OK for now.
            os.close();
            bos.close();
        } catch (IOException e) {
            // Eat exceptions.
            logger.error("Problem closing a stream", e);
        }
    }

    // Set document vitals and store.
    this.setDocumentVitals(bytes, doc, m);
    this.storeDocument(doc);
}

From source file:com.sogrey.sinaweibo.utils.FileUtil.java

/**
 * ?/*from w  w w .j a v  a 2s  .  c  om*/
 * 
 * @author Sogrey
 * @date 2015630
 * @param sourceFile
 * @param targetFile
 */
public static void copyFile(File sourceFile, File targetFile) {

    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // ?
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // ?
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // ?
            outBuff.flush();
        } finally {
            // ?
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.sogrey.sinaweibo.utils.FileUtil.java

/**
 * ?//www.j  a v  a 2  s  .c om
 * 
 * @author Sogrey
 * @date 2015630
 * @param sourcePath
 * @param toPath
 */
public static void copyFile(String sourcePath, String toPath) {
    File sourceFile = new File(sourcePath);
    File targetFile = new File(toPath);
    createDipPath(toPath);
    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // ?
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // ?
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // ?
            outBuff.flush();
        } finally {
            // ?
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:gov.pnnl.goss.gridappsd.app.AppManagerImpl.java

private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);//from  w  w w  .  ja va2 s .  c  o  m
    }
    bos.close();
}

From source file:edu.ku.brc.ui.ImageDisplay.java

/**
 * @param urlStr//from w  ww.  ja  va  2  s  .c  o  m
 * @param tmpFile
 * @return
 */
private boolean fillFileFromWeb(final String urlStr, final File tmpFile) {
    if (bytes == null) {
        bytes = new byte[100 * 1024];
    }

    try {
        URL urlObj = new URL(urlStr);
        InputStream inpStream = urlObj.openStream();
        if (inpStream != null) {
            BufferedInputStream in = new BufferedInputStream(inpStream);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile));

            do {
                int numBytes = in.read(bytes);
                if (numBytes == -1) {
                    break;
                }
                bos.write(bytes, 0, numBytes);

            } while (true);
            in.close();
            bos.close();

            return true;
        }

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

    return false;
}

From source file:dpfmanager.shell.modules.report.core.ReportGenerator.java

/**
 * Extracts a zip entry (file entry).//from w  ww . jav a2 s  .  c  om
 *
 * @param zipIn the zip input stream
 * @param filePath the file path
 * @throws IOException exception
 */
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[4096];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

private String writeToFile(InputStream is, String filename) {
    Log.d("LDS", "new write to file");
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {/*w ww . ja  v a  2 s  .  c om*/
        in = new BufferedInputStream(is);
        out = new BufferedOutputStream(mContext.openFileOutput(filename, Context.MODE_PRIVATE));
        byte[] buffer = new byte[1024];
        int l;
        while ((l = in.read(buffer)) != -1) {
            out.write(buffer, 0, l);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        try {
            if (in != null)
                in.close();
            if (out != null) {
                Log.d("LDS", "new write to file to -> " + filename);
                out.flush();
                out.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return mContext.getFilesDir() + "/" + filename;
}

From source file:com.jsmartframework.web.manager.FilterControl.java

private void copyFileResource(InputStream is, String relativePath, ServletContext context) throws Exception {
    int count = 0;
    BufferedInputStream bis = new BufferedInputStream(is);
    String realFilePath = new File(context.getRealPath(PATH_SEPARATOR)).getPath() + PATH_SEPARATOR
            + relativePath;//w  w  w .java2 s.c  o  m

    FileOutputStream fos = new FileOutputStream(realFilePath);
    BufferedOutputStream bos = new BufferedOutputStream(fos, STREAM_BUFFER);

    byte data[] = new byte[STREAM_BUFFER];
    while ((count = bis.read(data, 0, STREAM_BUFFER)) != -1) {
        bos.write(data, 0, count);
    }
    bos.close();
    bis.close();
}

From source file:bammerbom.ultimatecore.bukkit.UltimateUpdater.java

/**
 * Part of Zip-File-Extractor, modified by Gravity for use with Updater.
 *
 * @param file the location of the file to extract.
 *///from w ww  .  ja  v a  2s  . c o  m
private void unzip(String file) {
    final File fSourceZip = new File(file);
    try {
        final String zipPath = file.substring(0, file.length() - 4);
        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration<? extends ZipEntry> e = zipFile.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());
            destinationFilePath.getParentFile().mkdirs();
            if (!entry.isDirectory()) {
                final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
                int b;
                final byte[] buffer = new byte[UltimateUpdater.BYTE_SIZE];
                final FileOutputStream fos = new FileOutputStream(destinationFilePath);
                final BufferedOutputStream bos = new BufferedOutputStream(fos, UltimateUpdater.BYTE_SIZE);
                while ((b = bis.read(buffer, 0, UltimateUpdater.BYTE_SIZE)) != -1) {
                    bos.write(buffer, 0, b);
                }
                bos.flush();
                bos.close();
                bis.close();
                final String name = destinationFilePath.getName();
                if (name.endsWith(".jar") && this.pluginExists(name)) {
                    File output = new File(updateFolder, name);
                    destinationFilePath.renameTo(output);
                }
            }
        }
        zipFile.close();

        // Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
        moveNewZipFiles(zipPath);

    } catch (final IOException e) {
    } finally {
        fSourceZip.delete();
    }
}