List of usage examples for java.io ByteArrayOutputStream flush
public void flush() throws IOException
From source file:software.uncharted.util.HTTPUtil.java
public static String post(String urlToRead, String postData) { URL url;//from www .j a va 2s . c o m HttpURLConnection conn; try { url = new URL(urlToRead); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/json"); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(postData); wr.flush(); wr.close(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; InputStream is = conn.getInputStream(); while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return buffer.toString(); } catch (Exception e) { e.printStackTrace(); System.err.println("Failed to read URL: " + urlToRead + " <" + e.getMessage() + ">"); } return null; }
From source file:Main.java
/** * Converts a node to a string./*from w ww . j a v a2 s. co m*/ * @param node The node to convert. * @return A string representation of the node. */ public static String getStringFromNode(Node node) { DOMSource domSource = new DOMSource(node); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, result); baos.flush(); return new String(baos.toByteArray()); } catch (Exception e) { throw new RuntimeException("Failed to stream node to string", e); } finally { try { baos.close(); } catch (Exception e) { } } }
From source file:net.ripe.rpki.commons.crypto.x509cert.X509CertificateUtil.java
/** * Get a base 64-encoded, DER-encoded X.509 subjectPublicKeyInfo as used for the Trust Anchor Locator (TAL) * * @throws X509CertificateOperationException * * @throws IOException//from w w w . j a v a 2 s . c om */ public static String getEncodedSubjectPublicKeyInfo(X509Certificate certificate) { byte[] tbsCertificate; try { tbsCertificate = certificate.getTBSCertificate(); } catch (CertificateEncodingException e) { throw new X509CertificateOperationException("Can't extract TBSCertificate from certificate", e); } ASN1Sequence tbsCertificateSequence = (ASN1Sequence) Asn1Util.decode(tbsCertificate); TBSCertificateStructure tbsCertificateStructure = new TBSCertificateStructure(tbsCertificateSequence); SubjectPublicKeyInfo subjectPublicKeyInfo = tbsCertificateStructure.getSubjectPublicKeyInfo(); try { byte[] data = subjectPublicKeyInfo.getEncoded(); Base64Encoder encoder = new Base64Encoder(); ByteArrayOutputStream out = new ByteArrayOutputStream(); encoder.encode(data, 0, data.length, out); out.flush(); return out.toString(); } catch (IOException e) { throw new X509CertificateOperationException("Can't encode SubjectPublicKeyInfo for certificate", e); } }
From source file:Main.java
public static byte[] decompress(byte[] bytes) throws IOException { GZIPInputStream gzip = null;//from w ww.j av a 2s.co m ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(bytes)); int len = 0; byte data[] = new byte[BUFFER_SIZE]; while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) { baos.write(data, 0, len); } gzip.close(); baos.flush(); return baos.toByteArray(); } finally { if (gzip != null) { gzip.close(); } if (baos != null) { baos.close(); } } }
From source file:com.chicm.cmraft.rpc.PacketUtils.java
private static int writeRpc(AsynchronousSocketChannel channel, Message header, Message body, int totalSize) throws IOException, InterruptedException, ExecutionException { // writing total size so that server can read all request data in one read //LOG.debug("total size:" + totalSize); long t = System.currentTimeMillis(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); writeIntToStream(totalSize, bos);//from w ww. ja v a 2s . c o m header.writeDelimitedTo(bos); if (body != null) body.writeDelimitedTo(bos); bos.flush(); byte[] b = bos.toByteArray(); ByteBuffer buf = ByteBuffer.allocateDirect(totalSize + 4); buf.put(b); buf.flip(); channel.write(buf).get(); if (LOG.isTraceEnabled()) { LOG.trace("Write Rpc message to socket, takes " + (System.currentTimeMillis() - t) + " ms, size " + totalSize); LOG.trace("message:" + body); } return totalSize; }
From source file:it.polito.elite.dog.core.library.model.DeviceStatus.java
/** * Serialize a DeviceStatus into a String using a byte encoding * @param object//from www . j a va 2 s . c om * a DeviceStatus ready for serialization * @return a byte encoded String of a DeviceStatus * @throws IOException */ public static String serializeToString(DeviceStatus object) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream encoder = new ObjectOutputStream(baos); //serialize the DeviceStatus encoder.writeObject(object); //flush and close everything encoder.flush(); baos.flush(); encoder.close(); baos.close(); return new String(Base64.encodeBase64(baos.toByteArray())); }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] waterMarkJPG(String baseImagePath, String waterMartPath) { try {/* ww w . j a v a 2 s .c o m*/ File origFile = new File(baseImagePath); ImageIcon icon = new ImageIcon(origFile.getPath()); BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB); Graphics graphics = bufferedImage.getGraphics(); graphics.drawImage(icon.getImage(), 0, 0, null); ImageIcon png = new ImageIcon(waterMartPath); graphics.drawImage(png.getImage(), 0, 0, null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); return imageInByte; } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:org.asqatasun.crawler.util.CrawlUtils.java
/** * Get the raw content of an image//from ww w .ja va2s .c om * @param is * @param imgExtension * @return */ public static byte[] getImageContent(InputStream is, String imgExtension) { // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream(BYTE_BUFFER_SIZE); byte[] resultImageAsRawBytes = null; try { BufferedImage image = ImageIO.read(is); // W R I T E ImageIO.write(image, imgExtension, baos); // C L O S E baos.flush(); resultImageAsRawBytes = baos.toByteArray(); baos.close(); } catch (IOException ex) { LOGGER.warn(ex.getMessage()); } return resultImageAsRawBytes; }
From source file:de.qaware.chronix.converter.common.Compression.java
/** * Compresses the given byte[]// w w w . j a v a 2 s.co m * * @param decompressed - the byte[] to compress * @return the byte[] compressed */ public static byte[] compress(byte[] decompressed) { if (decompressed == null) { return new byte[] {}; } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(decompressed.length); OutputStream gzipOutputStream = null; try { gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(decompressed); gzipOutputStream.flush(); byteArrayOutputStream.flush(); } catch (IOException e) { LOGGER.error("Exception occurred while compressing gzip stream.", e); return null; } finally { IOUtils.closeQuietly(gzipOutputStream); IOUtils.closeQuietly(byteArrayOutputStream); } return byteArrayOutputStream.toByteArray(); }
From source file:com.reydentx.core.common.PhotoUtils.java
public static byte[] cropBufferedImage(boolean isPNG, BufferedImage originalImage, int x, int y, int width, int height) throws IOException { byte[] imageFinal = null; if (originalImage != null) { // step 1: crop BufferedImage thumbnail = Scalr.crop(originalImage, x, y, width, height, Scalr.OP_ANTIALIAS); // step 2. convert BufferedImage to byte[]. String extension = ""; if (isPNG == true) { extension = "png"; } else {/* ww w .java 2 s.com*/ extension = "jpg"; } ByteArrayOutputStream outstream = new ByteArrayOutputStream(); ImageIO.write(thumbnail, extension, outstream); outstream.flush(); imageFinal = outstream.toByteArray(); outstream.close(); } return imageFinal; }