Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

public synchronized String toString() 

Source Link

Document

Converts the buffer's contents into a string decoding bytes using the platform's default character set.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] bs = { 65, 66, 67, 68, 69 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(bs);//  ww w.  ja  v  a2 s  .  c om

    String str = baos.toString();

    System.out.println(str);

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.write(75);/*from  w w  w .  j  a  va2  s. c  o  m*/

    String str = baos.toString();
    System.out.println("Before Resetting : " + str);

    baos.reset();

    baos.write(65);

    str = baos.toString();
    System.out.println("After Resetting : " + str);

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    baos.close();/*from  ww  w .j a v a  2s.  com*/

    baos.write(buf);

    System.out.print(baos.toString());

}

From source file:Main.java

public static void main(String[] args) throws IOException {

    int size = 0;

    byte[] bs = { 65, 66, 67, 68, 69 };

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    for (byte b : bs) {
        baos.write(b);/*from ww  w .  java 2  s .c  o m*/

        String str = baos.toString();
        size = baos.size();

        System.out.print(size + ":");
        System.out.println(str);
    }

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // write byte array to the output stream
    baos.write(55);/*from w  w  w  .j  av a2s  . c  om*/

    // converts the byte to the default charset value
    String str = baos.toString();

    // prints the string
    System.out.println(str);

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();

    String streamingDataSql = "CREATE TABLE XML_Data (id INTEGER, Data LONG)";
    try {//from ww  w.j a v  a 2 s  .c o  m
        stmt.executeUpdate("DROP TABLE XML_Data");
    } catch (SQLException se) {
        if (se.getErrorCode() == 942)
            System.out.println("Error dropping XML_Data table:" + se.getMessage());
    }
    stmt.executeUpdate(streamingDataSql);

    File f = new File("employee.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO XML_Data VALUES (?,?)");
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    ResultSet rset = stmt.executeQuery("SELECT Data FROM XML_Data WHERE id=100");
    if (rset.next()) {
        InputStream xmlInputStream = rset.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;/*from   w  ww  .  j av  a2  s  .c  o m*/
    PreparedStatement pstmt = null;
    Statement stmt = null;
    ResultSet rs = null;
    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    stmt = conn.createStatement();
    createXMLTable(stmt);
    File f = new File("build.xml");
    long fileLength = f.length();
    FileInputStream fis = new FileInputStream(f);
    String SQL = "INSERT INTO XML_Data VALUES (?,?)";
    pstmt = conn.prepareStatement(SQL);
    pstmt.setInt(1, 100);
    pstmt.setAsciiStream(2, fis, (int) fileLength);
    pstmt.execute();
    fis.close();
    SQL = "SELECT Data FROM XML_Data WHERE id=100";
    rs = stmt.executeQuery(SQL);
    if (rs.next()) {
        InputStream xmlInputStream = rs.getAsciiStream(1);
        int c;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((c = xmlInputStream.read()) != -1)
            bos.write(c);
        System.out.println(bos.toString());
    }
    rs.close();
    stmt.close();
    pstmt.close();
    conn.close();
}

From source file:geocodingsql.Main.java

/**
 * @param args the command line arguments
 *//*from w  w w  .ja v a 2 s .  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:MainClass.java

public static void main(String args[]) throws Exception {
    ByteArrayOutputStream f = new ByteArrayOutputStream(12);
    System.out.println("Please 10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }/*w  w w.  j  a va 2  s. c o m*/
    System.out.println("Buffer as a string");
    System.out.println(f.toString());
    System.out.println("Into array");
    byte b[] = f.toByteArray();
    for (int i = 0; i < b.length; i++) {
        System.out.print((char) b[i]);
    }
    System.out.println();
    OutputStream f2 = new FileOutputStream("test.txt");
    f.writeTo(f2);
    f.reset();
    System.out.println("10 characters and a return");
    while (f.size() != 10) {
        f.write(System.in.read());
    }
    System.out.println("Done..");
}

From source file:ShowImage.java

public static void main(String args[]) throws IOException {
    List<File> files = ImageIOUtils.getFiles(args, new String[] { "ntf", "nsf" });

    for (Iterator iter = files.iterator(); iter.hasNext();) {
        try {//ww w  .j  av a2  s .c  o  m
            File file = (File) iter.next();
            log.debug("Reading: " + file.getAbsolutePath());
            NITFReader imageReader = (NITFReader) ImageIOUtils.getImageReader("nitf", file);

            for (int i = 0; i < imageReader.getRecord().getImages().length; ++i) {
                log.debug(file.getName() + "[" + i + "]");

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                ImageSubheader subheader = imageReader.getRecord().getImages()[i].getSubheader();
                subheader.print(new PrintStream(stream));
                log.debug(stream.toString());

                try {
                    int numBands = subheader.getBandCount();
                    String irep = subheader.getImageRepresentation().getStringData().trim();
                    int bitsPerPixel = subheader.getNumBitsPerPixel().getIntData();
                    int nBytes = (bitsPerPixel - 1) / 8 + 1;

                    if (irep.equals("RGB") && numBands == 3) {
                        BufferedImage image = imageReader.read(i);
                        ImageIOUtils.showImage(image, file.getName() + "[" + i + "]", true);
                    } else {
                        // read each band, separately
                        for (int j = 0; j < numBands; ++j) {
                            if (nBytes == 1 || nBytes == 2 || nBytes == 4 || nBytes == 8) {
                                ImageReadParam readParam = imageReader.getDefaultReadParam();
                                readParam.setSourceBands(new int[] { j });
                                BufferedImage image = imageReader.read(i, readParam);
                                ImageIOUtils.showImage(image, file.getName() + "[" + i + "][" + j + "]", true);

                                ImageIO.write(image, "jpg",
                                        new FileOutputStream("image" + i + "_" + j + ".jpg"));

                                // downsample
                                // readParam.setSourceSubsampling(2, 2, 0,
                                // 0);
                                // BufferedImage smallerImage = imageReader
                                // .read(i, readParam);
                                //
                                // ImageIOUtils.showImage(smallerImage,
                                // "DOWNSAMPLED: " + file.getName());

                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println(ExceptionUtils.getStackTrace(e));
                    log.error(ExceptionUtils.getStackTrace(e));
                }
            }
        } catch (Exception e) {
            log.debug(ExceptionUtils.getStackTrace(e));
        }
    }
}