Java Code Examples for org.apache.commons.io.output.ByteArrayOutputStream#write()
The following examples show how to use
org.apache.commons.io.output.ByteArrayOutputStream#write() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphServiceController.java From console-java-connect-sample with MIT License | 6 votes |
/** * Converts an inputStream to a byte array. The input stream should be a stream of profile * picture bytes that comes in the response to a GET request on the Microsoft Graph API * * @param inputStream * @return */ private byte[] inputStreamToByteArray(InputStream inputStream) throws IOException { byte[] pictureBytes = null; try { BufferedInputStream bufferedInputStream = (BufferedInputStream) inputStream; byte[] buff = new byte[8000]; ByteArrayOutputStream bao = new ByteArrayOutputStream(); int bytesRead = 0; //This seems to be executing on the main thread!!! while ((bytesRead = bufferedInputStream.read(buff)) != -1) { bao.write(buff, 0, bytesRead); } pictureBytes = bao.toByteArray(); bao.close(); } catch (IOException ex) { DebugLogger.getInstance().writeLog(Level.SEVERE, "Attempting to read buffered network resource", ex); } return pictureBytes; }
Example 2
Source File: GraphServiceController.java From android-java-connect-sample with MIT License | 6 votes |
/** * Converts a BufferedInputStream to a byte array * * @param inputStream * @param bufferLength * @return * @throws IOException */ private byte[] convertBufferToBytes(BufferedInputStream inputStream, int bufferLength) throws IOException { if (inputStream == null) return null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[bufferLength]; int x = inputStream.read(buffer, 0, bufferLength); Log.i("GraphServiceController", "bytes read from picture input stream " + String.valueOf(x)); int n = 0; try { while ((n = inputStream.read(buffer, 0, bufferLength)) >= 0) { outputStream.write(buffer, 0, n); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } outputStream.close(); return outputStream.toByteArray(); }
Example 3
Source File: CreateWARCWritableFunction.java From flink-crawler with Apache License 2.0 | 6 votes |
private void outputWARCResourceRecord(Collector<Tuple2<NullWritable, WARCWritable>> collector, FetchResultUrl fetchResultUrl) throws IOException { byte[] content = fetchResultUrl.getContent(); StringBuffer buffer = new StringBuffer(); buffer.append("WARC/1.0\r\n"); buffer.append("WARC-Type: resource\r\n"); buffer.append(String.format("WARC-Target-URI: %s\r\n", fetchResultUrl.getFetchedUrl())); buffer.append(String.format("WARC-Date: %s\r\n", _dateFormat.format(new Date()))); buffer.append(String.format("WARC-Record-ID: <%s>\r\n", fetchResultUrl.getUrl())); buffer.append(String.format("Content-Type: %s\r\n", fetchResultUrl.getContentType())); buffer.append(String.format("Content-Length: %d\r\n", content.length)); buffer.append("\r\n"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(buffer.toString().getBytes("UTF-8")); bos.write(content); bos.write("\r\n\r\n\r\n".getBytes("UTF-8")); DataInputStream stream = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); bos.close(); WARCRecord record = new WARCRecord(stream); WARCWritable writable = new WARCWritable(record); collector.collect(new Tuple2<NullWritable, WARCWritable>(NullWritable.get(), writable)); stream.close(); }
Example 4
Source File: MPCoreUtils.java From dx-java with MIT License | 6 votes |
/** * Static method that transform an Input Stream to a String object, returns an empty string if InputStream is null. * * @param is Input Stream to process * @return a String with the stream content * @throws MPException */ public static String inputStreamToString(InputStream is) throws MPException { String value = ""; if (is != null) { try { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { result.write(buffer, 0, length); } value = result.toString("UTF-8"); } catch (Exception ex) { throw new com.mercadopago.exceptions.MPException(ex); } } return value; }
Example 5
Source File: MacroHelper.java From warp10-platform with Apache License 2.0 | 6 votes |
public static WarpScriptStackFunction wrap(String name, InputStream in, boolean secure) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; try { while(true) { int len = in.read(buf); if (len < 0) { break; } baos.write(buf, 0, len); } in.close(); String mc2 = new String(baos.toByteArray(), StandardCharsets.UTF_8); return wrap(name, mc2, secure); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
Example 6
Source File: DirectoryUtil.java From warp10-platform with Apache License 2.0 | 5 votes |
private static long computeHash(long k0, long k1, long timestamp, List<String> classSelectors, List<Map<String,String>> labelsSelectors) { // // Create a ByteArrayOutputStream into which the content will be dumped // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Add timestamp try { baos.write(Longs.toByteArray(timestamp)); if (null != classSelectors) { for (String classSelector: classSelectors) { baos.write(classSelector.getBytes(StandardCharsets.UTF_8)); } } if (null != labelsSelectors) { for (Map<String, String> map: labelsSelectors) { TreeMap<String,String> tm = new TreeMap<String, String>(); tm.putAll(map); for (Entry<String,String> entry: tm.entrySet()) { baos.write(entry.getKey().getBytes(StandardCharsets.UTF_8)); baos.write(entry.getValue().getBytes(StandardCharsets.UTF_8)); } } } } catch (IOException ioe) { return 0L; } // Compute hash byte[] data = baos.toByteArray(); long hash = SipHashInline.hash24(k0, k1, data, 0, data.length); return hash; }
Example 7
Source File: TreasuryXTeeServiceImplTest.java From j-road with Apache License 2.0 | 5 votes |
@Test public void sendDocumentTestV1() throws XRoadServiceConsumptionException { try { InputStream is = getClass().getClassLoader().getResourceAsStream("makseM3.bdoc"); ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write(is); is.close(); os.close(); treasuryXTeeService.sendDocument(UUID.randomUUID().toString(), TYPE, os.toByteArray()); } catch (Exception e) { e.printStackTrace(); Assert.fail("TreasuryXTeeServiceImplTest.sendDocumentTestV1: exception occurred"); } }
Example 8
Source File: UDPConn.java From PacketProxy with Apache License 2.0 | 4 votes |
public void put(byte[] data, int offset, int length) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); bout.write(data, offset, length); put(bout.toByteArray()); bout.close(); }
Example 9
Source File: S3TransporterTest.java From bender with Apache License 2.0 | 4 votes |
@Test public void testCompressedBuffer() throws TransportException, IllegalStateException, IOException { /* * Create mock client, requets, and replies */ AmazonS3Client mockClient = getMockClient(); /* * Capture the InputStream into a ByteArrayOutputStream before the Transport thread closes the * InputStream and makes it unavailable for reading. */ ByteArrayOutputStream captured = new ByteArrayOutputStream(); Answer answer = new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { UploadPartRequest req = invocation.getArgumentAt(0, UploadPartRequest.class); captured.write(req.getInputStream()); return new UploadPartResult(); } }; Mockito.doAnswer(answer).when(mockClient).uploadPart(any(UploadPartRequest.class)); /* * Fill buffer with mock data */ S3TransportBuffer buffer = new S3TransportBuffer(1000, true, new S3TransportSerializer()); InternalEvent mockIevent = mock(InternalEvent.class); doReturn("foo").when(mockIevent).getSerialized(); /* * Create transport */ Map<String, MultiPartUpload> multiPartUploads = new HashMap<String, MultiPartUpload>(0); S3Transport transport = new S3Transport(mockClient, "bucket", "basepath", true, multiPartUploads); /* * Do actual test */ buffer.add(mockIevent); LinkedHashMap<String, String> partitions = new LinkedHashMap<String, String>(); partitions.put(S3Transport.FILENAME_KEY, "a_filename"); ArgumentCaptor<UploadPartRequest> argument = ArgumentCaptor.forClass(UploadPartRequest.class); buffer.close(); transport.sendBatch(buffer, partitions, new TestContext()); verify(mockClient).uploadPart(argument.capture()); /* * Check results */ assertEquals("bucket", argument.getValue().getBucketName()); assertEquals("basepath/a_filename.bz2", argument.getValue().getKey()); assertEquals(1, argument.getValue().getPartNumber()); assertEquals(40, argument.getValue().getPartSize()); assertEquals("123", argument.getValue().getUploadId()); /* * Convert the actual InputStream from the client into a ByteArrayOutputStream which can be read * and verified. */ byte[] actualBytes = captured.toByteArray(); byte[] expectedBytes = {66, 90, 104, 57, 49, 65, 89, 38, 83, 89, 118, -10, -77, -27, 0, 0, 0, -63, 0, 0, 16, 1, 0, -96, 0, 48, -52, 12, -62, 12, 46, -28, -118, 112, -95, 32, -19, -19, 103, -54}; assertArrayEquals(expectedBytes, actualBytes); }
Example 10
Source File: S3TransporterTest.java From bender with Apache License 2.0 | 4 votes |
@Test public void testCompressed() throws TransportException, IllegalStateException, IOException { /* * Create mock client, requets, and replies */ AmazonS3Client mockClient = getMockClient(); /* * Capture the InputStream into a ByteArrayOutputStream before the Transport thread closes the * InputStream and makes it unavailable for reading. */ ByteArrayOutputStream captured = new ByteArrayOutputStream(); Answer answer = new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { UploadPartRequest req = invocation.getArgumentAt(0, UploadPartRequest.class); captured.write(req.getInputStream()); return new UploadPartResult(); } }; Mockito.doAnswer(answer).when(mockClient).uploadPart(any(UploadPartRequest.class)); /* * Fill buffer with mock data */ S3TransportBuffer buffer = new S3TransportBuffer(1000, false, new S3TransportSerializer()); InternalEvent mockIevent = mock(InternalEvent.class); doReturn("foo").when(mockIevent).getSerialized(); /* * Create transport */ Map<String, MultiPartUpload> multiPartUploads = new HashMap<String, MultiPartUpload>(0); S3Transport transport = new S3Transport(mockClient, "bucket", "basepath", true, multiPartUploads); /* * Do actual test */ buffer.add(mockIevent); LinkedHashMap<String, String> partitions = new LinkedHashMap<String, String>(); partitions.put(S3Transport.FILENAME_KEY, "a_filename"); ArgumentCaptor<UploadPartRequest> argument = ArgumentCaptor.forClass(UploadPartRequest.class); buffer.close(); transport.sendBatch(buffer, partitions, new TestContext()); verify(mockClient).uploadPart(argument.capture()); /* * Check results */ assertEquals("bucket", argument.getValue().getBucketName()); assertEquals("basepath/a_filename.bz2", argument.getValue().getKey()); assertEquals(1, argument.getValue().getPartNumber()); assertEquals(40, argument.getValue().getPartSize()); assertEquals("123", argument.getValue().getUploadId()); /* * Convert the actual InputStream from the client into a ByteArrayOutputStream which can be read * and verified. */ byte[] actualBytes = captured.toByteArray(); byte[] expectedBytes = {66, 90, 104, 57, 49, 65, 89, 38, 83, 89, 118, -10, -77, -27, 0, 0, 0, -63, 0, 0, 16, 1, 0, -96, 0, 48, -52, 12, -62, 12, 46, -28, -118, 112, -95, 32, -19, -19, 103, -54}; assertArrayEquals(expectedBytes, actualBytes); }
Example 11
Source File: CSARParser.java From NFVO with Apache License 2.0 | 4 votes |
private void readFiles(InputStream csar_file) throws IOException, NotFoundException { ZipEntry entry; this.scripts.clear(); this.folderNames.clear(); this.template = new ByteArrayOutputStream(); this.metadata = new ByteArrayOutputStream(); try (ZipInputStream zipStream = new ZipInputStream(csar_file)) { while ((entry = zipStream.getNextEntry()) != null) { if (!entry.isDirectory()) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int count; byte[] buffer = new byte[1024]; while ((count = zipStream.read(buffer)) != -1) { baos.write(buffer, 0, count); } String fileName = entry.getName(); if (fileName.toLowerCase().endsWith(".meta")) { this.metadata = baos; } else if (fileName.toLowerCase().endsWith(".yaml")) { if (fileName.toLowerCase().endsWith("metadata.yaml")) { this.vnfMetadata = baos; } else { this.template = baos; } } else { Script script = new Script(); String[] splittedName = fileName.split("/"); if (splittedName.length > 2) { String scriptName = splittedName[1] + "!_!" + splittedName[splittedName.length - 1]; folderNames.add(splittedName[1]); script.setName(scriptName); } else script.setName(splittedName[splittedName.length - 1]); script.setPayload(baos.toByteArray()); this.scripts.add(script); } } } } if (this.metadata == null) { throw new NotFoundException("CSARParser: You have to include the TOSCA.meta"); } if (this.vnfMetadata == null) { throw new NotFoundException("CSARParser: You have to include the Metadata.yaml"); } if (this.template == null) { throw new NotFoundException("CSARParser: No NSD or VNFD Template found"); } }