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, int size) 

Source Link

Document

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

Usage

From source file:action.ImageResult.java

public void execute(ActionInvocation invocation) throws IOException {
    ImageAction action = (ImageAction) invocation.getAction();
    HttpServletResponse response = ServletActionContext.getResponse();
    ServletOutputStream sos = response.getOutputStream();
    BufferedOutputStream output = null;
    byte[] imgBytes = action.getCustomImageInBytes();
    System.out.println("imgBytes--->" + imgBytes.toString());

    response.reset();/*from  w w  w .j a  v a  2  s.  c  o m*/
    response.setBufferSize(10240);
    response.setContentType("image/png");
    response.setContentLength(action.getCustomImageInBytes().length);
    response.setHeader("Content-Disposition", "inline; filename=\"sss\"");
    //response.getOutputStream().write(action.getCustomImageInBytes());
    // response.getOutputStream().flush();
    try {
        output = new BufferedOutputStream(response.getOutputStream(), 10240);
        output.write(imgBytes);
    } finally {
        // Gently close streams.
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.predic8.membrane.core.transport.http.HttpServerRunnable.java

public HttpServerRunnable(Socket socket, HttpTransport transport) throws IOException {
    this.exchange = new Exchange();
    exchange.setServerThread(this);
    log = LogFactory.getLog(HttpServerRunnable.class.getName());
    counter++;/*from w w  w. j a  v a  2 s  .  c  o  m*/
    log.debug("New ServerThread created. " + counter);
    this.sourceSocket = socket;
    srcIn = new BufferedInputStream(sourceSocket.getInputStream(), 2048);
    srcOut = new BufferedOutputStream(sourceSocket.getOutputStream(), 2048);
    sourceSocket.setSoTimeout(transport.getSocketTimeout());
    sourceSocket.setTcpNoDelay(transport.isTcpNoDelay());
    this.transport = transport;
    setClientSettings();
}

From source file:net.aircable.aircam.SL4A.java

public void connect() throws UnknownHostException, IOException, JSONException {
    proxy = AndroidProxy.sProxy;/*  w w  w.j a  v  a  2 s  . c o  m*/
    host = proxy.getAddress().getHostName();
    port = proxy.getAddress().getPort();
    secret = proxy.getSecret();

    connection = new Socket(host, port);
    input = new BufferedReader(new InputStreamReader(connection.getInputStream(), "8859_1"), 1 << 13);
    output = new PrintWriter(
            new OutputStreamWriter(new BufferedOutputStream(connection.getOutputStream(), 1 << 13), "8859_1"),
            true);
    id = 0;
    if (this.secret != null)
        try {
            this.callMethod("_authenticate", new JSONArray('[' + this.secret + ']'));
        } catch (RuntimeException e) {
            if (!e.getMessage().contains("Unknown RPC"))
                throw e;
        }
}

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  av a2 s.  c om

    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:edu.harvard.i2b2.eclipse.plugins.metadataLoader.util.FileUtil.java

public static void unzip(String zipname) throws IOException {

    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    //??    GZIPInputStream gzis = new GZIPInputStream(new BufferedInputStream(fis));

    // get directory of the zip file
    if (zipname.contains("\\"))
        zipname = zipname.replace("\\", "/");
    //   String zipDirectory = zipname.substring(0, zipname.lastIndexOf("/")+1) ;
    String zipDirectory = zipname.substring(0, zipname.lastIndexOf("."));
    new File(zipDirectory).mkdir();

    RunData.getInstance().setMetadataDirectory(zipDirectory);

    ZipEntry entry;/*ww w.jav a  2  s . c om*/
    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        if (entry.getName().contains("metadata")) {
            RunData.getInstance().setMetadataFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("schemes")) {
            RunData.getInstance().setSchemesFile(zipDirectory + "/" + entry.getName());
        } else if (entry.getName().contains("access")) {
            RunData.getInstance().setTableAccessFile(zipDirectory + "/" + entry.getName());
        }

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(zipDirectory + "/" + entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}

From source file:examples.utils.CifarReader.java

public static void downloadAndExtract() {

    if (new File("data", TEST_DATA_FILE).exists() == false) {
        try {/*from   w  ww .jav  a 2s .c  o m*/
            if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) {
                URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE);
                FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE);
                fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE);
                fos.close();
            }
            TarArchiveInputStream tar = new TarArchiveInputStream(
                    new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE)));
            TarArchiveEntry entry = null;
            while ((entry = tar.getNextTarEntry()) != null) {
                if (entry.isDirectory()) {
                    new File("data", entry.getName()).mkdirs();
                } else {
                    byte data[] = new byte[2048];
                    int count;
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File("data/", entry.getName())), 2048);

                    while ((count = tar.read(data, 0, 2048)) != -1) {
                        bos.write(data, 0, count);
                    }
                    bos.close();
                }
            }
            tar.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.pocketsoap.salesforce.soap.SoapRequestEntity.java

