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

public static void main(String[] args) {
    int numRows = 6, numCols = 11, pix = 20;
    PaletteData pd = new PaletteData(
            new RGB[] { new RGB(0x00, 0x00, 0x00), new RGB(0x80, 0x80, 0x80), new RGB(0xFF, 0xFF, 0xFF) });

    ImageData[] flagArray = new ImageData[3];
    for (int frame = 0; frame < flagArray.length; frame++) {
        flagArray[frame] = new ImageData(pix * numCols, pix * numRows, 4, pd);
        flagArray[frame].delayTime = 10;
        for (int x = 0; x < pix * numCols; x++) {
            for (int y = 0; y < pix * numRows; y++) {
                int value = (((x / pix) % 3) + (3 - ((y / pix) % 3)) + frame) % 3;
                flagArray[frame].setPixel(x, y, value);
            }/* w  w w . ja v a 2  s  . co m*/
        }
    }

    ImageLoader gifloader = new ImageLoader();
    ByteArrayOutputStream flagByte[] = new ByteArrayOutputStream[3];
    byte[][] gifarray = new byte[3][];
    gifloader.data = flagArray;

    for (int i = 0; i < 3; i++) {
        flagByte[i] = new ByteArrayOutputStream();
        flagArray[0] = flagArray[i];
        gifloader.save(flagByte[i], SWT.IMAGE_GIF);
        gifarray[i] = flagByte[i].toByteArray();
    }

    byte[] gif = new byte[4628];
    System.arraycopy(gifarray[0], 0, gif, 0, 61);
    System.arraycopy(new byte[] { 33, (byte) 255, 11 }, 0, gif, 61, 3);
    System.arraycopy("NETSCAPE2.0".getBytes(), 0, gif, 64, 11);
    System.arraycopy(new byte[] { 3, 1, -24, 3, 0, 33, -7, 4, -24 }, 0, gif, 75, 9);
    System.arraycopy(gifarray[0], 65, gif, 84, 1512);

    for (int i = 1; i < 3; i++) {
        System.arraycopy(gifarray[i], 61, gif, 1516 * i + 80, 3);
        gif[1516 * i + 83] = (byte) -24;
        System.arraycopy(gifarray[i], 65, gif, 1516 * i + 84, 1512);
    }

    try {
        DataOutputStream in = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream(new File("FlagGIF.gif"))));
        in.write(gif, 0, gif.length);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cmd.freebase2rdf.java

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();/*from   w  ww . j a v a2 s  .  c om*/
    }
    File input = new File(args[0]);
    if (!input.exists())
        error("File " + input.getAbsolutePath() + " does not exist.");
    if (!input.canRead())
        error("Cannot read file " + input.getAbsolutePath());
    if (!input.isFile())
        error("Not a file " + input.getAbsolutePath());
    File output = new File(args[1]);
    if (output.exists())
        error("Output file " + output.getAbsolutePath()
                + " already exists, this program do not override existing files.");
    if (output.canWrite())
        error("Cannot write file " + output.getAbsolutePath());
    if (output.isDirectory())
        error("Not a file " + output.getAbsolutePath());
    if (!output.getName().endsWith(".nt.gz"))
        error("Output filename should end with .nt.gz, this is the only format supported.");

    BufferedReader in = new BufferedReader(
            new InputStreamReader(new BZip2CompressorInputStream(new FileInputStream(input))));
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(output)));
    String line;

    ProgressLogger progressLogger = new ProgressLogger(log, "lines", 100000, 1000000);
    progressLogger.start();
    Freebase2RDF freebase2rdf = null;
    try {
        freebase2rdf = new Freebase2RDF(out);
        while ((line = in.readLine()) != null) {
            freebase2rdf.send(line);
            progressLogger.tick();
        }
    } finally {
        if (freebase2rdf != null)
            freebase2rdf.close();
    }
    print(log, progressLogger);
}

From source file:com.leonarduk.finance.chart.PieChartFactory.java

/**
 * Starting point for the demo./*from  w ww .  j  a  v a  2s .c om*/
 *
 * @param args
 *            ignored.
 */
