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

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

Introduction

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

Prototype

public static String toString(byte[] input, String encoding) throws IOException 

Source Link

Document

Get the contents of a byte[] as a String using the specified character encoding.

Usage

From source file:com.sulistionoadi.belajar.service.ExecutorService.java

public void runJob() {
    try {//from w  ww.j a  v a2  s .c o  m
        ClassLoader classLoader = getClass().getClassLoader();
        String contentFile = IOUtils.toString(classLoader.getResourceAsStream("data.csv"), "UTF-8");
        String[] arrContent = contentFile.split("\n");

        ReadWriteLock fileLock = new ReadWriteLock();
        File logFile = new File(pathlog);
        if (!logFile.exists()) {
            logFile.createNewFile();
        }

        JsonLogFile jsonLog = new JsonLogFile();

        fileLock.lockWrite();
        mapper.writerWithDefaultPrettyPrinter().writeValue(logFile, jsonLog);
        fileLock.unlockWrite();

        System.out.println("Running Process Insert into Batch Queue");
        for (String line : arrContent) {
            if (StringUtils.hasText(line)) {
                while (true) {
                    try {
                        boolean inserted = taskExecutor.getThreadPoolExecutor().getQueue()
                                .offer(new WriteLogTask(line, logFile, fileLock), 60, TimeUnit.SECONDS);
                        if (inserted) {
                            break;
                        }
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ExecutorService.class.getName()).log(Level.SEVERE, null, ex);
                        break;
                    }
                }
                taskExecutor.execute(taskExecutor.getThreadPoolExecutor().getQueue().poll());
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(ExecutorService.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(ExecutorService.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.myapp.controller.act.rest.editor.main.StencilsetRestResource.java

@RequestMapping(value = "/act/service/editor/stencilset", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public @ResponseBody String getStencilset() {
    InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
    try {/*from w  ww .j a  v a2 s .c  o m*/
        return IOUtils.toString(stencilsetStream, "utf-8");
    } catch (Exception e) {
        e.printStackTrace();
        throw new ActivitiException("Error while loading stencil set", e);
    }
}

From source file:com.doctor330.cloud.modules.act.rest.editor.main.StencilsetRestResource.java

@RequestMapping(value = "/act/service/editor/stencilset", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public @ResponseBody String getStencilset() {
    InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
    try {/*  w  ww.  j ava  2s.c om*/
        return IOUtils.toString(stencilsetStream, "utf-8");
    } catch (Exception e) {
        throw new ActivitiException("Error while loading stencil set", e);
    }
}

From source file:fr.dutra.confluence2wordpress.core.toc.TOCBuilderTest.java

@Test
public void test() throws Exception {

    //Given/*  w  w w  .j  a  va 2  s.c o m*/
    PageContext pageContext = Mockito.mock(PageContext.class);
    Mockito.when(pageContext.getPageTitle()).thenReturn("Test");
    Mockito.when(pageContext.getOriginalContext()).thenReturn(pageContext);
    String xml = IOUtils.toString(this.getClass().getResourceAsStream("/toc.txt"), "UTF-8");
    TOCBuilder c = new TOCBuilder();

    //When
    Heading toc = c.buildTOC(xml, pageContext);

    //Then

    /*
     * Expected structure:
     *  
       0 Test [null]*
       3 0.0.1 [Test-0.0.1]
           4 0.0.1.1 [Test-0.0.1.1]*
           4 0.0.1.2 [Test-0.0.1.2]*
     1 1 [Test-1]
          3 1.0.1 [Test-1.0.1]*
     * Nodes marked with an asterisk should have an author.
     */

    assertThat(toc.getLevel()).isEqualTo(0);
    assertThat(toc.getLabel()).isEqualTo("Test");
    assertThat(toc.getAnchor()).isNull();
    assertThat(toc.getAuthor())
            .isEqualsToByComparingFields(new Author("Author 0", "Author 0", null, null, null, null));
    assertThat(toc.getChildren()).hasSize(2);

    Heading _0_0_1 = toc.getChildren().get(0);
    assertThat(_0_0_1.getLevel()).isEqualTo(3);
    assertThat(_0_0_1.getLabel())
            .isEqualTo("0.0.1 - Title with spaces, accents, double quotes and single quotes: ('\")");
    assertThat(_0_0_1.getAnchor()).isEqualTo(
            "Test-0.0.1-Titlewithspaces%2Caccents%2Cdoublequotesandsinglequotes%3A%28%27%22%C3%A9%29");
    assertThat(_0_0_1.getAuthor()).isNull();
    assertThat(_0_0_1.getChildren()).hasSize(2);

    Heading _0_0_1_1 = _0_0_1.getChildren().get(0);
    assertThat(_0_0_1_1.getLevel()).isEqualTo(4);
    assertThat(_0_0_1_1.getLabel()).isEqualTo("0.0.1.1");
    assertThat(_0_0_1_1.getAnchor()).isEqualTo("Test-0.0.1.1");
    assertThat(_0_0_1_1.getAuthor()).isEqualsToByComparingFields(
            new Author("Author 0.0.1.1", "Author 0.0.1.1", null, null, null, null));
    assertThat(_0_0_1_1.getChildren()).isEmpty();

    Heading _0_0_1_2 = _0_0_1.getChildren().get(1);
    assertThat(_0_0_1_2.getLevel()).isEqualTo(4);
    assertThat(_0_0_1_2.getLabel()).isEqualTo("0.0.1.2");
    assertThat(_0_0_1_2.getAnchor()).isEqualTo("Test-0.0.1.2");
    assertThat(_0_0_1_2.getAuthor()).isEqualsToByComparingFields(
            new Author("Author 0.0.1.2", "Author 0.0.1.2", null, null, null, null));
    assertThat(_0_0_1_2.getChildren()).isEmpty();

    Heading _1 = toc.getChildren().get(1);
    assertThat(_1.getLevel()).isEqualTo(1);
    assertThat(_1.getLabel()).isEqualTo("1");
    assertThat(_1.getAnchor()).isEqualTo("Test-1");
    assertThat(_1.getAuthor()).isNull();
    assertThat(_1.getChildren()).hasSize(1);

    Heading _1_0_1 = _1.getChildren().get(0);
    assertThat(_1_0_1.getLevel()).isEqualTo(3);
    assertThat(_1_0_1.getLabel()).isEqualTo("1.0.1");
    assertThat(_1_0_1.getAnchor()).isEqualTo("Test-1.0.1");
    assertThat(_1_0_1.getAuthor())
            .isEqualsToByComparingFields(new Author("Author 1.0.1", "Author 1.0.1", null, null, null, null));
    assertThat(_1_0_1.getChildren()).isEmpty();
}

From source file:com.reprezen.swaggerparser.test.ExamplesTest.java

@Parameters
public static Collection<URL> findExamples() throws IOException {
    Collection<URL> examples = Lists.newArrayList();
    Deque<URL> dirs = Queues.newArrayDeque();
    String auth = System.getenv("GITHUB_AUTH") != null ? System.getenv("GITHUB_AUTH") + "@" : "";
    String request = String.format("https://%sapi.github.com/repos/%s/contents/%s?ref=%s", auth, SPEC_REPO,
            EXAMPLES_ROOT, EXAMPLES_BRANCH);
    dirs.add(new URL(request));
    while (!dirs.isEmpty()) {
        URL url = dirs.remove();/*from w w w.j  a v a 2 s.  c  o m*/
        String json = IOUtils.toString(url, Charsets.UTF_8);
        JsonNode tree = mapper.readTree(json);
        for (JsonNode result : iterable(tree.elements())) {
            String type = result.get("type").asText();
            String path = result.get("path").asText();
            String resultUrl = result.get("url").asText();
            if (type.equals("dir")) {
                dirs.add(new URL(resultUrl));
            } else if (type.equals("file") && (path.endsWith(".yaml") || path.endsWith(".json"))) {
                String downloadUrl = result.get("download_url").asText();
                examples.add(new URL(downloadUrl));
            }
        }
    }
    return examples;
}

From source file:com.nestlabs.sdk.CameraAndroidTest.java

@Test
public void testCameraToParcel() {
    try {/*  ww w. jav a  2  s  .  c o m*/
        String json = IOUtils.toString(this.getClass().getResourceAsStream(TEST_CAMERA_JSON), "utf-8");
        Camera camera = mapper.readValue(json, Camera.class);

        Parcel parcel = Parcel.obtain();
        camera.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);

        Camera cameraFromParcel = Camera.CREATOR.createFromParcel(parcel);
        assertEquals(camera, cameraFromParcel);
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:ec.edu.ucuenca.analizer.service.RunTestServiceImpl.java

@Override
public String runMainTest(String endpoint, String queryFile, String folderToTest) {

    StringBuilder prefixes = new StringBuilder();
    prefixes.append(" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> ");

    /**/*from   ww  w  .  j  a  va2s.c  o m*/
     * Getting list of files
     */
    try {

        String query = "";
        query = IOUtils.toString(this.getClass().getResourceAsStream("/" + folderToTest + "/" + queryFile),
                "UTF-8");
        QueryExecution execution = null;
        try {
            long start = System.currentTimeMillis();
            long end = start + 5 * 1000; // 60 seconds * 1000 ms/sec
            while ((System.currentTimeMillis() < end) || execution == null) {
                execution = QueryExecutionFactory.sparqlService(endpoint, query);
                if (execution != null) {
                    break;
                }
            }
            if (execution == null) {
                System.out.println("    Time Exceeded, No results obtained  ");

            } else {
                int allRetrieveResources = 0;

                ResultSet queryResult = execution.execSelect();
                while (queryResult.hasNext()) {
                    QuerySolution solution = queryResult.next();
                    String uri = solution.get("resource").toString();
                    allRetrieveResources++;
                }
                System.out.println("    Recovered resources from " + allRetrieveResources);
                execution.close();

            }
        } catch (Exception e) {
            System.out.println("    No results obtained");
        } finally {

        }
        return "";
    } catch (IOException ex) {
        Logger.getLogger(RunTestServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "";
}

From source file:com.nestlabs.sdk.MetadataAndroidTest.java

@Test
public void testMetadataToParcel() {
    try {/* w ww.  j  ava  2  s .  c o m*/
        String json = IOUtils.toString(this.getClass().getResourceAsStream(TEST_METADATA_JSON), "utf-8");
        Metadata metadata = mapper.readValue(json, Metadata.class);

        Parcel parcel = Parcel.obtain();
        metadata.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);

        Metadata metadataFromParcel = Metadata.CREATOR.createFromParcel(parcel);
        assertEquals(metadata, metadataFromParcel);
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:com.nestlabs.sdk.DeviceAndroidTest.java

@Test
public void testNestDeviceToParcel() {
    try {/*www .jav  a2  s.  c o  m*/
        String json = IOUtils.toString(this.getClass().getResourceAsStream(TEST_DEVICE_JSON), "utf-8");
        Device device = mapper.readValue(json, Device.class);

        Parcel parcel = Parcel.obtain();
        device.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);

        Device deviceFromParcel = Device.CREATOR.createFromParcel(parcel);
        assertEquals(device, deviceFromParcel);
    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}

From source file:com.nestlabs.sdk.ThermostatAndroidTest.java

@Test
public void testThermostatToParcel() {
    try {/*from   w w  w  .j a  v  a 2s  . c om*/
        String json = IOUtils.toString(this.getClass().getResourceAsStream(TEST_THERMOSTAT_JSON), "utf-8");
        Thermostat thermostat = mapper.readValue(json, Thermostat.class);

        Parcel parcel = Parcel.obtain();
        thermostat.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);

        Thermostat thermostatFromParcel = Thermostat.CREATOR.createFromParcel(parcel);
        assertEquals(thermostat, thermostatFromParcel);

    } catch (IOException e) {
        e.printStackTrace();
        Assert.fail();
    }
}