List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream
public ByteArrayOutputStream()
From source file:Main.java
public static void main(String[] args) throws IOException { URL url = new URL("http://www.java2s.com/style/download.png"); InputStream inputStream = url.openStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int n = 0;//w w w . j a v a2 s .c o m while (-1 != (n = inputStream.read(buffer))) { output.write(buffer, 0, n); } inputStream.close(); byte[] data = output.toByteArray(); OutputStream out = new FileOutputStream("data.png"); out.write(data); out.close(); for (byte b : data) { System.out.printf("0x%x ", b); } }
From source file:MainClass.java
public static void main(String args[]) throws Exception { String[] a = new String[] { "a", "b", "c" }; Vector v = new Vector(Arrays.asList()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(v);/* w ww . j a va2 s . c om*/ oos.close(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); Vector v2 = (Vector) ois.readObject(); Enumeration e = v.elements(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } }
From source file:SaveVector.java
public static void main(String args[]) throws Exception { String data[] = { "Java", "Source", "and", "Support", "." }; Vector v = new Vector(Arrays.asList(data)); ByteArrayOutputStream b = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(b); o.writeObject(v);//ww w . jav a 2 s . c o m o.close(); ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray()); ObjectInputStream oo = new ObjectInputStream(bb); Vector v2 = (Vector) oo.readObject(); Enumeration e = v.elements(); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException, ClassNotFoundException { MyBean sc = new MyBean("Test1", "Test2"); System.out.println("Before:\n" + sc); ByteArrayOutputStream buf = new ByteArrayOutputStream(); ObjectOutputStream o = new ObjectOutputStream(buf); o.writeObject(sc);/*from ww w. j ava 2 s . co m*/ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray())); MyBean sc2 = (MyBean) in.readObject(); System.out.println("After:\n" + sc2); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Object object = new JButton("push me"); ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser")); out.writeObject(object);/*from w ww. j a v a2 s .c o m*/ out.close(); // Serialize to a byte array ByteArrayOutputStream bos = new ByteArrayOutputStream(); out = new ObjectOutputStream(bos); out.writeObject(object); out.close(); // Get the bytes of the serialized object byte[] buf = bos.toByteArray(); System.out.println(new String(buf)); }
From source file:Main.java
public static void main(String args[]) throws Exception { final ByteArrayOutputStream out = new ByteArrayOutputStream(); float sampleRate = 8000; int sampleSizeInBits = 8; int channels = 1; boolean signed = true; boolean bigEndian = true; final AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); final TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); line.open(format);/*from w ww.j a va2 s.c o m*/ line.start(); Runnable runner = new Runnable() { int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); byte buffer[] = new byte[bufferSize]; public void run() { try { int count = line.read(buffer, 0, buffer.length); if (count > 0) { out.write(buffer, 0, count); } out.close(); } catch (IOException e) { System.err.println("I/O problems: " + e); System.exit(-1); } } }; Thread captureThread = new Thread(runner); captureThread.start(); byte audio[] = out.toByteArray(); InputStream input = new ByteArrayInputStream(audio); final SourceDataLine line1 = (SourceDataLine) AudioSystem.getLine(info); final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize()); line1.open(format); line1.start(); runner = new Runnable() { int bufferSize = (int) format.getSampleRate() * format.getFrameSize(); byte buffer[] = new byte[bufferSize]; public void run() { try { int count; while ((count = ais.read(buffer, 0, buffer.length)) != -1) { if (count > 0) { line1.write(buffer, 0, count); } } line1.drain(); line1.close(); } catch (IOException e) { System.err.println("I/O problems: " + e); System.exit(-3); } } }; Thread playThread = new Thread(runner); playThread.start(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf")); ByteArrayOutputStream os = new ByteArrayOutputStream(); XmpWriter xmp = new XmpWriter(os); XmpSchema dc = new DublinCoreSchema(); XmpArray subject = new XmpArray(XmpArray.UNORDERED); subject.add("Metadata"); dc.setProperty(DublinCoreSchema.SUBJECT, subject); xmp.addRdfDescription(dc);//from w w w . jav a 2 s .c o m PdfSchema pdf = new PdfSchema(); pdf.setProperty(PdfSchema.KEYWORDS, "Hello World, XMP, Metadata"); pdf.setProperty(PdfSchema.VERSION, "1.4"); xmp.addRdfDescription(pdf); xmp.close(); writer.setXmpMetadata(os.toByteArray()); document.open(); document.add(new Paragraph("Hello World")); document.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { String sessionCookie = null;/* w ww. ja va 2 s .c o m*/ URL url = new java.net.URL("http://127.0.0.1/yourServlet"); URLConnection con = url.openConnection(); if (sessionCookie != null) { con.setRequestProperty("cookie", sessionCookie); } con.setUseCaches(false); con.setDoOutput(true); con.setDoInput(true); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteOut); out.flush(); byte buf[] = byteOut.toByteArray(); con.setRequestProperty("Content-type", "application/octet-stream"); con.setRequestProperty("Content-length", "" + buf.length); DataOutputStream dataOut = new DataOutputStream(con.getOutputStream()); dataOut.write(buf); dataOut.flush(); dataOut.close(); DataInputStream in = new DataInputStream(con.getInputStream()); int count = in.readInt(); in.close(); if (sessionCookie == null) { String cookie = con.getHeaderField("set-cookie"); if (cookie != null) { sessionCookie = parseCookie(cookie); System.out.println("Setting session ID=" + sessionCookie); } } System.out.println(count); }
From source file:Main.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String URL = "jdbc:odbc:dbname"; Connection dbConn = DriverManager.getConnection(URL, "user", "passw"); Employee employee = new Employee(42, "AA", 9); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(employee);//from w w w . j a va 2s. com byte[] employeeAsBytes = baos.toByteArray(); PreparedStatement pstmt = dbConn.prepareStatement("INSERT INTO EMPLOYEE (emp) VALUES(?)"); ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes); pstmt.setBinaryStream(1, bais, employeeAsBytes.length); pstmt.executeUpdate(); pstmt.close(); Statement stmt = dbConn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT emp FROM Employee"); while (rs.next()) { byte[] st = (byte[]) rs.getObject(1); ByteArrayInputStream baip = new ByteArrayInputStream(st); ObjectInputStream ois = new ObjectInputStream(baip); Employee emp = (Employee) ois.readObject(); } stmt.close(); rs.close(); dbConn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int size = 200; BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.BLUE);/*w w w. j a v a2 s . c o m*/ for (int i = 0; i < size; i += 5) { g.drawOval(i, i, size - i, size - i); } g.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image, "png", baos); String data = DatatypeConverter.printBase64Binary(baos.toByteArray()); String imageString = "data:image/png;base64," + data; String html = "<html><body><img src='" + imageString + "'></body></html>"; System.out.println(html); }