public final void writeRequest(OutputStream out) throws IOException {
    XMLOutputFactory f = XMLOutputFactory.newInstance();
    try {//from   ww w  .java  2  s . c  o  m
        XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(out, 1024), "UTF-8");
        w.writeStartDocument();
        w.writeStartElement("s", "Envelope", SOAP_NS);
        w.writeNamespace("s", SOAP_NS);
        w.writeNamespace("p", PARTNER_NS);
        w.setPrefix("p", PARTNER_NS);
        w.setPrefix("s", SOAP_NS);
        if (hasHeaders()) {
            w.writeStartElement(SOAP_NS, "Header");
            writeHeaders(w);
            w.writeEndElement();
        }
        w.writeStartElement(SOAP_NS, "Body");
        writeBody(w);
        w.writeEndElement();//body
        w.writeEndElement();//envelope
        w.writeEndDocument();
        w.close();
    } catch (XMLStreamException e) {
        throw new IOException("Error generating request xml", e);
    }
}

From source file:com.glaf.core.entity.mybatis.MyBatisSessionFactory.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {//w w  w  .  j  a  v  a  2 s.  c o m
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String name = zipEntry.getName();
            if (StringUtils.endsWith(name, "Mapper.xml")) {
                tmpByte = new byte[BUFFER];
                baos = new ByteArrayOutputStream();
                bos = new BufferedOutputStream(baos, BUFFER);
                int i = 0;
                while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                    bos.write(tmpByte, 0, i);
                }
                bos.flush();
                byte[] bytes = baos.toByteArray();
                IOUtils.closeStream(baos);
                IOUtils.closeStream(baos);
                zipMap.put(zipEntry.getName(), bytes);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

From source file:com.streamsets.datacollector.cluster.TarFileCreator.java

public static void createTarGz(File dir, File outputFile) throws IOException {
    Utils.checkState(dir.isDirectory(), Utils.formatL("Path {} is not a directory", dir));
    Utils.checkState(dir.canRead(), Utils.formatL("Directory {} cannot be read", dir));
    FileOutputStream dest = new FileOutputStream(outputFile);
    TarOutputStream out = new TarOutputStream(new BufferedOutputStream(new GZIPOutputStream(dest), 65536));
    File[] files = dir.listFiles();
    Utils.checkState(files != null, Utils.formatL("Directory {} could not be read", dir));
    Utils.checkState(files.length > 0, Utils.formatL("Directory {} is empty", dir));
    tarFolder(null, dir.getAbsolutePath(), out);
    out.close();/*from  ww w . jav a2  s.  c om*/
}

From source file:com.petpet.c3po.gatherer.FileExtractor.java

/**
 * Unpack data from the stream to specified directory.
 * //from  w ww  .j a  v a2s .c o  m
 * @param in
 *          stream with tar data
 * @param outputDir
 *          destination directory
 * @return true in case of success, otherwise - false
 */
private static void extract(ArchiveInputStream in, File outputDir) {
    try {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            // replace : for windows OS.
            final File file = new File(outputDir, entry.getName().replaceAll(":", "_"));

            if (entry.isDirectory()) {

                if (!file.exists()) {
                    file.mkdirs();
                }

            } else {
                file.getParentFile().mkdirs();
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);

                try {

                    IOUtils.copy(in, out);
                    out.flush();

                } finally {
                    try {
                        out.close();

                    } catch (IOException e) {
                        LOG.debug("An error occurred while closing the output stream for file '{}'. Error: {}",
                                file, e.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.debug("An error occurred while handling archive file. Error: {}", e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOG.debug("An error occurred while closing the archive stream . Error: {}", e.getMessage());
            }
        }
    }
}