public static void main(final String[] args) {
    final PieChartFactory factory = new PieChartFactory("Title");
    factory.put("A", 12.2).put("B", 13.2).put("C", 31.2);

    try {
        // write an HTML page incorporating the image with an image map
        final File file2 = new File("multipiechart100.html");
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(file2));
        final PrintWriter writer = new PrintWriter(out);
        writer.println("<HTML>");
        writer.println("<HEAD><TITLE>JFreeChart Image Map Demo</TITLE></HEAD>");
        writer.println("<BODY>");
        // ChartUtilities.writeImageMap(writer, "chart", info);
        writer.println(ChartDisplay.saveImageAsPngAndReturnHtmlLink("345", 400, 400, factory.buildChart()));
        writer.println("</BODY>");
        writer.println("</HTML>");
        writer.close();

    } catch (final IOException e) {
        System.out.println(e.toString());
    }

}

From source file:info.debatty.java.datasets.examples.GaussianMixtureBuilder.java

/**
 * @param args the command line arguments
 *///  ww  w.  java2 s . co m
public static void main(String[] args) throws IOException, ClassNotFoundException {
    Dataset dataset = new Dataset.Builder(DIMENSIONALITY, CENTERS).setOverlap(Dataset.Builder.Overlap.MEDIUM)
            .varyDeviation(true).varyWeight(true).setSize(SIZE).build();

    // You can serialize and save your Dataset.
    // This will not save all the points, but only the Dataset oject
    // (including eventual random seeds),
    // which allows to reproduce the dataset using only a small amount of
    // memory
    File file = File.createTempFile("testfile", ".ser");
    dataset.save(new FileOutputStream(file));

    Dataset d2 = (Dataset) Dataset.load(new FileInputStream(file));

    // You can also save to complete data to disk if needed
    // (e.g. for plotting with Gnuplot)
    d2.saveCsv(new BufferedOutputStream(new FileOutputStream(File.createTempFile("gaussian", ".dat"))));

    float[][] float_array = new float[SIZE][];
    int i = 0;
    for (double[] vector : d2) {
        //GaussianMixture.println(vector);
        float_array[i] = toFloatArray(vector);
        i++;
    }

    final FastScatterPlot2D demo = new FastScatterPlot2D("Gaussian Mixture Plot", transposeMatrix(float_array));
    demo.pack();
    demo.setVisible(true);
}

From source file:Base64Stuff.java

public static void main(String[] args) {
    //Random random = new Random();
    try {/*from   w  w w  . j a va2  s.c om*/
        File file1 = new File("C:\\\\Program Files\\\\ImageJ\\\\images\\\\confocal-series-10001.tif");
        //File file2 = new File("C:\\Program Files\\ImageJ\\images\\confocal-series-10000.tif");
        ImagePlus image1 = new ImagePlus(
                "C:\\\\Program Files\\\\ImageJ\\\\images\\\\confocal-series-10001.tif");
        //ImagePlus image2 = new ImagePlus("C:\\Program Files\\ImageJ\\images\\two.tif");

        byte[] myBytes1 = org.apache.commons.io.FileUtils.readFileToByteArray(file1);
        //byte[] myBytes2 = org.apache.commons.io.FileUtils.readFileToByteArray(file2);
        //random.nextBytes(randomBytes);

        //String internalVersion1 = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(myBytes1);
        //String internalVersion2 = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.encode(myBytes2);
        byte[] apacheBytes1 = org.apache.commons.codec.binary.Base64.encodeBase64(myBytes1);
        //byte[] apacheBytes2 =  org.apache.commons.codec.binary.Base64.encodeBase64(myBytes2);
        String string1 = new String(apacheBytes1);
        //String string2 = new String(apacheBytes2);

        System.out.println("File1 length:" + string1.length());
        //System.out.println("File2 length:" + string2.length());
        System.out.println(string1);
        //System.out.println(string2);

        System.out.println("Image1 size: (" + image1.getWidth() + "," + image1.getHeight() + ")");
        //System.out.println("Image2 size: (" + image2.getWidth() + "," + image2.getHeight() + ")");

        String urlParameters = "data=" + string1 + "&size=1000x1000";

        URL url = new URL("http://api.qrserver.com/v1/create-qr-code/");
        URLConnection conn = url.openConnection();

        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        writer.write(urlParameters);
        writer.flush();

        //byte buf[] = new byte[700000000];

        BufferedInputStream reader = new BufferedInputStream(conn.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream("C:\\Users\\expertoweb\\Desktop\\qrcode2.png"));

        int data;
        while ((data = reader.read()) != -1) {
            bos.write(data);
        }

        writer.close();
        reader.close();
        bos.close();

    } catch (IOException e) {
    }
}

