List of usage examples for org.apache.commons.io.output ByteArrayOutputStream size
public synchronized int size()
From source file:com.streamsets.pipeline.lib.sdcipc.SdcIpcRequestFragmenter.java
static byte[] extract(InputStream is, ByteArrayOutputStream overflowBuffer, int limit) throws IOException { // the inputstream we get has been already stripped of the magic byte if first call byte[] message; if (copy(is, overflowBuffer, limit - overflowBuffer.size())) { // got rest of payload without exceeding the max message size if (overflowBuffer.size() == 0) { // there is no more payload message = null;// w ww . ja va 2s. co m } else { // extract the rest payload and prefix it with the magic byte byte[] data = overflowBuffer.toByteArray(); message = new byte[data.length + 1]; message[0] = JSON1_MAGIC_NUMBER; System.arraycopy(data, 0, message, 1, data.length); overflowBuffer.reset(); } } else { // got partial payload, exceeded the max message size byte[] data = overflowBuffer.toByteArray(); // find last full record in partial payload int lastEOL = findEndOfLastLineBeforeLimit(data, limit); if (lastEOL == -1) { throw new IOException(Utils.format("Maximum message size '{}' exceeded", limit)); } // extract payload up to last EOL and prefix with the magic byte message = new byte[lastEOL + 1]; message[0] = JSON1_MAGIC_NUMBER; System.arraycopy(data, 0, message, 1, lastEOL); // put back in the stream buffer the portion of the payload that did not make it to the message overflowBuffer.reset(); overflowBuffer.write(data, lastEOL, data.length - lastEOL); } return message; }
From source file:com.github.neoio.nio.util.NIOUtils.java
public static ByteBuffer readChannelToBuffer(ReadableByteChannel channel) throws NetIOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//ww w .j av a2s. com logger.debug("bytes read from channel: " + IOUtils.copy(Channels.newInputStream(channel), bos)); } catch (IOException e) { throw new NetIOException(e); } return ByteBuffer.wrap(ArrayUtils.subarray(bos.toByteArray(), 0, bos.size())); }
From source file:com.btoddb.trellis.common.serialization.JavaSerializer.java
@Override public int calculateSerializedSize(Serializable obj) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializeToOutputStream(obj, baos);//from w w w . j a v a 2 s. co m return baos.size(); }
From source file:com.ettrema.http.caldav.demo.TFolderResource.java
public Resource createNew(String newName, InputStream inputStream, Long length, String contentType) throws IOException { ByteArrayOutputStream bos = readStream(inputStream); log.debug("createNew: " + bos.size() + " - name: " + newName + " current child count: " + this.children.size()); TResource r = new TBinaryResource(this, newName, bos.toByteArray(), contentType); log.debug("new child count: " + this.children.size()); return r;/*w w w. jav a2 s. c o m*/ }
From source file:com.streamsets.pipeline.stage.origin.ipctokafka.TestSdcStreamFragmenter.java
@Test public void testCopy() throws IOException { // empty IS/*from w w w . j av a 2 s .c o m*/ InputStream is = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Assert.assertTrue(SdcStreamFragmenter.copy(is, baos, 1)); Assert.assertEquals(0, baos.size()); // IS size + 1 == limit is = new ByteArrayInputStream(new byte[10]); baos = new ByteArrayOutputStream(); Assert.assertTrue(SdcStreamFragmenter.copy(is, baos, 11)); Assert.assertEquals(10, baos.size()); // IS size == limit is = new ByteArrayInputStream(new byte[10]); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcStreamFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcStreamFragmenter.copy(is, baos, 1)); Assert.assertEquals(10, baos.size()); // IS size > limit is = new ByteArrayInputStream(new byte[15]); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcStreamFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcStreamFragmenter.copy(is, baos, 10)); Assert.assertEquals(15, baos.size()); // verify copy fidelity byte[] arr = new byte[15]; for (int i = 0; i < arr.length; i++) { arr[i] = (byte) i; } is = new ByteArrayInputStream(arr); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcStreamFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcStreamFragmenter.copy(is, baos, 10)); Assert.assertEquals(15, baos.size()); Assert.assertArrayEquals(arr, baos.toByteArray()); }
From source file:com.streamsets.pipeline.stage.origin.ipctokafka.TestSdcStreamFragmenter.java
@Test public void testExtract() throws IOException { // empty input InputStream is = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertNull(arr);//from www. j a va 2 s.co m Assert.assertEquals(0, baos.size()); // input size < limit is = new ByteArrayInputStream(new byte[] { 1, 2, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcStreamFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertEquals(0, baos.size()); // input size == limit, EOL at EOF is = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcStreamFragmenter.JSON1_MAGIC_NUMBER, 1, 2, 3, 4, '\n' }, arr); Assert.assertEquals(0, baos.size()); // input size == limit, EOL before EOF is = new ByteArrayInputStream(new byte[] { 1, 2, '\n', 4, 5 }); baos = new ByteArrayOutputStream(); arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcStreamFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertArrayEquals(new byte[] { 4, 5 }, baos.toByteArray()); // input size > limit is = new ByteArrayInputStream(new byte[] { 1, 2, '\n', 4, 5, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcStreamFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertArrayEquals(new byte[] { 4, 5 }, baos.toByteArray()); arr = SdcStreamFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcStreamFragmenter.JSON1_MAGIC_NUMBER, 4, 5, '\n' }, arr); Assert.assertEquals(0, baos.size()); }
From source file:com.orange.mmp.mvc.bundle.Controller.java
@SuppressWarnings("unchecked") @Override/* ww w. j ava 2 s . c o m*/ protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { int pathInfoStart = request.getRequestURI().indexOf(urlMapping); if (pathInfoStart < 0) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setContentLength(0); return null; } // Get user agent to obtain branchID String userAgent = request.getHeader(Constants.HTTP_HEADER_USERAGENT); Mobile mobile = new Mobile(); mobile.setUserAgent(userAgent); Mobile[] mobiles = (Mobile[]) DaoManagerFactory.getInstance().getDaoManager().getDao("mobile").find(mobile); String branchId = null;//Constants.DEFAULT_BRANCH_ID; if (mobiles != null && mobiles.length > 0) { branchId = mobiles[0].getBranchId(); } else { Branch branch = new Branch(); branch.setDefault(true); Branch[] branches = (Branch[]) DaoManagerFactory.getInstance().getDaoManager().getDao("branch") .find(branch); if (branches != null && branches.length > 0) branchId = branches[0].getId(); } String pathInfo = request.getRequestURI().substring(pathInfoStart + urlMapping.length()); String requestParts[] = pathInfo.split("/"); String widgetId = null; String resourceName = null; InputStream input = null; if (requestParts.length > 2) { widgetId = requestParts[1]; resourceName = pathInfo.substring(widgetId.length() + 2); } else { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.setContentLength(0); return null; } input = WidgetManager.getInstance().getWidgetResource(resourceName, widgetId, branchId); if (input == null) { response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setContentLength(0); } else { ByteArrayOutputStream resourceBuffer = new ByteArrayOutputStream(); OutputStream output = response.getOutputStream(); try { IOUtils.copy(input, resourceBuffer); response.setContentLength(resourceBuffer.size()); resourceBuffer.writeTo(output); } catch (IOException ioe) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.setContentLength(0); } finally { if (input != null) input.close(); if (output != null) output.close(); if (resourceBuffer != null) resourceBuffer.close(); } } return null; }
From source file:com.streamsets.pipeline.lib.sdcipc.TestSdcIpcRequestFragmenter.java
@Test public void testCopy() throws IOException { // empty IS//ww w . jav a2 s .c om InputStream is = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Assert.assertTrue(SdcIpcRequestFragmenter.copy(is, baos, 1)); Assert.assertEquals(0, baos.size()); // IS size + 1 == limit is = new ByteArrayInputStream(new byte[10]); baos = new ByteArrayOutputStream(); Assert.assertTrue(SdcIpcRequestFragmenter.copy(is, baos, 11)); Assert.assertEquals(10, baos.size()); // IS size == limit is = new ByteArrayInputStream(new byte[10]); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcIpcRequestFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcIpcRequestFragmenter.copy(is, baos, 1)); Assert.assertEquals(10, baos.size()); // IS size > limit is = new ByteArrayInputStream(new byte[15]); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcIpcRequestFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcIpcRequestFragmenter.copy(is, baos, 10)); Assert.assertEquals(15, baos.size()); // verify copy fidelity byte[] arr = new byte[15]; for (int i = 0; i < arr.length; i++) { arr[i] = (byte) i; } is = new ByteArrayInputStream(arr); baos = new ByteArrayOutputStream(); Assert.assertFalse(SdcIpcRequestFragmenter.copy(is, baos, 10)); Assert.assertEquals(10, baos.size()); Assert.assertTrue(SdcIpcRequestFragmenter.copy(is, baos, 10)); Assert.assertEquals(15, baos.size()); Assert.assertArrayEquals(arr, baos.toByteArray()); }
From source file:com.streamsets.pipeline.lib.sdcipc.TestSdcIpcRequestFragmenter.java
@Test public void testExtract() throws IOException { // empty input InputStream is = new ByteArrayInputStream(new byte[0]); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertNull(arr);//from w w w.ja va 2s.c o m Assert.assertEquals(0, baos.size()); // input size < limit is = new ByteArrayInputStream(new byte[] { 1, 2, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcIpcRequestFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertEquals(0, baos.size()); // input size == limit, EOL at EOF is = new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcIpcRequestFragmenter.JSON1_MAGIC_NUMBER, 1, 2, 3, 4, '\n' }, arr); Assert.assertEquals(0, baos.size()); // input size == limit, EOL before EOF is = new ByteArrayInputStream(new byte[] { 1, 2, '\n', 4, 5 }); baos = new ByteArrayOutputStream(); arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcIpcRequestFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertArrayEquals(new byte[] { 4, 5 }, baos.toByteArray()); // input size > limit is = new ByteArrayInputStream(new byte[] { 1, 2, '\n', 4, 5, '\n' }); baos = new ByteArrayOutputStream(); arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcIpcRequestFragmenter.JSON1_MAGIC_NUMBER, 1, 2, '\n' }, arr); Assert.assertArrayEquals(new byte[] { 4, 5 }, baos.toByteArray()); arr = SdcIpcRequestFragmenter.extract(is, baos, 5); Assert.assertArrayEquals(new byte[] { SdcIpcRequestFragmenter.JSON1_MAGIC_NUMBER, 4, 5, '\n' }, arr); Assert.assertEquals(0, baos.size()); }
From source file:de.dal33t.powerfolder.test.transfer.BandwidthLimitText.java
public void testLimiteds() { BandwidthLimiter bl = BandwidthLimiter.LAN_INPUT_BANDWIDTH_LIMITER; bl.setAvailable(10000);/*from w ww. j av a 2s. c o m*/ byte b[] = new byte[1000]; ByteArrayOutputStream bout = new ByteArrayOutputStream(); try (LimitedOutputStream out = new LimitedOutputStream(bl, bout)) { out.write(b); } catch (IOException e) { fail(e.toString()); } assertTrue("Wrong amount left/write", bl.getAvailable() == 10000 - b.length); assertTrue("Wrong amount written", bout.size() == b.length); ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); bl.setAvailable(10000); int read = 0; try (LimitedInputStream in = new LimitedInputStream(bl, bin)) { read = in.read(b); } catch (IOException e) { fail(e.toString()); } assertTrue("Wrong amount left/read", bl.getAvailable() == 10000 - read); assertTrue("Wrong amount read", bin.available() == b.length - read); }