List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(byte b[], int off, int len)
From source file:Main.java
/** * Reads all the bytes from the passed input stream till end of stream * reached.//from w w w . ja v a 2s . c om * @param in The input stream to read from * @return array of bytes read * @exception IOException If any I/O exception occurs while reading data */ public static byte[] readFully(InputStream in) throws IOException { //create a new byte array stream to store the read data ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); //read the bytes till EOF byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { //append the bytes read to the byteArray buffer byteArray.write(buffer, 0, bytesRead); } //return the bytes read return byteArray.toByteArray(); }
From source file:Main.java
public static byte[] getBytesFromFile(File f) { if (f == null) { return null; }//w ww .j av a 2 s . c o m FileInputStream stream = null; ByteArrayOutputStream out = null; try { stream = new FileInputStream(f); out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = stream.read(b)) != -1) out.write(b, 0, n); return out.toByteArray(); } catch (IOException e) { } finally { try { if (stream != null) stream.close(); if (out != null) out.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
From source file:Main.java
public static byte[] zlibDecompress(byte[] data, int offset, int length) { byte[] output = null; Inflater decompresser = new Inflater(); decompresser.reset();/*from w w w .ja v a 2s .co m*/ try { decompresser.setInput(data, offset, length); } catch (Exception e) { return null; } ByteArrayOutputStream o = new ByteArrayOutputStream(data.length); try { byte[] buf = new byte[1024]; while (!decompresser.finished()) { int i = decompresser.inflate(buf); o.write(buf, 0, i); } output = o.toByteArray(); } catch (Exception e) { output = data; e.printStackTrace(); } finally { try { o.close(); } catch (IOException e) { e.printStackTrace(); } } decompresser.end(); return output; }
From source file:Main.java
public static byte[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);// w ww.jav 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(); System.out.println("Original: " + data.length / 1024 + " Kb"); System.out.println("Compressed: " + output.length / 1024 + " Kb"); return output; }
From source file:Main.java
private static byte[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);//from w w w .j a v a 2 s .co 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; }
From source file:Main.java
public static byte[] Compress(String text) throws Exception { Deflater compressor = new Deflater(); byte[] bytes = text.getBytes("UTF-16LE"); compressor.setInput(bytes);//from www .j a v a 2 s . co m // Create an expandable byte array to hold the compressed data ByteArrayOutputStream bos = new ByteArrayOutputStream(); compressor.finish(); byte[] buffer = new byte[1024]; try { while (!compressor.finished()) { int count = compressor.deflate(buffer); bos.write(buffer, 0, count); } } finally { compressor.finish(); } bos.close(); return bos.toByteArray(); }
From source file:com.hybridbpm.ui.component.chart.util.DiagrammeUtil.java
public static byte[] readFromSource(String path) { InputStream streamSource = DiagrammeUtil.class.getResourceAsStream(path); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length = 0; try {// w w w .j a va 2 s. c o m while ((length = streamSource.read(buffer)) != -1) { baos.write(buffer, 0, length); } } catch (IOException ex) { Logger.getLogger(DiagrammeUtil.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); } return baos.toByteArray(); }
From source file:de.decidr.workflowmodeleditor.server.servicetask.WSDLUploadServletImpl.java
/** * Completely read input stream to memory and convert it to a ByteArrayInputStream. *//*ww w . j a va 2 s . com*/ private static ByteArrayInputStream receiveUpload(InputStream stream) throws IOException { byte[] buffer = new byte[4096]; ByteArrayOutputStream data = new ByteArrayOutputStream(stream.available()); int bytesRead; while ((bytesRead = stream.read(buffer)) > 0) { data.write(buffer, 0, bytesRead); } return new ByteArrayInputStream(data.toByteArray()); }
From source file:com.machinepublishers.jbrowserdriver.Util.java
static byte[] toBytes(InputStream inputStream) throws IOException { try {//from w w w. j av a 2 s . c o m final byte[] bytes = new byte[8192]; ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length); try { for (int len = 0; -1 != (len = inputStream.read(bytes, 0, bytes.length)); out.write(bytes, 0, len)) ; } catch (EOFException | SSLProtocolException | ConnectionClosedException | SocketException e) { } return out.toByteArray(); } finally { close(inputStream); } }
From source file:com.vmware.identity.rest.core.client.RequestExecutor.java
private static String consumeContent(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(in.available()); final byte[] data = new byte[8196]; int len = 0;/*from w w w . j a v a2s . c o m*/ while ((len = in.read(data)) > 0) { out.write(data, 0, len); } return out.toString(); }