Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

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

Prototype

public ByteArrayOutputStream(int size) 

Source Link

Document

Creates a new ByteArrayOutputStream , with a buffer capacity of the specified size, in bytes.

Usage

From source file:com.javacreed.examples.sql.Example4.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {

            @Override//from w  w w .  j a v a 2 s . co  m
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (InputStream in = new LZ4BlockInputStream(
                        CryptoUtils.wrapInToCipheredInputStream(resultSet.getBinaryStream("compressed")))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new LZ4BlockOutputStream(
                        CryptoUtils.wrapInToCipheredOutputStream(baos))) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }

        };
        test.runTest();
    }
    Example4.LOGGER.debug("Done");
}

From source file:com.javacreed.examples.sql.Example2.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {
            @Override//  ww w . ja v a 2 s  . c  om
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (GZIPInputStream in = new GZIPInputStream(resultSet.getBinaryStream("compressed"))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                // Compress the data before inserting it. We need to compress before inserting the data to make this process
                // as realistic as possible.
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new GZIPOutputStream(baos, data.length())) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example2.LOGGER.debug("Done");
}

From source file:geocodingsql.Main.java

/**
 * @param args the command line arguments
 *//*from w  ww .j  av a 2s .  co  m*/
public static void main(String[] args) throws JSONException {
    Main x = new Main();
    ResultSet rs = null;
    String string = "";

    x.establishConnection();
    rs = x.giveName();

    try {
        while (rs.next()) {
            string += rs.getString(1) + " ";
        }

        JOptionPane.showMessageDialog(null, string, "authors", 1);
    } catch (Exception e) {
        System.out.println("Problem when printing the database.");
    }
    x.closeConnection();

    // Now do Geocoding
    String req = "https://maps.googleapis.com/maps/api/geocode/json?latlng=41.3166662867211,-72.9062497615814&result_type=point_of_interest&key="
            + key;
    try {
        URL url = new URL(req + "&sensor=false");
        URLConnection conn = url.openConnection();
        ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
        IOUtils.copy(conn.getInputStream(), output);
        output.close();
        req = output.toString();
    } catch (Exception e) {
        System.out.println("Geocoding Error");
    }
    JSONObject jObject = new JSONObject(req);
    JSONArray resultArray = jObject.getJSONArray("results");
    //this prints out the neighborhood of the provided coordinates
    System.out.println(resultArray.getJSONObject(0).getJSONArray("address_components")
            .getJSONObject("neighborhood").getString("long_name"));
}

From source file:geocodingissues.Main.java

/**
 * @param args the command line arguments
 *///from   www  .j  a  v a2s .c o  m
public static void main(String[] args) throws JSONException {
    Main x = new Main();
    ResultSet rs = null;

    x.establishConnection();
    //x.addColumns(); //already did this

    rs = x.getLatLong();

    int id;
    double latitude;
    double longitude;
    String req;

    String street_no;
    String street;
    String neighborhood;
    String locality;
    String PC;

    JSONObject jObject;
    JSONArray resultArray;
    JSONArray compArray;

    try {
        while (rs.next()) {
            id = rs.getInt("id");
            latitude = rs.getDouble("latitude");
            longitude = rs.getDouble("longitude");

            //System.out.println("id: " + id);

            req = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + Double.toString(latitude) + ","
                    + Double.toString(longitude)
                    + "&result_type=street_address|neighborhood|locality|postal_code&key=" + key;

            try {
                URL url = new URL(req + "&sensor=false");
                URLConnection conn = url.openConnection();
                ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
                IOUtils.copy(conn.getInputStream(), output);
                output.close();
                req = output.toString();
            } catch (Exception e) {
                System.out.println("Geocoding Error");
            }
            if (req.contains("OVER_QUERY_LIMIT")) {
                System.out.println("Over Daily Query Limit");
                System.exit(0);
            }
            if (!req.contains("ZERO_RESULTS")) {
                //System.out.println("req: ");
                //System.out.println(req);
                jObject = new JSONObject(req);
                resultArray = jObject.getJSONArray("results");

                // Retrieve information on street address and insert into table
                compArray0 = resultArray.getJSONObject(0).getJSONArray("address_components");
                street_no = compArray0.getJSONObject(0).getString("long_name");
                street = compArray0.getJSONObject(1).getString("long_name");
                x.insertValues(id, street_no, street);

                // Retrieve information on neighborhood and insert into table
                compArray1 = resultArray.getJSONObject(1).getJSONArray("address_components");
                neighborhood = compArray1.getJSONObject(0).getString("long_name");
                x.insertNbhd(id, neighborhood);

                // Retrieve information on locality and insert into table
                compArray2 = resultArray.getJSONObject(2).getJSONArray("address_components");
                locality = compArray2.getJSONObject(0).getString("long_name");
                x.insertLocality(id, locality);

                // Retrieve information on postal code and insert into table
                compArray3 = resultArray.getJSONObject(3).getJSONArray("address_components");
                PC = compArray3.getJSONObject(0).getString("long_name");
                x.insertPC(id, PC);
            }
        }
    } catch (Exception e) {
        System.out.println("Problem when updating the database.");
    }
    x.closeConnection();
}

From source file:Main.java

public static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;/*w w  w.j  av a 2  s . c  om*/
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}

From source file:Main.java

public static byte[] readAll(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int c = is.read(buf);
    while (-1 != c) {
        baos.write(buf, 0, c);//from  w ww. j  av  a  2  s . c o  m
        c = is.read(buf);
    }
    baos.flush();
    baos.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] decodeB62(char[] data) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
    try {//from w  ww. j a va2s.c om
        int pos = 0, val = 0;
        for (int i = 0; i < data.length; i++) {
            char c = data[i];
            if (c == 'i') {
                c = data[++i];
                c = /**/c == 'a' ? 'i' :
                /**/c == 'b' ? '+' :
                /**/c == 'c' ? '/' : data[--i];
            }
            val = (val << 6) | decodes[c];
            pos += 6;
            while (pos > 7) {
                baos.write(val >> (pos -= 8));
                val &= ((1 << pos) - 1);
            }
        }
    } catch (Exception e) {
    }
    return baos.toByteArray();
}

From source file:Main.java

public static final byte[] compress(byte[] val) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    gos.write(val, 0, val.length);
    gos.finish();/*from   www  . j  a v  a  2 s.  c  o  m*/
    gos.close();

    // store it and set compression flag
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[4096];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4096);

    // Do the first byte via a blocking read
    outputStream.write(inputStream.read());

    // Slurp the rest
    int available = 0;// inputStream.available();
    boolean run = true;
    while (run && (available = inputStream.available()) > 0) {
        // Log.d(TAG, "slurp " + available);
        while (available > 0) {
            int cbToRead = Math.min(buffer.length, available);
            int cbRead = inputStream.read(buffer, 0, cbToRead);
            if (cbRead <= 0) {
                run = false;/*ww  w  . j av a2 s. com*/
                break;
            }
            outputStream.write(buffer, 0, cbRead);
            available -= cbRead;
        }
    }
    return outputStream.toByteArray();
}

From source file:Main.java

private static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//from w w  w  .ja v  a2s.  c o m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index  
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    deflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Compressed: " + output.length + " bytes.");
    return output;
}