Example usage for org.apache.commons.io IOUtils copy

List of usage examples for org.apache.commons.io IOUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils copy.

Prototype

public static void copy(Reader input, OutputStream output, String encoding) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

Usage

From source file:io.fares.junit.soapui.outside.test.SoapUIMockRunnerTest.java

public static String testMockService(String endpoint) throws MalformedURLException, IOException {

    HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection();
    con.setRequestMethod("POST");
    con.addRequestProperty("Accept", "application/soap+xml");
    con.addRequestProperty("Content-Type", "application/soap+xml");
    con.setDoOutput(true);/* w  w w  .j  a  va  2 s  . c  om*/
    con.getOutputStream().write(
            "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"><soap:Header/><soap:Body><GetWeather xmlns=\"http://www.webserviceX.NET\"><CityName>Brisbane</CityName></GetWeather></soap:Body></soap:Envelope>"
                    .getBytes("UTF-8"));
    InputStream is = con.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    String rs = writer.toString();
    LOG.fine(rs);
    return rs;

}

From source file:com.gmail.tylerfilla.axon.ui.activity.HelpFOSSViewerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Inflate and set content view
    setContentView(R.layout.activity_help_foss_viewer);

    // Find stuff
    Toolbar appBar = (Toolbar) findViewById(R.id.activity_help_foss_viewer_app_bar);
    WebView browser = (WebView) findViewById(R.id.activity_help_foss_viewer_browser);

    // Configure app bar
    setSupportActionBar(appBar);/*from  w w w .j  a v  a 2 s .  c o  m*/

    // Display up button in app bar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // Apply dark tint to navigation icon
    // noinspection ConstantConditions
    Drawable navigationIcon = DrawableCompat.wrap(appBar.getNavigationIcon());
    DrawableCompat.setTint(navigationIcon, 0x8b000000);
    appBar.setNavigationIcon(navigationIcon);

    // Get component index which was passed in
    int componentIndex = getIntent().getIntExtra("componentIndex", -1);

    if (componentIndex != -1) {
        // Component information
        String componentName = "";
        String licenseName = "";
        String licenseTextURL = "";

        try {
            // Read components file into a buffer
            StringWriter componentFileBuffer = new StringWriter();
            IOUtils.copy(getAssets().open("foss/components.json"), componentFileBuffer,
                    Charset.defaultCharset());

            // Parse component object from file
            JSONObject component = ((JSONArray) new JSONTokener(componentFileBuffer.toString()).nextValue())
                    .getJSONObject(componentIndex);

            // Extract component information from its JSON object
            componentName = component.getString("name");
            licenseName = component.getString("licenseName");
            licenseTextURL = component.getString("licenseTextURL");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        // Set title and subtitle
        setTitle(componentName);
        appBar.setSubtitle(licenseName);

        // Load license URL
        browser.loadUrl(licenseTextURL);
    }
}

From source file:it.cnr.ilc.tokenizer.utils.Utilities.java

/**
 *
 * @param is/*w ww. j ava 2 s  . co  m*/
 * @return
 */
public String convertInputStreamToString(InputStream is) {
    StringWriter writer = new StringWriter();
    String encoding = Vars.encoding;
    String message = "";
    String theString = "";
    try {
        IOUtils.copy(is, writer, encoding);
        theString = writer.toString();
    } catch (Exception e) {
        message = "IOException in coverting the stream into a string " + e.getMessage();
        Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, message);
    }

    return theString;

}

From source file:com.jaspersoft.android.jaspermobile.test.support.TestResource.java

