Java Code Examples for org.apache.commons.io.output.ByteArrayOutputStream#close()

The following examples show how to use org.apache.commons.io.output.ByteArrayOutputStream#close() . 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 vote down vote up
/**
 * 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 vote down vote up
/**
 * 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 vote down vote up
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: S3TransportBufferTest.java    From bender with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdd() throws IOException, TransportException {
  S3TransportSerializer serializer = mock(S3TransportSerializer.class);
  doReturn("foo".getBytes()).when(serializer).serialize(any(InternalEvent.class));
  S3TransportBuffer buffer = new S3TransportBuffer(5, false, serializer);

  InternalEvent mockEvent = mock(InternalEvent.class);
  buffer.add(mockEvent);

  ByteArrayOutputStream baos = buffer.getInternalBuffer();
  baos.close();

  String actual = new String(baos.toByteArray());
  assertEquals("foo", actual);
  assertEquals(false, buffer.isEmpty());
}
 
Example 5
Source File: GPGFileEncryptorTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Encrypt a test string with a symmetric key and check that it can be decrypted
 * @throws IOException
 * @throws PGPException
 */
@Test
public void encryptSym() throws IOException, PGPException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  OutputStream os = GPGFileEncryptor.encryptFile(baos, PASSWORD, "DES");

  os.write(EXPECTED_FILE_CONTENT_BYTES);
  os.close();
  baos.close();

  byte[] encryptedBytes = baos.toByteArray();

  try (InputStream is = GPGFileDecryptor.decryptFile(new ByteArrayInputStream(encryptedBytes), "test")) {
    byte[] decryptedBytes = IOUtils.toByteArray(is);

    Assert.assertNotEquals(EXPECTED_FILE_CONTENT_BYTES, encryptedBytes);
    Assert.assertEquals(EXPECTED_FILE_CONTENT_BYTES, decryptedBytes);
  }
}
 
Example 6
Source File: GPGFileEncryptorTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * Encrypt a test string with an asymmetric key and check that it can be decrypted
 * @throws IOException
 * @throws PGPException
 */
@Test
public void encryptAsym() throws IOException, PGPException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  OutputStream os = GPGFileEncryptor.encryptFile(baos, getClass().getResourceAsStream(PUBLIC_KEY),
      Long.parseUnsignedLong(KEY_ID, 16), "CAST5");

  os.write(EXPECTED_FILE_CONTENT_BYTES);
  os.close();
  baos.close();

  byte[] encryptedBytes = baos.toByteArray();

  try (InputStream is = GPGFileDecryptor.decryptFile(new ByteArrayInputStream(encryptedBytes),
      getClass().getResourceAsStream(PRIVATE_KEY), PASSPHRASE)) {
    byte[] decryptedBytes = IOUtils.toByteArray(is);

    Assert.assertNotEquals(EXPECTED_FILE_CONTENT_BYTES, encryptedBytes);
    Assert.assertEquals(EXPECTED_FILE_CONTENT_BYTES, decryptedBytes);
  }
}
 
Example 7
Source File: ConfigTransformer.java    From nifi-minifi with Apache License 2.0 5 votes vote down vote up
protected static void writeNiFiPropertiesFile(ByteArrayOutputStream nifiPropertiesOutputStream, String destPath) throws IOException {
    final Path nifiPropertiesPath = Paths.get(destPath, "nifi.properties");
    try (FileOutputStream nifiProperties = new FileOutputStream(nifiPropertiesPath.toString())) {
        nifiPropertiesOutputStream.writeTo(nifiProperties);
    } finally {
        if (nifiPropertiesOutputStream != null) {
            nifiPropertiesOutputStream.flush();
            nifiPropertiesOutputStream.close();
        }
    }
}
 
Example 8
Source File: TreasuryXTeeServiceImplTest.java    From j-road with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: UDPConn.java    From PacketProxy with Apache License 2.0 4 votes vote down vote up
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();
}