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:eu.seaclouds.platform.dashboard.config.PlannerFactory.java

public String getServiceOfferings() throws URISyntaxException, IOException {
    InputStream in = getClass().getResourceAsStream(DEFAULT_OFFERINGS_PATH);
    StringWriter writer = new StringWriter();
    IOUtils.copy(in, writer, Charsets.UTF_8);
    return writer.toString();
}

From source file:kltn.geocoding.Geocoding.java

private static Geometry getBoundary(String s)
        throws MalformedURLException, IOException, org.json.simple.parser.ParseException {
    String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM";
    link = link + "&address=" + URLEncoder.encode(s);
    URL url = new URL(link);
    HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
    InputStream is = httpsCon.getInputStream();
    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");

    String jsonString = writer.toString();
    JSONParser parse = new JSONParser();
    Object obj = parse.parse(jsonString);
    JSONObject jsonObject = (JSONObject) obj;
    System.out.println(s);/*ww  w.  j  a  v a 2  s  . com*/
    System.out.println(jsonObject.toJSONString());
    JSONArray resultArr = (JSONArray) jsonObject.get("results");
    Object resultObject = parse.parse(resultArr.get(0).toString());
    JSONObject resultJsonObject = (JSONObject) resultObject;

    Object geoObject = parse.parse(resultJsonObject.get("geometry").toString());
    JSONObject geoJsonObject = (JSONObject) geoObject;

    if (!geoJsonObject.containsKey("bounds")) {
        return null;
    }
    Object boundObject = parse.parse(geoJsonObject.get("bounds").toString());
    JSONObject boundJsonObject = (JSONObject) boundObject;
    //        System.out.println(boundJsonObject.toJSONString());

    Object southwest = parse.parse(boundJsonObject.get("southwest").toString());
    JSONObject southwestJson = (JSONObject) southwest;
    String southwestLat = southwestJson.get("lat").toString();
    String southwestLong = southwestJson.get("lng").toString();

    Object northeast = parse.parse(boundJsonObject.get("northeast").toString());
    JSONObject northeastJson = (JSONObject) northeast;
    String northeastLat = northeastJson.get("lat").toString();
    String northeastLong = northeastJson.get("lng").toString();

    String polygon = "POLYGON((" + southwestLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim()
            + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + southwestLat.trim() + ","
            + southwestLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " "
            + northeastLat.trim() + "))";
    Geometry geo = wktToGeometry(polygon);
    return geo;
}

From source file:de.micromata.genome.util.runtime.AssertUtils.java

/**
 * Gets the file lines./*from   w  ww  .  j a  v a2  s .  c  o m*/
 *
 * @param f the f
 * @return the file lines
 */
private static List<String> getFileLines(final File f) {
    final StringWriter sout = new StringWriter();
    try {
        IOUtils.copy(new FileInputStream(f), sout, Charset.defaultCharset());
    } catch (final Exception ex) {
        return null;
    }
    final String fc = sout.getBuffer().toString();
    final List<String> s = getLines(fc);
    return s;
}

From source file:cc.kune.core.server.manager.file.FileDownloadManagerUtils.java

/**
 * Gets the inpu stream as string.//from w ww.j  av a2  s . c o m
 * 
 * @param iStream
 *          the i stream
 * @return the inpu stream as string
 * @throws IOException
 *           Signals that an I/O exception has occurred.
 */
public static String getInpuStreamAsString(final InputStream iStream) throws IOException {
    final StringWriter writer = new StringWriter();
    IOUtils.copy(iStream, writer, "UTF-8");
    return writer.toString();
}

From source file:com.adaptris.core.common.FileInputParameterImpl.java

protected String load(URLString loc, String encoding) throws IOException {
    String content = null;//  www  .  j  a v a  2 s  .c  om

    try (InputStream inputStream = connect(loc)) {
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStream, writer, encoding);
        content = writer.toString();
    }
    return content;
}

From source file:com.mmiagency.knime.nodes.moz.api.util.ConnectionUtil.java

/**
 * /*from   w w  w  . java  2s. com*/
 * Method to make a GET HTTP connecton to 
 * the given url and return the output
 * 
 * @param urlToFetch url to be connected
 * @return the http get response
 */
public static String makeRequest(String urlToFetch) throws Exception {

    HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
        public void initialize(HttpRequest request) {
        }
    });
    HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(urlToFetch.toString()));
    HttpResponse response = request.execute();
    StringWriter writer = new StringWriter();
    IOUtils.copy(response.getContent(), writer, response.getContentEncoding());
    return writer.toString();
}

From source file:com.seer.datacruncher.streams.JsonDataStreamTest.java

@Test
public void testJsonDataStream() {
    InputStream in = this.getClass().getClassLoader()
            .getResourceAsStream(stream_file_path + json_test_stream_file_name);
    StringWriter writer = new StringWriter();
    try {// w  ww . ja  va 2s.c om
        IOUtils.copy(in, writer, "UTF-8");
    } catch (IOException e) {
        assertTrue("IOException while json file reading", false);
    }
    String stream = writer.toString();
    DatastreamsInput datastreamsInput = new DatastreamsInput();
    String res = datastreamsInput.datastreamsInput(stream, (Long) schemaEntity.getIdSchema(), null, true);
    assertTrue("Json file validation failed", Boolean.parseBoolean(res));
}

From source file:com.flipkart.phantom.task.utils.StringUtils.java

/**
 * always encode in UTF8. does NOT close your input stream. client need to close it.
 * @param inputStream//  www  .  ja va  2s  . co  m
 * @return String encoded using UTF-8 scheme.
 * @throws Exception, any exception encountered. Will not check for any null condition.
 */
public static String inputStream2String(InputStream inputStream) throws Exception {
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    String theString = writer.toString();
    return theString;
}

From source file:com.floragunn.searchguard.test.helper.file.FileHelper.java

public static final String loadFile(final String file) throws IOException {
    final StringWriter sw = new StringWriter();
    IOUtils.copy(FileHelper.class.getResourceAsStream("/" + file), sw, StandardCharsets.UTF_8);
    return sw.toString();
}

From source file:com.temenos.interaction.core.resource.TestResourceMetadataManager.java

@Test
public void testConstructor() throws Exception {
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("AirlinesMetadata.xml");
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, "UTF-8");
    String xml = writer.toString();
    ResourceStateMachine stateMachine = mock(ResourceStateMachine.class);
    ResourceMetadataManager mdProducer = new ResourceMetadataManager(xml, stateMachine);
    Metadata metadata = mdProducer.getMetadata();
    assertNotNull(metadata);/*from   w ww.ja  v  a  2s.c o  m*/
}