Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

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

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:cn.lynx.emi.license.GenerateLicense.java

public static void main(String[] args) throws ClassNotFoundException, ParseException {
    if (args == null || args.length != 4) {
        System.err.println("Please provide [machine code] [cpu] [memory in gb] [yyyy-MM-dd] as parameter");
        return;//from  w w w  . j av a 2 s .  co m
    }

    InputStream is = GenerateLicense.class.getResourceAsStream("/privatekey");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String key = null;
    try {
        key = br.readLine();
    } catch (IOException e) {
        System.err.println(
                "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath");
        e.printStackTrace();
        return;
    }

    if (key == null) {
        System.err.println(
                "Can't read the private key file, make sure there's a file named \"privatekey\" in the root classpath");
        return;
    }

    String machineCode = args[0];
    int cpu = Integer.parseInt(args[1]);
    long mem = Long.parseLong(args[2]) * 1024 * 1024 * 1024;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    long expDate = sdf.parse(args[3]).getTime();

    LicenseBean lb = new LicenseBean();
    lb.setCpuCount(cpu);
    lb.setMemCount(mem);
    lb.setMachineCode(machineCode);
    lb.setExpireDate(expDate);

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(lb);
        os.close();

        String serializedLicense = Base64.encodeBase64String(baos.toByteArray());
        System.out.println("License:" + encrypt(key, serializedLicense));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/* ww  w  .j  ava2s. c o m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    createPdf("HelloLetter.pdf", "field", "value");
    RandomAccessFileOrArray letter = new RandomAccessFileOrArray("HelloLetter.pdf");
    PdfReader reader = new PdfReader(letter, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(reader, baos);
    AcroFields form = stamper.getAcroFields();
    form.setField("field", "World,");
    stamper.setFormFlattening(true);//from  w  w  w  .j a v  a2  s  .  com
    stamper.close();

    reader = new PdfReader(baos.toByteArray());
    Document document = new Document(reader.getPageSizeWithRotation(1));
    PdfCopy writer = new PdfCopy(document, new FileOutputStream("HelloWorldStampCopy.pdf"));
    document.open();
    writer.addPage(writer.getImportedPage(reader, 1));

    reader = new PdfReader(letter, null);
    baos = new ByteArrayOutputStream();
    stamper = new PdfStamper(reader, baos);
    form = stamper.getAcroFields();
    form.setField("field", "People,");
    stamper.setFormFlattening(true);
    stamper.close();

    reader = new PdfReader(baos.toByteArray());
    writer.addPage(writer.getImportedPage(reader, 1));
    document.close();
}

From source file:com.javacreed.examples.sql.Example3.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  . c o m
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (InputStream in = new LZ4BlockInputStream(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(baos)) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example3.LOGGER.debug("Done");
}

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  v  a  2s . 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:ByteArrayIOApp.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*from   ww w.  j  av a  2s .co m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());
    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();
    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);
    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

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/*from   ww  w  .  j ava  2s.  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:net.padaf.xmpbox.parser.XMLPropertiesDescriptionManager.java

/**
 * Sample of using to write/read information
 * //from  w ww. ja  va2s .c o  m
 * @param args
 *            Not used
 * @throws BuildPDFAExtensionSchemaDescriptionException
 *             When errors during building/reading xml file
 */
public static void main(String[] args) throws BuildPDFAExtensionSchemaDescriptionException {
    XMLPropertiesDescriptionManager ptMaker = new XMLPropertiesDescriptionManager();

    // add Descriptions
    for (int i = 0; i < 3; i++) {
        ptMaker.addPropertyDescription("name" + i, "description" + i);

    }

    // Display XML conversion
    System.out.println("Display XML Result:");
    ptMaker.toXML(System.out);

    // Sample to show how to build object from XML file
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ptMaker.toXML(bos);
    IOUtils.closeQuietly(bos);

    // emulate a new reading
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    ptMaker = new XMLPropertiesDescriptionManager();
    ptMaker.loadListFromXML(is);
    List<PropertyDescription> result = ptMaker.getPropertiesDescriptionList();
    System.out.println();
    System.out.println();
    System.out.println("Result of XML Loading :");
    for (PropertyDescription propertyDescription : result) {
        System.out.println(propertyDescription.getPropertyName() + " :" + propertyDescription.getDescription());
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    String s = "This is a test.";
    for (int i = 0; i < s.length(); ++i)
        outStream.write(s.charAt(i));/*from ww  w  .  j a  v  a  2 s .  c  o  m*/
    System.out.println("outstream: " + outStream);
    System.out.println("size: " + outStream.size());

    ByteArrayInputStream inStream;
    inStream = new ByteArrayInputStream(outStream.toByteArray());
    int inBytes = inStream.available();

    System.out.println("inStream has " + inBytes + " available bytes");
    byte inBuf[] = new byte[inBytes];
    int bytesRead = inStream.read(inBuf, 0, inBytes);

    System.out.println(bytesRead + " bytes were read");
    System.out.println("They are: " + new String(inBuf));
}

From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("inputfile1.zip");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CopyUtils.copy(fis, baos);/*from  ww  w .java  2  s. c o m*/

    byte[] byteArr = baos.toByteArray();
    fis.close();

    fis = new FileInputStream("inputfile2.zip");
    baos = new ByteArrayOutputStream();
    CopyUtils.copy(fis, baos);

    byteArr = baos.toByteArray();
    fis.close();

    //      if (args.length < 2) {
    //         System.err.println("Usage: " +
    //               ZipFileAggregator.class.getName() +
    //               " <output file> <file1, file2...>");
    //         System.exit(1);
    //      }
    //
    //      ZipFileAggregator agg=null;
    //      try {
    //         agg=new ZipFileAggregator(new FileOutputStream(args[0]));
    //         for (int i = 1; i < args.length; ++i) {
    //            File inputFile = new File(args[i]);
    //            System.out.println("Adding " + args[i]);
    //            try {
    //               agg.addFile(args[i], inputFile);
    //            } catch (ZipFileAggregator.BadInputZipFileException e) {
    //               System.out.println("Recoverable exception: " + e);
    //               System.out.println("Continuing...");
    //            }
    //         }
    //      } finally {
    //         if (agg != null) agg.close();
    //      }
    //      System.out.println("Done!");
}