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

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

Introduction

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

Prototype

public static InputStream toInputStream(String input) 

Source Link

Document

Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.

Usage

From source file:gobblin.source.extractor.filebased.FileBasedExtractorTest.java

public void testReadRecordWithEmptyAndNonEmptyFiles()
        throws DataRecordException, IOException, FileBasedHelperException {
    String file1 = "file1.txt";
    String file2 = "file2.txt";
    String file3 = "file3.txt";

    WorkUnitState state = new WorkUnitState();
    state.setProp(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL, Joiner.on(",").join(file1, file2, file3));

    FileBasedHelper fsHelper = Mockito.mock(FileBasedHelper.class);
    Mockito.when(fsHelper.getFileStream(file1)).thenReturn(IOUtils.toInputStream("record1 \n record2"));
    Mockito.when(fsHelper.getFileStream(file2)).thenReturn(IOUtils.toInputStream(""));
    Mockito.when(fsHelper.getFileStream(file3))
            .thenReturn(IOUtils.toInputStream("record3 \n record4 \n record5"));

    FileBasedExtractor<String, String> extractor = new DummyFileBasedExtractor<String, String>(state, fsHelper);

    Assert.assertEquals(getNumRecords(extractor), 5);
}

From source file:com.aliasi.demo.framework.DemoCommand.java

private void processString(StreamDemo demo, Properties properties) {
    InputStream in = null;//w  ww .jav a2s . c o m
    OutputStream out = null;
    try {
        out = new FileOutputStream("C:\\Users\\nasif.noorudeen\\NLPProcessing\\sample.xml");
        in = IOUtils.toInputStream(mtweets);
        demo.process(in, out, properties, mMap);
    } catch (IOException e) {
        System.out.println("Exception msg=" + e);
        e.printStackTrace(System.out);
    } finally {
        Streams.closeQuietly(in);
        Streams.closeQuietly(out);
    }

}

From source file:net.di2e.ecdr.search.transform.atom.response.AtomResponseTransformerTest.java

@Test
public void testInvalidEntry() throws Exception {
    AtomSearchResponseTransformerConfig config = mock(AtomSearchResponseTransformerConfig.class);
    QueryRequest request = mock(QueryRequest.class);
    AtomResponseTransformer transformer = new AtomResponseTransformer(config);
    String atomXML = IOUtils.toString(getClass().getResourceAsStream(ATOM_INVALID_FILE));
    SourceResponse response = transformer.processSearchResponse(IOUtils.toInputStream(atomXML), request,
            SITE_NAME);//ww w  .j  a  v a 2 s .c  o  m
    assertEquals(0, response.getHits());
    assertEquals(0, response.getResults().size());
}

From source file:com.github.restdriver.clientdriver.unit.ClientDriverResponseTest.java

@Test
public void creatingResponseWithInputStreamContentHasBody() {
    ClientDriverResponse response = new ClientDriverResponse(IOUtils.toInputStream("content"),
            "application/octet-stream");

    assertThat(response.hasBody(), is(true));
}

From source file:com.msopentech.odatajclient.engine.performance.BasicPerfTest.java

@Test
public void readJSONViaLowerlevelLibs() throws IOException {
    final ObjectMapper mapper = new ObjectMapper();

    final JsonNode entry = mapper.readTree(IOUtils.toInputStream(input.get(ODataPubFormat.JSON)));
    assertNotNull(entry);/*from  w w w  .j  a  v  a  2s  .  c om*/
}

From source file:com.github.rholder.gradle.dependency.DependencyConversionUtil.java

private static void acumenTemplateFromClasspath(File extractedJarFile, File outputFile) throws IOException {
    InputStream input = DependencyConversionUtil.class.getResourceAsStream("/init-acumen.gradle");

    // replace token with extracted file, replace '\' with '/' to handle Windows paths
    String processed = IOUtils.toString(input).replace("#ACUMEN_JAR#", extractedJarFile.getAbsolutePath())
            .replace("\\", "/");
    input.close();/* w  ww. j  ava2s.c  om*/

    InputStream processedInput = IOUtils.toInputStream(processed);

    OutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
    IOUtils.copy(processedInput, output);

    output.close();
    processedInput.close();
}

From source file:com.smartitengineering.event.hub.core.ChannelHubResource.java

