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:TimeServer.java

public void run() {
    Socket client = null;//from w w w.ja  v a2  s  .  c om

    while (true) {
        if (sock == null)
            return;
        try {
            client = sock.accept();
            BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());
            PrintWriter os = new PrintWriter(bos, false);
            String outLine;

            Date now = new Date();
            os.println(now);
            os.flush();

            os.close();
            client.close();
        } catch (IOException e) {
            System.out.println("Error: couldn't connect to client.");
            System.exit(1);
        }
    }
}

From source file:MainClass.java

static void applyCipher(String inFile, String outFile, Cipher cipher) throws Exception {
    CipherInputStream in = new CipherInputStream(new FileInputStream(inFile), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
    int BUFFER_SIZE = 8;
    byte[] buffer = new byte[BUFFER_SIZE];
    int numRead = 0;
    do {/*  www . j ava 2s . c  o  m*/
        numRead = in.read(buffer);
        if (numRead > 0)
            out.write(buffer, 0, numRead);
    } while (numRead == 8);
}

From source file:Main.java

public static void save(String filename, Node node) throws IOException {
    PrintStream out = null;/*from  w  w  w. j  a  v  a 2s .c  o  m*/
    try {
        out = new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)), true, "UTF-8");
        print(out, node);
    } catch (Exception ex) {
        throw new IOException(ex.getLocalizedMessage());
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
    }
}

From source file:Main.java

/** Write a DOM to a file. */
public static void writeXML(Document document, File file) throws IOException {
    // save the DOM to file
    StreamResult result = new StreamResult(new BufferedOutputStream(new FileOutputStream(file)));
    TransformerFactory transFactory = TransformerFactory.newInstance();
    try {/* w w  w  .  j ava 2s .  c om*/
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty("indent", "yes");
        transformer.transform(new DOMSource(document), result);
    } catch (TransformerConfigurationException ex) {
        throw new IOException(ex.getMessage());
    } catch (TransformerException ex) {
        throw new IOException(ex.getMessage());
    }
    result.getOutputStream().close();
}

From source file:Main.java

private static void copyWithStreams(File aSourceFile, File aTargetFile) {
    InputStream inStream = null;/*from  w ww.  j  a  v a  2  s.com*/
    OutputStream outStream = null;

    try {
        try {
            byte[] bucket = new byte[32 * 1024];
            inStream = new BufferedInputStream(new FileInputStream(aSourceFile));
            outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, false));
            int bytesRead = 0;
            while (bytesRead != -1) {
                bytesRead = inStream.read(bucket); //-1, 0, or more
                if (bytesRead > 0) {
                    outStream.write(bucket, 0, bytesRead);
                }
            }
        } finally {
            if (inStream != null)
                inStream.close();
            if (outStream != null)
                outStream.close();
        }
    } catch (FileNotFoundException ex) {
    } catch (IOException ex) {
    }
}

From source file:Utils.java

/**
 * Unpack an archive from a URL//from w  w w  . j a  v a2  s.c  o m
 * 
 * @param url
 * @param targetDir
 * @return the file to the url
 * @throws IOException
 */
public static File unpackArchive(URL url, File targetDir) throws IOException {
    if (!targetDir.exists()) {
        targetDir.mkdirs();
    }
    InputStream in = new BufferedInputStream(url.openStream(), 1024);
    // make sure we get the actual file
    File zip = File.createTempFile("arc", ".zip", targetDir);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
    copyInputStream(in, out);
    out.close();
    return unpackArchive(zip, targetDir);
}

From source file:Main.java

public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpURLConnection urlConnection = null;
    try {/*w w  w . j ava 2 s .  c  o m*/
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:com.nuvolect.deepdive.util.FileUtil.java

public static void writeFile(File file, String fileContents) {

    try {/* w w  w  .  j av a2 s .  c  o  m*/
        OutputStream out = null;

        FileUtils.forceMkdirParent(file);

        out = new BufferedOutputStream(new FileOutputStream(file));

        out.write(fileContents.getBytes());

        if (out != null)
            out.close();
    } catch (IOException e) {
        LogUtil.log(FileUtil.class, "File write failed: " + e.toString());
    }
}

From source file:MainClass.java

public void run() {
    try {/*from w  ww  .j  a v a 2 s  . c  o  m*/
        OutputStream out = new BufferedOutputStream(socket.getOutputStream());
        while (!socket.isClosed()) {
            out.write(data);
        }
        socket.close();
    } catch (Exception e) {
    }
}

From source file:com.glaf.chart.util.ChartUtils.java

public static byte[] createChart(Chart chartModel, JFreeChart chart) {
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    try {//w w  w. j  a  v a  2 s  .  c o  m
        baos = new ByteArrayOutputStream();
        bos = new BufferedOutputStream(baos);
        java.awt.image.BufferedImage bi = chart.createBufferedImage(chartModel.getChartWidth(),
                chartModel.getChartHeight());

        if ("png".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos);
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsPNG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        } else if ("jpeg".equalsIgnoreCase(chartModel.getImageType())) {
            EncoderUtil.writeBufferedImage(bi, chartModel.getImageType(), bos);
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            ServletUtilities.saveChartAsJPEG(chart, chartModel.getChartWidth(), chartModel.getChartHeight(),
                    info, null);
        }

        bos.flush();
        baos.flush();

        return baos.toByteArray();

    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(bos);
    }
}