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

The following examples show how to use org.apache.commons.io.output.ByteArrayOutputStream#toString() . 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: Mp4v2InfoLoader.java    From AudioBookConverter with GNU General Public License v2.0 6 votes vote down vote up
public static void updateDuration(MediaInfo mediaInfo, String outputFileName) throws IOException {
    Process process = null;
    try {
        ProcessBuilder infoProcessBuilder = new ProcessBuilder(Utils.MP4INFO, outputFileName);
        process = infoProcessBuilder.start();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamCopier.copy(process.getInputStream(), out);
        StreamCopier.copy(process.getErrorStream(), System.err);
        String info = out.toString(Charset.defaultCharset());
        long duration = parseDuration(info);
        if (duration != 0) {
            mediaInfo.setDuration(duration);
        }
    } finally {
        Utils.closeSilently(process);
    }
}
 
Example 2
Source File: MPCoreUtils.java    From dx-java with MIT License 6 votes vote down vote up
/**
 * 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 3
Source File: DistributedCachingHeaderSerializationTestcase.java    From product-ei with Apache License 2.0 6 votes vote down vote up
@Test(groups = "wso2.esb", description = "cache meditor test enabling axis2 clustering.")
public void testDistributedCachingHeaderSerialization() throws Exception {

    String requestXml = "<a>ABC</a>";


    SimpleHttpClient httpClient = new SimpleHttpClient();
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/xml;charset=UTF-8");
    HttpResponse response1 = httpClient.doPost(getApiInvocationURL("CachingTest")+"/test", headers, requestXml, "application/xml;charset=UTF-8");
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    response1.getEntity().writeTo(baos1);
    String actualValue1 = baos1.toString();

    // this is to populate response from cache mediator
    HttpResponse response2 = httpClient.doPost(getApiInvocationURL("CachingTest")+"/test", headers, requestXml, "application/xml;charset=UTF-8");
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    response2.getEntity().writeTo(baos2);
    String actualValue2 = baos2.toString();

    Assert.assertEquals(actualValue1, requestXml);
    Assert.assertEquals(actualValue2, requestXml);
    Assert.assertTrue(stringExistsInLog("CACHEMATCHEDCACHEMATCHED"));
}
 
Example 4
Source File: PlatformDataExportServiceIT.java    From find with MIT License 6 votes vote down vote up
@Test
public void exportToCsv() throws E, IOException {
    final R queryRequest = queryRequestBuilderFactory.getObject()
            .queryRestrictions(testUtils.buildQueryRestrictions())
            .queryType(QueryRequest.QueryType.MODIFIED)
            .build();

    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    exportService.exportQueryResults(outputStream, queryRequest, ExportFormat.CSV, Collections.emptyList(), 1001L);
    final String output = outputStream.toString();
    assertNotNull(output);

    try (final CSVParser csvParser = CSVParser.parse(output, CSVFormat.EXCEL)) {
        final List<CSVRecord> records = csvParser.getRecords();
        assertThat(records, not(empty()));
        final CSVRecord headerRecord = records.get(0);
        assertThat(headerRecord.get(0), endsWith("Reference")); // byte-order mark may get in the way
        assertEquals("Database", headerRecord.get(1));
        final CSVRecord firstDataRecord = records.get(1);
        final String firstDataRecordReference = firstDataRecord.get(0);
        assertNotNull(firstDataRecordReference);
        assertFalse(firstDataRecordReference.trim().isEmpty());
        final String firstDataRecordDatabase = firstDataRecord.get(1);
        assertFalse(firstDataRecordDatabase.trim().isEmpty());
    }
}
 
Example 5
Source File: PlaylistServiceTestExport.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testExportToM3U() throws Exception {

    when(mediaFileDao.getFilesInPlaylist(eq(23))).thenReturn(getPlaylistFiles());
    when(settingsService.getPlaylistExportFormat()).thenReturn("m3u");

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    playlistService.exportPlaylist(23, outputStream);
    String actual = outputStream.toString();
    Assert.assertEquals(IOUtils.toString(getClass().getResourceAsStream("/PLAYLISTS/23.m3u")), actual);
}
 
Example 6
Source File: DistributedCachingHeaderSerializationTestcase.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
@Test(groups = "wso2.esb", description = "cache meditor test enabling axis2 clustering.")
public void testDistributedCachingHeaderSerialization() throws Exception {
    String requestXml = "<a>ABC</a>";

    SimpleHttpClient httpClient = new SimpleHttpClient();
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/xml;charset=UTF-8");
    HttpResponse response1 = httpClient.doPost(getApiInvocationURL("CachingTest") + "/test", headers, requestXml,
            "application/xml;charset=UTF-8");
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    response1.getEntity().writeTo(baos1);
    String actualValue1 = baos1.toString();

    // this is to populate response from cache mediator
    HttpResponse response2 = httpClient.doPost(getApiInvocationURL("CachingTest") + "/test", headers, requestXml,
            "application/xml;charset=UTF-8");
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    response2.getEntity().writeTo(baos2);
    String actualValue2 = baos2.toString();

    Assert.assertEquals(actualValue1, requestXml);
    Assert.assertEquals(actualValue2, requestXml);

    boolean existInLogs = carbonLogReader.checkForLog("CACHEMATCHEDCACHEMATCHED", DEFAULT_TIMEOUT);
    carbonLogReader.stop();
    Assert.assertTrue(existInLogs);
}
 
Example 7
Source File: PlaylistServiceTestExport.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testExportToM3U() throws Exception {

    when(mediaFileDao.getFilesInPlaylist(eq(23))).thenReturn(getPlaylistFiles());
    when(settingsService.getPlaylistExportFormat()).thenReturn("m3u");

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    playlistService.exportPlaylist(23, outputStream);
    String actual = outputStream.toString();
    Assert.assertEquals(IOUtils.toString(getClass().getResourceAsStream("/PLAYLISTS/23.m3u")), actual);
}
 
Example 8
Source File: Downloader.java    From Zom-Android-XMPP with GNU General Public License v3.0 5 votes vote down vote up
public static String bytesToHex(byte[] keyAndIv) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        new HexEncoder().encode(keyAndIv, 0, keyAndIv.length, baos);
        return baos.toString();
    }
    catch (IOException ioe)
    {
        ioe.printStackTrace();
        return null;
    }
}
 
Example 9
Source File: GoFileConfigDataSource.java    From gocd with Apache License 2.0 5 votes vote down vote up
public String configAsXml(CruiseConfig config, boolean skipPreprocessingAndValidation) throws Exception {
    LOGGER.debug("[Config Save] === Converting config to XML");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    magicalGoConfigXmlWriter.write(config, outputStream, skipPreprocessingAndValidation);
    LOGGER.debug("[Config Save] === Done converting config to XML");
    return outputStream.toString();
}
 
Example 10
Source File: ESBJAVA4318Testcase.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
@Test(groups = "wso2.esb", description = "cache meditor with payloads including Processing Insturctions")
public void testCacheMediatorWithPIs() throws Exception {

    String requestXml =
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">\n"
                    + "   <soapenv:Header>\n"
                    + "      <m:Trans xmlns:m=\"http://www.w3schools.com/transaction/\">234</m:Trans>\n"
                    + "   </soapenv:Header>\n" + "   <soapenv:Body>\n" + "      <ser:getFullQuote>\n"
                    + "         <ser:request>\n" + "            <xsd:symbol>IBM</xsd:symbol>\n"
                    + "         </ser:request>\n" + "      </ser:getFullQuote>\n" + "   </soapenv:Body>\n"
                    + "</soapenv:Envelope>";

    final String expectedValue = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope "
            + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><b xmlns=\"http://ws"
            + ".apache.org/ns/synapse\"><?xml-multiple  array?><xyz><a xmlns=\"\">after "
            + "cache</a></xyz></b></soapenv:Body></soapenv:Envelope>";

    DefaultHttpClient httpclient = new DefaultHttpClient();

    // this is to populate the cache mediator
    HttpPost httpPost = new HttpPost(getProxyServiceURLHttp("PF"));
    httpPost.addHeader("SOAPAction", "urn:getFullQuote");
    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
    HttpEntity stringEntity = new StringEntity(requestXml, CharEncoding.UTF_8);
    httpPost.setEntity(stringEntity);
    HttpResponse response = httpclient.execute(httpPost);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.getEntity().writeTo(baos);
    String actualValue = baos.toString();

    // this is to get the actual cache response
    HttpPost httpPost2 = new HttpPost(getProxyServiceURLHttp("PF"));
    httpPost2.addHeader("SOAPAction", "urn:getFullQuote");
    httpPost2.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
    HttpEntity stringEntity2 = new StringEntity(requestXml, CharEncoding.UTF_8);
    httpPost2.setEntity(stringEntity2);
    HttpResponse response2 = httpclient.execute(httpPost);

    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    response2.getEntity().writeTo(baos2);
    String actualValue2 = baos2.toString();
    Assert.assertEquals(actualValue2, expectedValue);
}
 
Example 11
Source File: ESBJAVA4318Testcase.java    From product-ei with Apache License 2.0 4 votes vote down vote up
@Test(groups = "wso2.esb", description = "cache meditor with payloads including Processing Insturctions")
public void testCacheMediatorWithPIs() throws Exception {

    String requestXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\">\n"
            + "   <soapenv:Header>\n"
            + "      <m:Trans xmlns:m=\"http://www.w3schools.com/transaction/\">234</m:Trans>\n"
            + "   </soapenv:Header>\n" + "   <soapenv:Body>\n" + "      <ser:getFullQuote>\n"
            + "         <ser:request>\n" + "            <xsd:symbol>IBM</xsd:symbol>\n"
            + "         </ser:request>\n" + "      </ser:getFullQuote>\n" + "   </soapenv:Body>\n"
            + "</soapenv:Envelope>";

    final String expectedValue = "<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope " +
            "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Body><b xmlns=\"http://ws" +
            ".apache.org/ns/synapse\"><?xml-multiple  array?><xyz><a xmlns=\"\">after " +
            "cache</a></xyz></b></soapenv:Body></soapenv:Envelope>";

    DefaultHttpClient httpclient = new DefaultHttpClient();

    // this is to populate the cache mediator
    HttpPost httpPost = new HttpPost(getProxyServiceURLHttp("PF"));
    httpPost.addHeader("SOAPAction", "urn:getFullQuote");
    httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
    HttpEntity stringEntity = new StringEntity(requestXml, CharEncoding.UTF_8);
    httpPost.setEntity(stringEntity);
    HttpResponse response = httpclient.execute(httpPost);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.getEntity().writeTo(baos);
    String actualValue = baos.toString();

    // this is to get the actual cache response
    HttpPost httpPost2 = new HttpPost(getProxyServiceURLHttp("PF"));
    httpPost2.addHeader("SOAPAction", "urn:getFullQuote");
    httpPost2.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
    HttpEntity stringEntity2 = new StringEntity(requestXml, CharEncoding.UTF_8);
    httpPost2.setEntity(stringEntity2);
    HttpResponse response2 = httpclient.execute(httpPost);

    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    response2.getEntity().writeTo(baos2);
    String actualValue2 = baos2.toString();
    Assert.assertEquals(actualValue2, expectedValue);
}
 
Example 12
Source File: AbstractJibxLoader.java    From celerio with Apache License 2.0 4 votes vote down vote up
public String toXml(T object) throws XmlMappingException, IOException {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    write(object, stream);
    return stream.toString();
}