@Broadcast
@POST// w w w  . j  a  va  2 s .c  o  m
@Cluster(name = "EventHub", value = JGroupsFilter.class)
public Response broadcast(@HeaderParam("Content-type") String contentType, String message) {
    checkAuthToken();
    checkChannelExistence();
    final String eventContentType;
    //HTTP Request entity body can not be blank
    if (StringUtils.isBlank(message)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    final boolean isHtmlPost;
    if (StringUtils.isBlank(contentType)) {
        eventContentType = MediaType.APPLICATION_OCTET_STREAM;
        isHtmlPost = false;
    } else if (contentType.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
        eventContentType = MediaType.APPLICATION_OCTET_STREAM;
        isHtmlPost = true;
        try {
            //Will search for the first '=' if not found will take the whole string
            final int startIndex = message.indexOf("=") + 1;
            //Consider the first '=' as the start of a value point and take rest as value
            final String realMsg = message.substring(startIndex);
            //Decode the message to ignore the form encodings and make them human readable
            message = URLDecoder.decode(realMsg, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
    } else {
        eventContentType = contentType;
        isHtmlPost = false;
    }
    Event event = APIFactory.getEventBuilder()
            .eventContent(APIFactory.getContent(eventContentType, IOUtils.toInputStream(message))).build();
    final Channel channel = HubPersistentStorerSPI.getInstance().getStorer().getChannel(channelName);
    event = HubPersistentStorerSPI.getInstance().getStorer().create(channel, event);
    //Add a new line at the end of the message to ensure that the message is flushed to its listeners
    message = message + "\n";
    Broadcastable broadcastable = new Broadcastable(message, broadcaster);
    ResponseBuilder builder = Response.ok(broadcastable);
    builder.location(getAbsoluteURIBuilder().path(EventResource.class).build(event.getPlaceholderId()));
    if (isHtmlPost) {
        builder.status(Response.Status.SEE_OTHER);
        builder.location(getAbsoluteURIBuilder().path(ChannelEventsResource.class).build(channelName));
    }
    return builder.build();
}

From source file:net.di2e.ecdr.search.transform.atom.response.AtomResponseTransformer.java

@Override
public SourceResponse processSearchResponse(InputStream inputStream, QueryRequest request, String siteName) {
    List<Result> resultList = new ArrayList<Result>();

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    Parser parser = null;/* w  w w  .  j a  v  a2 s .  com*/
    Document<Feed> atomDoc;
    try {

        Thread.currentThread().setContextClassLoader(AtomResponseTransformer.class.getClassLoader());
        parser = ABDERA.getParser();
        if (LOGGER.isTraceEnabled()) {
            StringWriter writer = new StringWriter();
            try {
                IOUtils.copy(inputStream, writer);
                LOGGER.trace("Transforming the following atom feed into a DDF SourceResponse:{}{}",
                        System.lineSeparator(), writer);
                inputStream = IOUtils.toInputStream(writer.toString());
            } catch (IOException e) {
                LOGGER.trace("Could not print out atom stream for log: {}", e.getMessage());
            }
        }
        atomDoc = parser.parse(new InputStreamReader(inputStream));
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }

    Feed feed = atomDoc.getRoot();

    List<Entry> entries = feed.getEntries();
    int size = entries.size();
    for (Entry entry : entries) {
        if (isValidEntry(entry)) {
            Metacard metacard = entryToMetacard(entry, siteName);
            resultList.add(metacardToResult(entry, metacard));
        } else {
            LOGGER.debug("Skipping invalid entry: {}", entry);
            size--;
        }
    }

    long totalResults = size;
    Element totalResultsElement = atomDoc.getRoot().getExtension(OpenSearchConstants.TOTAL_RESULTS);

    if (totalResultsElement != null) {
        try {
            totalResults = Long.parseLong(totalResultsElement.getText());
        } catch (NumberFormatException e) {
            LOGGER.warn("Received invalid number of results from Atom response ["
                    + totalResultsElement.getText() + "]", e);
        }
    }

    Map<String, Serializable> responseProperties = null;

    return new SourceResponseImpl(request, responseProperties, resultList, totalResults);
}

From source file:mitm.common.util.NameValueLineIteratorTest.java

@Test
public void testNameValueLineIteratorEmpty() throws IOException {
    String data = "";

    Iterator<NameValueLineIterator.Entry> it = new NameValueLineIterator(IOUtils.toInputStream(data));

    assertFalse(it.hasNext());//from  ww  w.  j  av  a2s.  c  om
}

From source file:gaffer.serialisation.json.hyperloglogplus.HyperLogLogPlusJsonSerialisationTest.java

private void runTestWithSketch(final HyperLogLogPlus sketch) throws IOException {
    // When - serialise
    final String json = mapper.writeValueAsString(sketch);

    // Then - serialise
    final String[] parts = json.split("[:,]");
    final String[] expectedParts = { "{\"hyperLogLogPlus\"", "{\"hyperLogLogPlusSketchBytes\"", "BYTES",
            "\"cardinality\"", sketch.cardinality() + "}}" };
    for (int i = 0; i < parts.length; i++) {
        if (2 != i) { // skip checking the bytes
            assertEquals(expectedParts[i], parts[i]);
        }/* w ww .j av  a  2  s  . com*/
    }

    // When - deserialise
    final HyperLogLogPlus deserialisedSketch = mapper.readValue(IOUtils.toInputStream(json),
            HyperLogLogPlus.class);

    // Then - deserialise
    assertNotNull(deserialisedSketch);
    assertEquals(sketch.cardinality(), deserialisedSketch.cardinality());
}