List of usage examples for java.io ByteArrayOutputStream size
public synchronized int size()
From source file:freenet.support.compress.Bzip2Compressor.java
@Override public int decompress(byte[] dbuf, int i, int j, byte[] output) throws CompressionOutputSizeException { // Didn't work with Inflater. // FIXME fix sometimes to use Inflater - format issue? ByteArrayInputStream bais = new ByteArrayInputStream(dbuf, i, j); ByteArrayOutputStream baos = new ByteArrayOutputStream(output.length); int bytes = 0; try {/* w ww. j ava 2 s .co m*/ decompress(bais, baos, output.length, -1); bytes = baos.size(); } catch (IOException e) { // Impossible throw new Error("Got IOException: " + e.getMessage(), e); } byte[] buf = baos.toByteArray(); System.arraycopy(buf, 0, output, 0, bytes); return bytes; }
From source file:com.tassadar.multirommgr.installfragment.UbuntuChannel.java
public boolean loadDeviceImages(String device_name, Device dev) throws Exception { String path = m_devices.get(device_name); if (path == null || path.isEmpty()) throw new Exception("Device " + device_name + " was not found in this channel!"); Log.d(TAG, "Loading index " + path); ByteArrayOutputStream out = new ByteArrayOutputStream(32768); try {//w ww . ja v a 2 s . c o m if (!Utils.downloadFile(dev.getUbuntuBaseUrl() + path, out, null, true) || out.size() == 0) return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } m_images = new TreeMap<Integer, UbuntuImage>(); Object rawObject = new JSONTokener(out.toString()).nextValue(); if (!(rawObject instanceof JSONObject)) { Log.e(TAG, "Malformed manifest format!"); return false; } JSONArray images = ((JSONObject) rawObject).getJSONArray("images"); for (int i = 0; i < images.length(); ++i) { JSONObject img = images.getJSONObject(i); // We only need full images because we do only clean install if (!img.getString("type").equals("full")) continue; UbuntuImage uimg = new UbuntuImage(img); m_images.put(uimg.version, uimg); } return true; }
From source file:org.apache.olingo.fit.OpenType.java
private Response replaceServiceName(final Response response) { try {//from www .j a v a2 s .c o m final String content = IOUtils.toString((InputStream) response.getEntity(), Constants.ENCODING) .replaceAll("Static\\.svc", "OpenType.svc"); final Response.ResponseBuilder builder = Response.status(response.getStatus()); for (String headerName : response.getHeaders().keySet()) { for (Object headerValue : response.getHeaders().get(headerName)) { builder.header(headerName, headerValue); } } final InputStream toBeStreamedBack = IOUtils.toInputStream(content, Constants.ENCODING); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(toBeStreamedBack, baos); IOUtils.closeQuietly(toBeStreamedBack); builder.header("Content-Length", baos.size()); builder.entity(new ByteArrayInputStream(baos.toByteArray())); return builder.build(); } catch (Exception e) { return response; } }
From source file:org.appverse.web.framework.backend.frontfacade.json.controllers.CustomMappingJacksonHttpMessageConverter.java
@Override protected Long getContentLength(Object o, MediaType contentType) { try {// w w w . j a v a 2s . com ByteArrayOutputStream baos = new ByteArrayOutputStream(); JsonGenerator baosjsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(baos); this.objectMapper.writeValue(baosjsonGenerator, o); ByteArrayOutputStream os = (ByteArrayOutputStream) baosjsonGenerator.getOutputTarget(); return new Long(os.size()); } catch (Exception e) { return new Long(0); } }
From source file:org.apache.gobblin.crypto.GobblinEncryptionProviderTest.java
@Test public void testCanBuildAes() throws IOException { Map<String, Object> properties = new HashMap<>(); properties.put(EncryptionConfigParser.ENCRYPTION_ALGORITHM_KEY, "aes_rotating"); properties.put(EncryptionConfigParser.ENCRYPTION_KEYSTORE_PATH_KEY, getClass().getResource("/encryption_provider_test_keystore").toString()); properties.put(EncryptionConfigParser.ENCRYPTION_KEYSTORE_PASSWORD_KEY, "abcd"); StreamCodec c = EncryptionFactory.buildStreamCryptoProvider(properties); Assert.assertNotNull(c);//from w w w . java 2 s. co m byte[] toEncrypt = "Hello!".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream cipherOut = new ByteArrayOutputStream(); OutputStream cipherStream = c.encodeOutputStream(cipherOut); cipherStream.write(toEncrypt); cipherStream.close(); Assert.assertTrue(cipherOut.size() > 0, "Expected to be able to write ciphertext!"); }
From source file:com.conwet.silbops.model.JSONvsRMIPerfT.java
@SuppressWarnings("unchecked") public static void sizeSeveralJSONWithTypes(JSONizable[] objects, String feature, String type) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream stream;// www .jav a 2 s.co m FeatureMap featureMap = mock(FeatureMap.class); when(featureMap.getPrefix(feature)).thenReturn("ps"); try { stream = new ObjectOutputStream(baos); for (JSONizable object : objects) { JSONObject payLoad = new JSONObject(); payLoad.put(type, object.toJSON()); ImmutableMessage msg = new ImmutableMessage(feature, type, payLoad, "0"); String codedMsg = msg.toJSONObject(featureMap).toJSONString(); stream.writeObject(codedMsg); } } catch (Exception ex) { ex.printStackTrace(); } int size = baos.size(); int sizePerMessage = size / objects.length; System.out.println(" Size indicating type " + objects.length + " messages - JSON: " + size + " bytes (" + sizePerMessage + " bytes/msg)"); }
From source file:mitm.common.scheduler.TaskSchedulerTest.java
@Test public void testInputStreamTimeoutTaskCancelTask() throws IOException { TaskScheduler scheduler = new TaskScheduler("test"); InputStream input = new SlowInputStream(50); Task task = new InputStreamTimeoutTask(input, "testInputStreamTimeoutTaskCancelTask"); scheduler.addTask(task, 10);/* ww w.ja v a 2s .c om*/ assertFalse(task.isCanceled()); task.cancel(); assertTrue(task.isCanceled()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(input, bos); assertEquals(50, bos.size()); }
From source file:mitm.common.scheduler.TaskSchedulerTest.java
@Test public void testInputStreamTimeoutTaskCancelScheduler() throws IOException { TaskScheduler scheduler = new TaskScheduler("test"); InputStream input = new SlowInputStream(50); Task task = new InputStreamTimeoutTask(input, "testInputStreamTimeoutTaskCancelScheduler"); scheduler.addTask(task, 10);/* w w w.j a v a2 s .c o m*/ assertFalse(scheduler.isCanceled()); scheduler.cancel(); assertTrue(scheduler.isCanceled()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(input, bos); assertEquals(50, bos.size()); }
From source file:com.id.get.expand.spring.MarshallingView.java
@Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { Object toBeMarshalled = locateToBeMarshalled(model); if (toBeMarshalled == null) { throw new ServletException("Unable to locate object to be marshalled in model: " + model); }/*from w ww.j a v a2s.co m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); this.marshaller.marshal(toBeMarshalled, new StreamResult(baos)); setResponseContentType(request, response); response.setContentLength(baos.size()); baos.writeTo(response.getOutputStream()); }
From source file:gr.abiss.calipso.fs.FilePersistenceService.java
public default FileDTO scaleFile(BufferedImage img, String contentType, int maxWidth, int maxHeight) throws IOException { BufferedImage scaled = Scalr.resize(img, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH, maxWidth, maxHeight, Scalr.OP_ANTIALIAS);//from ww w. j a v a2s .c o m ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(scaled, getImageIoFormat(contentType), os); FileDTO scaledFile = new FileDTO.Builder().contentLength(os.size()).contentType(contentType) .in(new ByteArrayInputStream(os.toByteArray())).build(); os.close(); return scaledFile; }