List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream
public ByteArrayOutputStream()
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 www . j a va 2 s.c om*/ 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:net.awl.edoc.pdfa.compression.FlateDecode.java
public static void main(String[] args) throws Exception { Inflater inf = new Inflater(false); File f = new File("resources/content_2_0.ufd"); FileInputStream fis = new FileInputStream(f); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(fis, baos);/* w w w . j a v a 2 s .c om*/ IOUtils.closeQuietly(fis); IOUtils.closeQuietly(baos); byte[] buf = baos.toByteArray(); inf.setInput(buf); byte[] res = new byte[buf.length]; int size = inf.inflate(res); String s = new String(res, 0, size, "utf8"); System.err.println(s); }
From source file:illarion.compile.Compiler.java
public static void main(final String[] args) { ByteArrayOutputStream stdOutBuffer = new ByteArrayOutputStream(); PrintStream orgStdOut = System.out; System.setOut(new PrintStream(stdOutBuffer)); SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install();/*from w w w. j a v a 2 s.c o m*/ Options options = new Options(); final Option npcDir = new Option("n", "npc-dir", true, "The place where the compiled NPC files are stored."); npcDir.setArgs(1); npcDir.setArgName("directory"); npcDir.setRequired(false); options.addOption(npcDir); final Option questDir = new Option("q", "quest-dir", true, "The place where the compiled Quest files are stored."); questDir.setArgs(1); questDir.setArgName("directory"); questDir.setRequired(false); options.addOption(questDir); final Option type = new Option("t", "type", true, "This option is used to set what kind of parser is supposed to be used in case" + " the content of standard input is processed."); type.setArgs(1); type.setArgName("type"); type.setRequired(false); options.addOption(type); CommandLineParser parser = new GnuParser(); try { CommandLine cmd = parser.parse(options, args); String[] files = cmd.getArgs(); if (files.length > 0) { System.setOut(orgStdOut); stdOutBuffer.writeTo(orgStdOut); processFileMode(cmd); } else { System.setOut(orgStdOut); processStdIn(cmd); } } catch (final ParseException e) { final HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.printHelp("java -jar compiler.jar [Options] File", options, true); System.exit(-1); } catch (final IOException e) { LOGGER.error(e.getLocalizedMessage()); System.exit(-1); } }
From source file:Hex.java
public static void main(String[] args) { if (args.length != 3) { System.out.println("Usage: HexStrToBin enc/dec <infileName> <outfilename>"); System.exit(1);//from w ww . ja v a2s. c o m } try { ByteArrayOutputStream os = new ByteArrayOutputStream(); InputStream in = new FileInputStream(args[1]); int len = 0; byte buf[] = new byte[1024]; while ((len = in.read(buf)) > 0) os.write(buf, 0, len); in.close(); os.close(); byte[] data = null; if (args[0].equals("dec")) data = decode(os.toString()); else { String strData = encode(os.toByteArray()); data = strData.getBytes(); } FileOutputStream fos = new FileOutputStream(args[2]); fos.write(data); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.metamx.datatypes.ExampleWriteNewLineJson.java
public static void main(String[] args) throws Exception { final MmxAuctionSummary sampleAuction1 = MmxAuctionSummary.builder() .timestamp(new DateTime("2014-01-01T00:00:00.000Z")).auctionType(2).build(); final MmxAuctionSummary sampleAuction2 = MmxAuctionSummary.builder() .timestamp(new DateTime("2014-01-01T01:00:00.000Z")).auctionType(1).build(); List<MmxAuctionSummary> auctionList = Arrays.asList(sampleAuction1, sampleAuction2); final String separator = "\n"; final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); final OutputStream outStream = new ByteArrayOutputStream(); for (MmxAuctionSummary auction : auctionList) { outStream.write(objectMapper.writeValueAsBytes(auction)); outStream.write(separator.getBytes()); }/*w w w . ja va 2s.c om*/ System.out.println(outStream.toString()); }
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); }/*from w w w.j a v a 2 s . c o 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:net.awl.edoc.pdfa.font.CFFTest.java
/** * @param args// w w w. j a v a 2 s . c o m */ public static void main(String[] args) throws Exception { FileInputStream fis = new FileInputStream("/home/eric/UneCFFType0.cff0"); // FileInputStream fis = new FileInputStream("/home/eric/UneCFFType2.cff2"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copyLarge(fis, bos); CFFParser cp = new CFFParser(); List<CFFFont> lf = cp.parse(bos.toByteArray()); CFFFontROS font = (CFFFontROS) lf.get(0); System.out.println("CharstringType : " + font.getProperty("CharstringType")); int CID = 85; int fdAIndex = font.getFdSelect().getFd(CID); Map<String, Object> fd = font.getFontDict().get(fdAIndex); Map<String, Object> pd = font.getPrivDict().get(fdAIndex); for (Mapping m : font.getMappings()) { if (m.getSID() == CID) { // List<Object> l1 = new // Type1CharStringParser().parse(font.getCharStringsDict().get(m.getName())); List<Object> l2 = new Type2CharStringParser().parse(m.getBytes()); System.err.println(""); } } System.out.println(""); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); // Create a new document Document xmlDoc = builder.newDocument(); // Create root node for the document... Element root = xmlDoc.createElement("Players"); xmlDoc.appendChild(root);/*from w ww .j av a 2s. c o m*/ // Create a "player" node Element player = xmlDoc.createElement("player"); // Set the players ID attribute player.setAttribute("ID", "1"); // Create currentRank node... Element currentRank = xmlDoc.createElement("currentRank"); currentRank.setTextContent("1"); player.appendChild(currentRank); // Create previousRank node... Element previousRank = xmlDoc.createElement("previousRank"); previousRank.setTextContent("1"); player.appendChild(previousRank); // Create playerName node... Element playerName = xmlDoc.createElement("PlayerName"); playerName.setTextContent("Max"); player.appendChild(playerName); // Create Money node... Element money = xmlDoc.createElement("Money"); money.setTextContent("15"); player.appendChild(money); // Add the player to the root node... root.appendChild(player); ByteArrayOutputStream baos = null; baos = new ByteArrayOutputStream(); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty(OutputKeys.METHOD, "xml"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource domSource = new DOMSource(xmlDoc); StreamResult sr = new StreamResult(baos); tf.transform(domSource, sr); baos.flush(); System.out.println(new String(baos.toByteArray())); baos.close(); }
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;/*w w w. j ava 2s . c om*/ } 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:com.unifil.agendapaf.exemplos.word.XhtmlToDocxAndBack.java
public static void main(String[] args) throws Exception { // String xhtml // = "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:100%;\"><tbody><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr><tr><td>test</td><td>test</td></tr></tbody></table>"; String xhtml = FileUtils.readFileToString(new File("docx/a.html"), "UTF-8"); System.out.println("XHTML " + xhtml); // To docx, with content controls WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); //XHTMLImporter.setDivHandler(new DivToSdt()); wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(xhtml, null)); System.out.println(/*w w w .j a v a 2 s . c o m*/ XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)); wordMLPackage.save(new java.io.File("docx/OUT_from_XHTML.docx")); // Back to XHTML HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(wordMLPackage); // output to an OutputStream. OutputStream os = new ByteArrayOutputStream(); // If you want XHTML output Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true); Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL); System.out.println(((ByteArrayOutputStream) os).toString()); }