public String asString() {
    InputStream inputStream = asStream();

    StringWriter writer = new StringWriter();
    try {//www  .ja v  a2 s.  c o m
        IOUtils.copy(inputStream, writer, "UTF-8");
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return writer.toString();
}

From source file:com.nfl.dm.clubsites.cms.articles.subapp.articleeditor.layouteditor.components.ParagraphImageLabel.java

public ParagraphImageLabel(String assetIdentifier) {

    try {//from   w  ww.ja  v  a  2  s  . c om
        StringWriter writer = new StringWriter();
        IOUtils.copy(getClass().getResourceAsStream(
                "/com/nfl/poc/subapp/articleeditor/layouteditor/templates/paragraph_image_template.html"),
                writer, null);
        paragraphImageTemplate = writer.toString();
        setValue(assetIdentifier);
    } catch (IOException e) {
        e.printStackTrace();
    }

    setContentMode(ContentMode.HTML);
}

From source file:at.mukprojects.giphy4j.entity.SerachFeedTest.java

@Before
public void setUp() throws Exception {
    File inputFile = new File(RESFILE);

    inputStream = new FileInputStream(inputFile);
    writer = new StringWriter();

    IOUtils.copy(inputStream, writer, "UTF-8");
    jsonResponse = writer.toString();//from w  w w .  ja  v a  2  s  . co m

    gson = new GsonBuilder().create();
    giphy = new Giphy(API_KEY);

    RequestSender senderCode = Mockito.mock(HttpRequestSender.class);
    when(senderCode.sendRequest(any(Request.class))).thenThrow(new IOException("Bad response! Code: 401"));
    giphyMockExceptionCode = new Giphy(API_KEY, senderCode);

    RequestSender senderParse = Mockito.mock(HttpRequestSender.class);
    when(senderParse.sendRequest(any(Request.class)))
            .thenThrow(new IOException("Unparseable response body! \n { '' }"));
    giphyMockExceptionParse = new Giphy(API_KEY, senderCode);

}

From source file:ch.cyberduck.core.sftp.auth.SFTPPublicKeyAuthenticationTest.java

@Test
public void testAuthenticateKeyNoPassword() throws Exception {
    final Credentials credentials = new Credentials(System.getProperties().getProperty("sftp.user"));
    final Local key = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    try {//from ww w . j  a  va2s.c  om
        credentials.setIdentity(key);
        new DefaultLocalTouchFeature().touch(key);
        IOUtils.copy(new StringReader(System.getProperties().getProperty("sftp.key")),
                key.getOutputStream(false), Charset.forName("UTF-8"));
        final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch", credentials);
        final SFTPSession session = new SFTPSession(host);
        session.open(new DisabledHostKeyCallback());
        assertTrue(new SFTPPublicKeyAuthentication(session).authenticate(host, new DisabledPasswordStore(),
                new DisabledLoginCallback() {
                    @Override
                    public Credentials prompt(final Host bookmark, String username, String title, String reason,
                            LoginOptions options) throws LoginCanceledException {
                        fail();
                        throw new LoginCanceledException();
                    }
                }, new DisabledCancelCallback()));
        session.close();
    } finally {
        key.delete();
    }
}

From source file:cc.recommenders.io.ReadingArchive.java

@Override
public String getNextPlain() {
    try {/*www.ja v  a 2  s  .c om*/
        ZipEntry next = entries.nextElement();
        InputStream in = zipFile.getInputStream(next);
        StringWriter writer = new StringWriter();
        IOUtils.copy(in, writer, Charset.defaultCharset().toString());
        String str = writer.toString();
        in.close();

        return str;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.adaptris.util.text.mime.PartIteratorCase.java

protected static String toString(byte[] b) throws Exception {
    StringWriter out = new StringWriter();
    try (InputStream in = new ByteArrayInputStream(b)) {
        IOUtils.copy(in, out, Charset.defaultCharset());
    }//from  w  w w.  ja  v  a  2s.  c  o m
    return out.toString();
}

From source file:ch.cyberduck.core.manta.MantaPublicKeyAuthenticationTest.java

@Test
public void testAuthenticateOpenSSHKeyWithoutPassphrase() throws Exception {
    Assume.assumeNotNull(System.getProperty("manta.url"), System.getProperty("manta.key_id"),
            System.getProperty("manta.key_path"));

    final Credentials credentials = new Credentials(System.getProperty("manta.user"), "");
    final Local key = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    credentials.setIdentity(key);/*from   w  ww. j  a  va 2 s  .  c  om*/

    try {
        new DefaultLocalTouchFeature().touch(key);
        IOUtils.copy(new FileReader(System.getProperty("manta.key_path")), key.getOutputStream(false),
                StandardCharsets.UTF_8);
        final String hostname = new URL(System.getProperty("manta.url")).getHost();
        final Host host = new Host(new MantaProtocol(), hostname, credentials);
        final MantaSession session = new MantaSession(host, new DisabledX509TrustManager(),
                new DefaultX509KeyManager());
        session.open(new DisabledHostKeyCallback());
        session.login(new DisabledPasswordStore(), new DisabledLoginCallback() {
            @Override
            public Credentials prompt(final Host bookmark, final String username, final String title,
                    final String reason, final LoginOptions options) throws LoginCanceledException {
                // no passphrase to set
                return null;
            }
        }, new DisabledCancelCallback());
        assertEquals(session.getClient().getContext().getMantaKeyId(), System.getProperty("manta.key_id"));
        session.close();
    } finally {
        key.delete();
    }
}