From source file:com.icesoft.applications.faces.address.XWrapperUtil.java

public static void main(String[] args) {

    if (log.isDebugEnabled()) {
        log.debug("Converting database...");
    }/*from  w  w w . j a v  a 2 s  .com*/

    //load the CSV file
    InputStream is = MatchAddressDB.class.getResourceAsStream(CSV_ADDRESS_DB);
    BufferedReader buff = new BufferedReader(new InputStreamReader(is));

    XMLEncoder xEncode = null;
    XAddressDataWrapper xData;

    //open the XML encoder and attempt to write the xml file
    try {
        xEncode = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(XML_GZ_ADDRESS_DB)));
    } catch (FileNotFoundException ex) {
        log.error("Database could not be written.", ex);
    }

    //first line
    char[] line = getNextLine(buff);

    while (line != null) {

        //get three strings within quotes
        String addressValues[] = new String[3];
        int stringValueStart = 0, stringValueEnd;

        for (int i = 0; i < 3; i++) {
            //opening quote
            while (line[stringValueStart++] != '\"') {
            }
            stringValueEnd = stringValueStart + 1;
            //closing quote
            while (line[stringValueEnd] != '\"') {
                stringValueEnd++;
            }
            //value
            addressValues[i] = new String(line, stringValueStart, stringValueEnd - stringValueStart);
            stringValueStart = stringValueEnd + 1;
        }

        //assign the data to the wrapper
        xData = new XAddressDataWrapper();
        xData.setCity(addressValues[1]);
        xData.setState(addressValues[2]);
        xData.setZip(addressValues[0]);

        //read the next line (entry) in the CSV file
        line = getNextLine(buff);
    }
    //close the XML Encoder
    try {
        xEncode.close();
    } catch (NullPointerException npe) {
        log.error("Could not close XML Encoder.", npe);
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Closed XML Encoder.");
    }
}

From source file:org.jfree.chart.demo.MeterChartDemo2.java

/**
 * Starting point for the demo./* w  w w. ja v  a  2 s  .  co  m*/
 *
 * @param args  ignored.
 */
public static void main(final String[] args) {

    final ValueDataset dataset = new DefaultValueDataset(75.0);
    final MeterPlot plot = new MeterPlot(dataset);
    final JFreeChart chart = new JFreeChart("Scaled Image Test", plot);

    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************

    // save it to an image
    try {
        final File file1 = new File("meterchart100.png");
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(file1));
        final BufferedImage image = chart.createBufferedImage(200, 200, 400, 400, null);
        ChartUtilities.writeBufferedImageAsPNG(out, image);
    } catch (IOException e) {
        System.out.println(e.toString());
    }

}

From source file:edu.byu.nlp.data.app.DataExporter.java

public static void main(String[] args) throws IOException {
    args = new ArgumentParser(DataExporter.class).parseArgs(args).getPositionalArgs();

    RandomGenerator rnd = new MersenneTwister();
    Dataset dataset = readData(rnd);/*  w  w  w.  j  av a  2  s.co m*/

    Iterable<String> it = Iterables.transform(dataset, new Instance2SVMLitePlus());
    if (args.length < 1) {
        Writers.writeLines(new PrintWriter(new BufferedOutputStream(System.out)), it);
    } else {
        Files2.writeLines(it, args[0]);
    }
}

From source file:de.topobyte.osm4j.pbf.executables.EntitySplitBlockwise.java

