List of usage examples for java.io ByteArrayOutputStream size
public synchronized int size()
From source file:it.govpay.web.handler.MessageLoggingHandlerUtils.java
@SuppressWarnings("unchecked") public static boolean logToSystemOut(SOAPMessageContext smc, String tipoServizio, int versioneServizio, Logger log) {/*w w w .java 2 s . c o m*/ Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); GpContext ctx = null; Message msg = new Message(); SOAPMessage message = smc.getMessage(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { message.writeTo(baos); msg.setContent(baos.toByteArray()); } catch (Exception e) { log.error("Exception in handler: " + e); } Map<String, List<String>> httpHeaders = null; if (outboundProperty.booleanValue()) { ctx = GpThreadLocal.get(); httpHeaders = (Map<String, List<String>>) smc.get(MessageContext.HTTP_RESPONSE_HEADERS); msg.setType(MessageType.RESPONSE_OUT); ctx.getContext().getResponse().setOutDate(new Date()); ctx.getContext().getResponse().setOutSize(Long.valueOf(baos.size())); } else { try { ctx = new GpContext(smc, tipoServizio, versioneServizio); ThreadContext.put("op", ctx.getTransactionId()); GpThreadLocal.set(ctx); } catch (Exception e) { log.error(e.getMessage(), e); return false; } httpHeaders = (Map<String, List<String>>) smc.get(MessageContext.HTTP_REQUEST_HEADERS); msg.setType(MessageType.REQUEST_IN); msg.setContentType(((HttpServletRequest) smc.get(MessageContext.SERVLET_REQUEST)).getContentType()); ctx.getContext().getRequest().setInDate(new Date()); ctx.getContext().getRequest().setInSize(Long.valueOf(baos.size())); } if (httpHeaders != null) { for (String key : httpHeaders.keySet()) { if (httpHeaders.get(key) != null) { if (key == null) msg.addHeader(new Property("Status-line", httpHeaders.get(key).get(0))); else if (httpHeaders.get(key).size() == 1) msg.addHeader(new Property(key, httpHeaders.get(key).get(0))); else msg.addHeader(new Property(key, ArrayUtils.toString(httpHeaders.get(key)))); } } } ctx.log(msg); return true; }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
public static void sizeExternalizeSeveralWithoutTypes(Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w w w.j a v a 2 s. c o m ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { object.writeExternal(out); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size no indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:com.github.nlloyd.hornofmongo.MongoScope.java
public static Long bsonsize(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws IOException { DBObject bsonObj = (DBObject) BSONizer.convertJStoBSON(args[0], true); Long size = new Long(0); if (bsonObj != null) { BasicOutputBuffer byteBuffer = new BasicOutputBuffer(); DefaultDBEncoder.FACTORY.create().writeObject(byteBuffer, bsonObj); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); byteBuffer.pipe(byteStream);//from w ww . ja v a2 s . com size = new Long(byteStream.size()); } return size; }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
public static void sizeSeveralJSONWithoutTypes(JSONizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream stream;/*from ww w . ja v a2s . co m*/ try { stream = new ObjectOutputStream(baos); for (JSONizable object : objects) { String json = object.toJSONString(); stream.writeObject(json); } } catch (Exception ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size no indicating type " + objects.length + " messages - JSON: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
public static void sizeExternalizeSeveralWithType(String type, Externalizable[] objects) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from ww w. ja va 2s . co m*/ ObjectOutputStream out = new ObjectOutputStream(baos); for (Externalizable object : objects) { Message msg = new MessageImpl(type, object, null); out.writeObject(msg); } out.close(); } catch (IOException ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size indicating types " + objects.length + " messages - RMI: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:Main.java
public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) { Bitmap bmp = null;/*from w w w. j a v a2s . c om*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos); LOG("getCompressBitmap result: " + result); try { if (file != null) { if (result) { byte[] bt = bos.toByteArray(); FileOutputStream fos = new FileOutputStream(file); fos.write(bt); fos.close(); LOG("file.length(): " + file.length()); if (file.length() > fileSize) { bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize); } else { bmp = BitmapFactory.decodeFile(file.getPath()); } } } else { bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size()); } bos.close(); } catch (Exception e) { LOG("getCompressBitmap result: e" + e.toString()); e.printStackTrace(); return null; } return bmp; }
From source file:org.openlmis.report.exporter.JasperReportExporter.java
/** * Handles exporting of jasper print to pdf format * @param jasperPrint//from w w w .j av a 2s . c om * @param response * @param byteArrayOutputStream * @return */ private static HttpServletResponse exportPdf(JasperPrint jasperPrint, String outputFileName, HttpServletResponse response, ByteArrayOutputStream byteArrayOutputStream) { JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream); try { exporter.exportReport(); } catch (JRException e) { throw new RuntimeException(e); } String fileName = outputFileName.isEmpty() ? OPENLMIS_REPORT_PDF : outputFileName + ".pdf"; response.setHeader(CONTENT_DISPOSITION, INLINE_FILENAME + fileName); response.setContentType(Constants.MEDIA_TYPE_PDF); response.setContentLength(byteArrayOutputStream.size()); return response; }
From source file:com.krawler.common.util.ByteUtil.java
private static byte[] getContent(InputStream is, int length, int sizeHint, long sizeLimit) throws IOException { if (length == 0) return new byte[0]; ByteArrayOutputStream baos = null; try {/* w w w .j av a 2 s. c om*/ if (length > 0 && (sizeHint > length || sizeHint < 0)) sizeHint = length; baos = new ByteArrayOutputStream(Math.max(sizeHint, 0)); byte[] buffer = new byte[8192]; int num, limit = length > 0 ? Math.min(buffer.length, length - baos.size()) : buffer.length; while ((num = is.read(buffer, 0, limit)) != -1) { baos.write(buffer, 0, num); if (sizeLimit > 0 && baos.size() > sizeLimit) throw new IOException("stream too large"); if (length > 0 && baos.size() >= length) break; limit = length > 0 ? Math.min(buffer.length, length - baos.size()) : buffer.length; } return baos.toByteArray(); } finally { closeStream(is); } }
From source file:com.googlecode.jcimd.PacketSerializer.java
private static Packet doDeserializePacket(InputStream inputStream, int maxMessageSize, boolean useChecksum, Log logger) throws IOException { ByteArrayOutputStream temp = new ByteArrayOutputStream(); int b;//ww w . j a v a 2 s.c o m while ((b = inputStream.read()) != END_OF_STREAM) { // Any data transmitted between packets SHALL be ignored. if (b == STX) { temp.write(b); break; } } if (b != STX) { //throw new SoftEndOfStreamException(); throw new IOException("End of stream reached and still no <STX> byte"); } // Read the input stream until ETX while ((b = inputStream.read()) != END_OF_STREAM) { temp.write(b); if (b == ETX) { break; } if (temp.size() >= maxMessageSize) { // Protect from buffer overflow throw new IOException( "Buffer overflow reached at " + temp.size() + " byte(s) and still no <ETX> byte"); } } if (b != ETX) { throw new IOException("End of stream reached and still no <ETX> byte"); } // Parse contents of "temp" (it contains the entire CIMD message // including STX and ETX bytes). byte bytes[] = temp.toByteArray(); if (logger.isTraceEnabled()) { logger.trace("Received " + bytes.length + " byte(s)"); } if (useChecksum) { // Read two (2) bytes, just before the ETX byte. StringBuilder buffer = new StringBuilder(2); buffer.append((char) bytes[bytes.length - 3]); buffer.append((char) bytes[bytes.length - 2]); try { int checksum = Integer.valueOf(buffer.toString(), 16); int expectedChecksum = calculateCheckSum(bytes, 0, bytes.length - 3); if (checksum != expectedChecksum) { throw new IOException("Checksum error: expecting " + expectedChecksum + " but got " + checksum); } } catch (NumberFormatException e) { throw new IOException("Checksum error: expecting HEX digits, but got " + buffer); } } // Deserialize bytes, minus STX, CC (check sum), and ETX. int end = useChecksum ? bytes.length - 3 : bytes.length - 1; Packet packet = deserializeFromByteArray(bytes, 1, end); if (logger.isDebugEnabled()) { logger.debug("Received " + packet); } return packet; }
From source file:com.zimbra.cs.imap.ImapMessage.java
static Pair<Long, InputStream> getContent(MailItem item) throws ServiceException { if (item instanceof Message) { return new Pair<Long, InputStream>(item.getSize(), item.getContentStream()); } else if (item instanceof Contact) { try {//from w ww . ja v a 2 s. com VCard vcard = VCard.formatContact((Contact) item); QCodec qcodec = new QCodec(); qcodec.setEncodeBlanks(true); StringBuilder header = new StringBuilder(); header.append("Subject: ").append(qcodec.encode(vcard.fn, MimeConstants.P_CHARSET_UTF8)) .append(ImapHandler.LINE_SEPARATOR); synchronized (GMT_DATE_FORMAT) { header.append("Date: ").append(GMT_DATE_FORMAT.format(new Date(item.getDate()))) .append(ImapHandler.LINE_SEPARATOR); } header.append("Content-Type: text/x-vcard; charset=\"utf-8\"").append(ImapHandler.LINE_SEPARATOR); header.append("Content-Transfer-Encoding: 8bit").append(ImapHandler.LINE_SEPARATOR); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(header.toString().getBytes(MimeConstants.P_CHARSET_ASCII)); baos.write(ImapHandler.LINE_SEPARATOR_BYTES); baos.write(vcard.getFormatted().getBytes(MimeConstants.P_CHARSET_UTF8)); return new Pair<Long, InputStream>((long) baos.size(), new SharedByteArrayInputStream(baos.toByteArray())); } catch (Exception e) { throw ServiceException.FAILURE("problems serializing contact " + item.getId(), e); } } else { return EMPTY_CONTENT; } }