Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:edu.ur.ir.ir_import.service.DefaultZipHelper.java

/**
 * Extracts the entry from the specified zip file.
 * /*from   w ww  .  ja v  a2s.  co  m*/
 * @param File f - file to write the data to
 * @param entry - entry to extract from the zip file
 * @param zip - zip file 
 * 
 */
public void getZipEntry(File f, ZipArchiveEntry entry, ZipFile zip) {
    InputStream content = null;
    OutputStream outStream = null;
    OutputStream out = null;

    try {
        content = zip.getInputStream(entry);
        byte[] fileBytes = new byte[1024];

        outStream = new FileOutputStream(f);
        out = new BufferedOutputStream(outStream);
        int amountRead = 0;
        while ((amountRead = content.read(fileBytes)) != -1) {
            out.write(fileBytes, 0, amountRead);
        }
        out.flush();
    } catch (Exception e) {
        log.error(e);
        errorEmailService.sendError(e);
    } finally {
        if (content != null) {
            try {
                content.close();
            } catch (IOException e) {
                log.error(e);
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                log.error(e);
            }
        }

        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }

}

From source file:gobblin.tunnel.TalkPastServer.java

@Override
void handleClientSocket(Socket clientSocket) throws IOException {
    LOG.info("Writing to client");
    try {//from   w  w  w. j a  va2  s . c  om
        final BufferedOutputStream serverOut = new BufferedOutputStream(clientSocket.getOutputStream());
        EasyThread clientWriterThread = new EasyThread() {
            @Override
            void runQuietly() throws Exception {
                long t = System.currentTimeMillis();
                try {
                    for (int i = 0; i < nMsgs; i++) {
                        serverOut.write(generateMsgFromServer(i).getBytes());
                        sleepQuietly(2);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                LOG.info("Server done writing in " + (System.currentTimeMillis() - t) + " ms");
            }
        }.startThread();
        _threads.add(clientWriterThread);

        BufferedReader serverIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String line = serverIn.readLine();
        while (line != null && !line.equals("Goodbye")) {
            String[] tokens = line.split(":", 2);
            String client = tokens[0];
            digestMsgsRecvdAtServer.get(client).update(line.getBytes());
            digestMsgsRecvdAtServer.get(client).update("\n".getBytes());
            line = serverIn.readLine();
        }
        LOG.info("Server done reading");
        try {
            clientWriterThread.join();
        } catch (InterruptedException e) {
        }
        serverOut.write("Goodbye\n".getBytes());
        serverOut.flush();
        clientSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
        throw e;
    }
}

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

public static byte[] gzip(byte[] bytes) {
    try {//  w  ww .j  av a2  s.c om
        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);
    }
}

From source file:ddf.camel.component.catalog.content.FileSystemDataAccessObject.java

public void store(String storePath, String suffix, String key, Object value) {
    OutputStream file = null;//w w  w. j  a  va 2s. c  om
    ObjectOutputStream output = null;
    try {
        File dir = new File(storePath);
        if (!dir.exists()) {
            if (!dir.mkdir()) {
                LOGGER.debug("Unable to create directory: {}", dir.getAbsolutePath());
            }
        }
        file = new FileOutputStream(storePath + key + suffix);
        OutputStream buffer = new BufferedOutputStream(file);
        output = new ObjectOutputStream(buffer);
        output.writeObject(value);
    } catch (IOException e) {
        LOGGER.debug("IOException storing value in cache with key = " + key, e);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(file);
    }
}

From source file:com.linkedin.pinot.common.utils.TarGzCompressionUtils.java

/**
 * Creates a tar.gz file at the specified path with the contents of the
 * specified directory./*from  w  w w  . jav  a  2s .co m*/
 *
 * @param directoryPath
 *          The path to the directory to create an archive of
 * @param tarGzPath
 *          The path to the archive to create
 * @return tarGzPath
 * @throws IOException
 *           If anything goes wrong
 */
public static String createTarGzOfDirectory(String directoryPath, String tarGzPath) throws IOException {
    FileOutputStream fOut = null;
    BufferedOutputStream bOut = null;
    GzipCompressorOutputStream gzOut = null;
    TarArchiveOutputStream tOut = null;
    if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
        tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
    }

    try {
        fOut = new FileOutputStream(new File(tarGzPath));
        bOut = new BufferedOutputStream(fOut);
        gzOut = new GzipCompressorOutputStream(bOut);
        tOut = new TarArchiveOutputStream(gzOut);
        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
        addFileToTarGz(tOut, directoryPath, "");
    } finally {
        tOut.finish();

        tOut.close();
        gzOut.close();
        bOut.close();
        fOut.close();
    }
    return tarGzPath;
}

From source file:biz.paluch.maven.configurator.TemplateProcessor.java