public static void main(String[] args) throws IOException {
    // @formatter:off
    Options options = new Options();
    OptionHelper.add(options, OPTION_INPUT, true, true, "input file");
    OptionHelper.add(options, OPTION_OUTPUT_NODES, true, false, "the file to write nodes to");
    OptionHelper.add(options, OPTION_OUTPUT_WAYS, true, false, "the file to write ways to");
    OptionHelper.add(options, OPTION_OUTPUT_RELATIONS, true, false, "the file to write relations to");
    // @formatter:on

    CommandLine line = null;//from www  . j a  v a  2s  . co m
    try {
        line = new GnuParser().parse(options, args);
    } catch (ParseException e) {
        System.out.println("unable to parse command line: " + e.getMessage());
        new HelpFormatter().printHelp(HELP_MESSAGE, options);
        System.exit(1);
    }

    if (line == null) {
        return;
    }

    String inputPath = line.getOptionValue(OPTION_INPUT);
    File file = new File(inputPath);
    PbfFile pbfFile = new PbfFile(file);

    OutputStream outNodes = null, outWays = null, outRelations = null;
    if (line.hasOption(OPTION_OUTPUT_NODES)) {
        String path = line.getOptionValue(OPTION_OUTPUT_NODES);
        FileOutputStream fos = new FileOutputStream(path);
        outNodes = new BufferedOutputStream(fos);
    }
    if (line.hasOption(OPTION_OUTPUT_WAYS)) {
        String path = line.getOptionValue(OPTION_OUTPUT_WAYS);
        FileOutputStream fos = new FileOutputStream(path);
        outWays = new BufferedOutputStream(fos);
    }
    if (line.hasOption(OPTION_OUTPUT_RELATIONS)) {
        String path = line.getOptionValue(OPTION_OUTPUT_RELATIONS);
        FileOutputStream fos = new FileOutputStream(path);
        outRelations = new BufferedOutputStream(fos);
    }

    if (outNodes == null && outWays == null && outRelations == null) {
        System.out.println("You should specify an output for at least one entity");
        System.exit(1);
    }

    EntitySplitBlockwise task = new EntitySplitBlockwise(pbfFile, outNodes, outWays, outRelations);
    task.execute();
}

From source file:de.topobyte.osm4j.pbf.executables.EntitySplit.java

public static void main(String[] args) throws IOException {
    // @formatter:off
    Options options = new Options();
    OptionHelper.add(options, OPTION_INPUT, true, false, "input file");
    OptionHelper.add(options, OPTION_OUTPUT_NODES, true, false, "the file to write nodes to");
    OptionHelper.add(options, OPTION_OUTPUT_WAYS, true, false, "the file to write ways to");
    OptionHelper.add(options, OPTION_OUTPUT_RELATIONS, true, false, "the file to write relations to");
    // @formatter:on

    CommandLine line = null;//from  w  ww .  j  ava2 s .  co  m
    try {
        line = new GnuParser().parse(options, args);
    } catch (ParseException e) {
        System.out.println("unable to parse command line: " + e.getMessage());
        new HelpFormatter().printHelp(HELP_MESSAGE, options);
        System.exit(1);
    }

    if (line == null) {
        return;
    }

    InputStream in = null;
    if (line.hasOption(OPTION_INPUT)) {
        String inputPath = line.getOptionValue(OPTION_INPUT);
        File file = new File(inputPath);
        FileInputStream fis = new FileInputStream(file);
        in = new BufferedInputStream(fis);
    } else {
        in = new BufferedInputStream(System.in);
    }

    OutputStream outNodes = null, outWays = null, outRelations = null;
    if (line.hasOption(OPTION_OUTPUT_NODES)) {
        String path = line.getOptionValue(OPTION_OUTPUT_NODES);
        FileOutputStream fos = new FileOutputStream(path);
        outNodes = new BufferedOutputStream(fos);
    }
    if (line.hasOption(OPTION_OUTPUT_WAYS)) {
        String path = line.getOptionValue(OPTION_OUTPUT_WAYS);
        FileOutputStream fos = new FileOutputStream(path);
        outWays = new BufferedOutputStream(fos);
    }
    if (line.hasOption(OPTION_OUTPUT_RELATIONS)) {
        String path = line.getOptionValue(OPTION_OUTPUT_RELATIONS);
        FileOutputStream fos = new FileOutputStream(path);
        outRelations = new BufferedOutputStream(fos);
    }

    if (outNodes == null && outWays == null && outRelations == null) {
        System.out.println("You should specify an output for at least one entity");
        System.exit(1);
    }

    EntitySplit task = new EntitySplit(in, outNodes, outWays, outRelations);
    task.execute();
}