List of usage examples for java.io ByteArrayOutputStream size
public synchronized int size()
From source file:org.dataconservancy.packaging.tool.ser.JenaModelSerializerTest.java
/** * Insures that serialization with a null language doesn't throw an exception * * @throws Exception/*from w w w .ja va 2 s. c o m*/ */ @Test public void testSerializeNullLang() throws Exception { // File out = File.createTempFile("JenaModelSerializerTest", ".out"); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Create a simple model to serialize Model m = ModelFactory.createDefaultModel(); m.add(m.createStatement(m.createResource(IPM_NS + "subject"), m.createProperty(IPM_NS, "predicate"), m.createResource(IPM_NS + "object"))); // Set a base URI underTest.setBase(IPM_NS); // Set a null language assertNull(underTest.getLang()); // Perform the test underTest.marshalOutputStream(m, out); // Assert that we have some output assertTrue(out.size() > 1); }
From source file:org.dataconservancy.packaging.tool.ser.JenaModelSerializerTest.java
/** * Insures that serialization with a null base URI doesn't throw an exception * * @throws Exception//from w ww . jav a 2s . c om */ @Test public void testSerializeNullBase() throws Exception { // File out = File.createTempFile("JenaModelSerializerTest", ".out"); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Create a simple model to serialize Model m = ModelFactory.createDefaultModel(); m.add(m.createStatement(m.createResource(IPM_NS + "subject"), m.createProperty(IPM_NS, "predicate"), m.createResource(IPM_NS + "object"))); // Set a null base URI assertNull(underTest.getBase()); // Set a language underTest.setLang("RDF/XML"); // Perform the test underTest.marshalOutputStream(m, out); // Assert that we have some output assertTrue(out.size() > 1); }
From source file:caarray.client.test.grid.GridApiFacade.java
public Integer copyFileContentsUtils(String api, List<CaArrayEntityReference> fileReferences, boolean compressed) throws Exception { if (fileReferences.isEmpty()) return 0; int total = 0; for (int i = 0; i < fileReferences.size(); i++) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); dataApiUtils.copyFileContentsToOutputStream(fileReferences.get(i), compressed, stream); total += stream.size(); }/*from w w w . j a v a 2s.com*/ return total; }
From source file:org.dataconservancy.packaging.tool.ser.JenaModelSerializerTest.java
/** * Insures serialization with a null language and null base URI doesn't throw an exception. * * @throws Exception/* ww w. ja v a2s .c o m*/ */ @Test public void testSerializeNullLangAndNullBase() throws Exception { // File out = File.createTempFile("JenaModelSerializerTest", ".out"); ByteArrayOutputStream out = new ByteArrayOutputStream(); // Create a simple model to serialize Model m = ModelFactory.createDefaultModel(); m.add(m.createStatement(m.createResource(IPM_NS + "subject"), m.createProperty(IPM_NS, "predicate"), m.createResource(IPM_NS + "object"))); // Set a null base URI assertNull(underTest.getBase()); // Set a null language assertNull(underTest.getLang()); // Perform the test underTest.marshalOutputStream(m, out); // Assert that we have some output assertTrue(out.size() > 1); }
From source file:org.kuali.kfs.kns.util.WebUtils.java
/** * A file that is not of type text/plain or text/html can be output through * the response using this method.//from ww w. jav a 2s.co m * * @param response * @param contentType * @param byteArrayOutputStream * @param fileName */ public static void saveMimeOutputStreamAsFile(HttpServletResponse response, String contentType, ByteArrayOutputStream byteArrayOutputStream, String fileName) throws IOException { // If there are quotes in the name, we should replace them to avoid issues. // The filename will be wrapped with quotes below when it is set in the header String updateFileName; if (fileName.contains("\"")) { updateFileName = fileName.replaceAll("\"", ""); } else { updateFileName = fileName; } // set response response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=\"" + updateFileName + "\""); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(byteArrayOutputStream.size()); // write to output OutputStream outputStream = response.getOutputStream(); byteArrayOutputStream.writeTo(response.getOutputStream()); outputStream.flush(); outputStream.close(); }
From source file:org.kuali.rice.kns.util.WebUtils.java
/** * A file that is not of type text/plain or text/html can be output through * the response using this method./*from w w w . j a v a2s. c o m*/ * * @param response * @param contentType * @param byteArrayOutputStream * @param fileName */ public static void saveMimeOutputStreamAsFile(HttpServletResponse response, String contentType, ByteArrayOutputStream byteArrayOutputStream, String fileName) throws IOException { // If there are quotes in the name, we should replace them to avoid issues. // The filename will be wrapped with quotes below when it is set in the header String updateFileName; if (fileName.contains("\"")) { updateFileName = fileName.replaceAll("\"", ""); } else { updateFileName = fileName; } // set response response.setContentType(contentType); response.setHeader("Content-disposition", "attachment; filename=\"" + updateFileName + "\""); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(byteArrayOutputStream.size()); // write to output OutputStream outputStream = response.getOutputStream(); byteArrayOutputStream.writeTo(response.getOutputStream()); outputStream.flush(); outputStream.close(); }
From source file:com.akamai.edgegrid.auth.EdgeGridV1Signer.java
/** * Get the SHA-256 hash of the POST body. * /*from w w w . ja v a 2 s. c om*/ * @param request the request. * @return the canonicalized data, and the possibly updated request. * @throws RequestSigningException */ protected CanonicalizerHelper getContentHash(HttpRequest request) throws RequestSigningException { String data = ""; HttpRequest updatedRequest = request; // only do hash for POSTs for this version if ("POST".equalsIgnoreCase(request.getRequestMethod())) { HttpContent content = request.getContent(); if (content != null) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); content.writeTo(bos); byte[] contentBytes = bos.toByteArray(); int lengthToHash = bos.size(); if (lengthToHash > maxBodySize) { LOGGER.warn(String.format( "Message body length '%d' is larger than the max '%d'. " + "Using '%d' bytes for computing the hash.", lengthToHash, maxBodySize, maxBodySize)); lengthToHash = maxBodySize; } else { if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("Content: %s", Base64.encodeBase64String(contentBytes))); } } byte[] digestBytes = getHash(contentBytes, 0, lengthToHash); if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format("Content hash: %s", Base64.encodeBase64String(digestBytes))); } // for non-retryable content, reset the content for downstream handlers if (!content.retrySupported()) { HttpContent newContent = new ByteArrayContent(content.getType(), contentBytes); updatedRequest = request.setContent(newContent); } data = Base64.encodeBase64String(digestBytes); } catch (IOException ioe) { throw new RequestSigningException("Failed to get content hash: failed to read content", ioe); } } } return new CanonicalizerHelper(data, updatedRequest); }
From source file:com.xpn.xwiki.plugin.image.ImagePlugin.java
/** * Reduces the size (i.e. the number of bytes) of an image by scaling its width and height and by reducing its * compression quality. This helps decreasing the time needed to download the image attachment. * //from w w w. ja v a2 s.c om * @param attachment the image to be shrunk * @param requestedWidth the desired image width; this value is taken into account only if it is greater than zero * and less than the current image width * @param requestedHeight the desired image height; this value is taken into account only if it is greater than zero * and less than the current image height * @param keepAspectRatio {@code true} to preserve the image aspect ratio even when both requested dimensions are * properly specified (in this case the image will be resized to best fit the rectangle with the * requested width and height), {@code false} otherwise * @param requestedQuality the desired compression quality * @param context the XWiki context * @return the modified image attachment * @throws Exception if shrinking the image fails */ private XWikiAttachment shrinkImage(XWikiAttachment attachment, int requestedWidth, int requestedHeight, boolean keepAspectRatio, float requestedQuality, XWikiContext context) throws Exception { Image image = this.imageProcessor.readImage(attachment.getContentInputStream(context)); // Compute the new image dimension. int currentWidth = image.getWidth(null); int currentHeight = image.getHeight(null); int[] dimensions = reduceImageDimensions(currentWidth, currentHeight, requestedWidth, requestedHeight, keepAspectRatio); float quality = requestedQuality; if (quality < 0) { // If no scaling is needed and the quality parameter is not specified, return the original image. if (dimensions[0] == currentWidth && dimensions[1] == currentHeight) { return attachment; } quality = this.defaultQuality; } // Scale the image to the new dimensions. RenderedImage shrunkImage = this.imageProcessor.scaleImage(image, dimensions[0], dimensions[1]); // Write the shrunk image to a byte array output stream. ByteArrayOutputStream bout = new ByteArrayOutputStream(); this.imageProcessor.writeImage(shrunkImage, attachment.getMimeType(context), quality, bout); // Create an image attachment for the shrunk image. XWikiAttachment thumbnail = (XWikiAttachment) attachment.clone(); thumbnail.setContent(new ByteArrayInputStream(bout.toByteArray()), bout.size()); return thumbnail; }
From source file:com.adaptris.util.text.mime.MultiPartOutput.java
private void inMemoryWrite(OutputStream out) throws MessagingException, IOException { MimeMultipart multipart = new MimeMultipart(); ByteArrayOutputStream partOut = new ByteArrayOutputStream(); mimeHeader.setHeader(HEADER_CONTENT_TYPE, multipart.getContentType()); // Write the part out to the stream first. for (KeyedBodyPart kbp : parts) { multipart.addBodyPart(kbp.getData()); }/*from w ww . ja v a 2s . c o m*/ multipart.writeTo(partOut); mimeHeader.setHeader(HEADER_CONTENT_LENGTH, String.valueOf(partOut.size())); writeHeaders(mimeHeader, out); out.write(partOut.toByteArray()); }
From source file:com.norconex.commons.lang.io.ByteArrayOutputStreamTest.java
@Test public void testByteArrayOutputStream() throws IOException { String val1 = "0123456789"; String val2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte[] b = null; ByteArrayOutputStream out = new ByteArrayOutputStream(5); String enc = CharEncoding.US_ASCII; // test nothing written b = new byte[5]; Assert.assertEquals("no-write-yet-getByte", -1, out.getByte(5)); Assert.assertEquals("no-write-yet-getBytes", -1, out.getBytes(b, 0)); // test getByte() out.write(val1.getBytes(enc)); Assert.assertEquals("getByte-0", '0', (char) out.getByte(0)); Assert.assertEquals("getByte-5", '5', (char) out.getByte(5)); Assert.assertEquals("getByte-9", '9', (char) out.getByte(9)); Assert.assertEquals("getByte-10", -1, out.getByte(10)); Assert.assertEquals("val1-count", 10, out.size()); // test getBytes() b = new byte[6]; out.getBytes(b, 0);// www .ja v a 2 s .c o m Assert.assertEquals("getBytes-012345", "012345", new String(b, enc)); b = new byte[3]; out.getBytes(b, 6); Assert.assertEquals("getBytes-678", "678", new String(b, enc)); b = new byte[3]; out.getBytes(b, 6); Assert.assertEquals("getBytes-678", "678", new String(b, enc)); b = new byte[12]; int read = out.getBytes(b, 8); Assert.assertEquals("getBytes-89", "89", new String(b, 0, read, enc)); out.write(val2.getBytes(enc)); out.getBytes(b, 8); Assert.assertEquals("getBytes-89ABCDEFGHIJ", "89ABCDEFGHIJ", new String(b, enc)); // toByteArray() Assert.assertEquals("toByteArray", val1 + val2, new String(out.toByteArray(), enc)); out.close(); }