public void processFile(File input, File output) throws IOException {

    Closer closer = Closer.create();/*from  w w w  .ja va2  s. c  om*/
    try {

        InputStream fis = closer.register(new FileInputStream(input));
        OutputStream fos = closer.register(new BufferedOutputStream(new FileOutputStream(output)));

        log.debug("Processing file " + input);
        log.debug("Output file " + output);

        processStream(fis, fos);

        log.debug("Processing file " + input + " done");

    } finally {
        closer.close();
    }
}

From source file:com.mycollab.module.file.service.impl.FileRawContentServiceImpl.java

@Override
public void saveContent(String objectPath, InputStream stream) {
    int startFileNameIndex = objectPath.lastIndexOf("/");
    if (startFileNameIndex > 0) {
        /*//w  w w.java 2s  . co m
         * make sure the directory exist
        */
        String folderPath = objectPath.substring(0, startFileNameIndex);
        File file = new File(baseFolder, folderPath);
        if (!file.exists() && !file.mkdirs()) {
            throw new MyCollabException("Create directory failed");
        }
    }

    try (BufferedOutputStream outStream = new BufferedOutputStream(
            new FileOutputStream(new File(baseFolder, objectPath)))) {
        byte[] buffer = new byte[BUFFER_SIZE];
        int byteRead;

        while ((byteRead = stream.read(buffer)) >= 0) {
            outStream.write(buffer, 0, byteRead);
        }
    } catch (IOException e) {
        throw new MyCollabException(e);
    }

}

From source file:com.honnix.cheater.bundle.CheaterActivator.java

private void setTrustStore(BundleContext context) throws IOException {
    InputStream is = context.getBundle().getEntry("/etc/jssecacerts").openStream();
    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(TEMP_TRUST_STORE));

    byte[] buffer = new byte[255];
    int length;//www .  j  a  v a 2s. co  m

    while ((length = is.read(buffer)) != -1) {
        os.write(buffer, 0, length);
    }

    os.flush();
    os.close();
    is.close();

    System.setProperty(CheaterConstant.TRUST_STORE_KEY, TEMP_TRUST_STORE);
}

From source file:ftpserver.Data.java

public void sendFile(String file) throws Exception {
    isStop = false;//from   w  w w  .  j  a  v a  2s  .c o m
    if (socket == null)
        throw new IOException("No client connected");
    String input = "";
    int i = 0;
    FileInputStream fin = null;
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(socket.getOutputStream());
        fin = new FileInputStream(file);
        /*
        if(data.type == 'A') {
           PrintWriter rout=new PrintWriter(out);
           BufferedReader br = new BufferedReader(new InputStreamReader(fin));
           while(true) {
              input = br.readLine();
              if(input==null)
          break;
              rout.
           }
           rout.flush();
           //rout.close();
        } else {
                   
        }*/
        while (true) {
            i = fin.read();
            if (i == -1 || isStop == true) //if aborted
                break;
            out.write(i);
        }
        out.flush();
    } catch (Exception e) {
        throw e;
    } finally {
        if (fin != null)
            fin.close();
    }
}

From source file:com.drive.student.xutils.http.callback.FileDownloadHandler.java

public File handleEntity(HttpEntity entity,
        com.drive.student.xutils.http.callback.RequestCallBackHandler callBackHandler, String target,
        boolean isResume, String responseFileName) throws IOException {
    if (entity == null || TextUtils.isEmpty(target)) {
        return null;
    }//from   w ww.  ja  va2s  . c om

    File targetFile = new File(target);

    if (!targetFile.exists()) {
        File dir = targetFile.getParentFile();
        if (dir.exists() || dir.mkdirs()) {
            targetFile.createNewFile();
        }
    }

    long current = 0;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        FileOutputStream fileOutputStream = null;
        if (isResume) {
            current = targetFile.length();
            fileOutputStream = new FileOutputStream(target, true);
        } else {
            fileOutputStream = new FileOutputStream(target);
        }
        long total = entity.getContentLength() + current;
        bis = new BufferedInputStream(entity.getContent());
        bos = new BufferedOutputStream(fileOutputStream);

        if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) {
            return targetFile;
        }

        byte[] tmp = new byte[4096];
        int len;
        while ((len = bis.read(tmp)) != -1) {
            bos.write(tmp, 0, len);
            current += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(total, current, false)) {
                    return targetFile;
                }
            }
        }
        bos.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(total, current, true);
        }
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(bos);
    }

    if (targetFile.exists() && !TextUtils.isEmpty(responseFileName)) {
        File newFile = new File(targetFile.getParent(), responseFileName);
        while (newFile.exists()) {
            newFile = new File(targetFile.getParent(), System.currentTimeMillis() + responseFileName);
        }
        return targetFile.renameTo(newFile) ? newFile : targetFile;
    } else {
        return targetFile;
    }
}