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, String encoding) throws IOException 

Source Link

Document

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

Usage

From source file:jeffaschenk.tomcat.zuul.util.KeyFileUtils.java

/**
 * Utility Main to provide capabilities to generate a protected KeyFile for use with Zuul
 *
 * @param args - Incoming program Arguments
 * @throws Exception/*ww w  .  j a v a  2  s  . co  m*/
 */
public static void main(String[] args) throws Exception {

    // Initialize our Ciphers
    KeyFileUtils keyFileUtils = new KeyFileUtils();
    keyFileUtils.InitCiphers();

    // Assume we our sole purpose in life is to prompt for a Password, which is the same,
    // ensure we have an argument to be out filename.
    if ((args == null) || (args.length != 1)) {
        System.out.println("Invalid number of Arguments specified!");
        System.out.println(
                "Usage: " + keyFileUtils.getClass().getName() + " <Fully Qualified Output Key Filename>");
        return;
    }

    // Validate Argument as a file.
    File outputKeyFile = new File(args[0]);
    if (outputKeyFile.exists()) {
        // Prompt to overwrite...
        char[] answer = readPassword("Specified Key File:[" + outputKeyFile + "], exists. Overwrite? (y/n)? ");
        if ((answer == null) || (answer.length == 0)
                || (!String.valueOf(answer).toUpperCase().startsWith("Y"))) {
            System.out.println(
                    "Existing Key File:[" + outputKeyFile + "], exists. Will not be Overwritten, Done.");
            return;
        } else {
            outputKeyFile.delete();
        }
    }

    // Prompt Operator for the Zuul Encryption Password to be encrypted in a file and saved for later access.
    // Prompt to overwrite...
    char[] password = obtainValidatedPassword("Enter Zuul Encryption Password: ");
    if (password == null) {
        System.out.println("Unable to obtain Password, Done.");
        return;
    }

    // Create our Input and Output Streams for the encryption of the Password.
    InputStream is = IOUtils.toInputStream(String.valueOf(password), "UTF-8");
    FileOutputStream fos = new FileOutputStream(outputKeyFile);

    // Encrypt
    keyFileUtils.CBCEncrypt(is, fos);

    // Now lets us Validate the file.
    keyFileUtils.ResetCiphers();
    keyFileUtils = new KeyFileUtils();
    keyFileUtils.InitCiphers();

    // Obtain the Decrypted Contents
    byte[] obtainedDecryptedData = keyFileUtils.CBCDecrypt(new FileInputStream(outputKeyFile));

    // Compare to validate...
    boolean valid = String.valueOf(password).equals(new String(obtainedDecryptedData));
    Arrays.fill(password, ' ');
    Arrays.fill(obtainedDecryptedData, (byte) 0x00);
    if (valid) {
        System.out.println("Key File:[" + outputKeyFile + "], validated and available for use, Done.");
        return;
    } else {
        System.out.println("Key File:[" + outputKeyFile + "], Did not Validate, very bad!");
        return;
    }
}

From source file:com.meltmedia.dropwizard.crypto.Mocks.java

public static void mockInput(Namespace namespace, String value) throws IOException {
    when(namespace.get(Commands.INFILE)).thenReturn(IOUtils.toInputStream(value, "UTF-8"));
}

From source file:cat.calidos.morfeu.model.injection.StringToParsedModule.java

@Produces
public static org.w3c.dom.Document produceDomDocument(DocumentBuilder db, @Named("Content") String content)
        throws ParsingException, FetchingException {

    // TODO: we can probably parse with something faster than building into dom
    try {/*from ww  w  .  ja va 2  s  .c om*/
        return db.parse(IOUtils.toInputStream(content, Config.DEFAULT_CHARSET));
    } catch (SAXException e) {
        throw new ParsingException("Problem when parsing '" + content.substring(0, 20) + "'", e);
    } catch (IOException e) {
        throw new FetchingException("IO problem when parsing '" + content.substring(0, 20) + "'", e);
    }

}

From source file:com.dalelotts.rpn.ScannerTest.java

@Test
public void hasNextReturnsFalseForEmptyString() throws Exception {
    // Given//www.  ja v a2s  . c om
    try (final InputStream inputStream = IOUtils.toInputStream("", "UTF-8")) {
        scanner = new Scanner(inputStream);

        // When
        final boolean hasNext = scanner.hasNext();

        // Then
        assertThat(hasNext, is(false));
    }
}

From source file:de.shadowhunt.subversion.internal.ResourcePropertyUtils.java

static InputStream escapedInputStream(final InputStream inputStream) throws IOException {
    final String rawData = IOUtils.toString(inputStream, UTF8);
    inputStream.close();/*from   ww w  . j  a  va2s . c o m*/

    final Matcher matcher = PATTERN.matcher(rawData);
    final String escapedData = matcher.replaceAll("<$1$2$3:$4" + MARKER + "$5$6$7>");
    return IOUtils.toInputStream(escapedData, UTF8);
}

From source file:de.shadowhunt.subversion.internal.AbstractRepositoryAddIT.java

public static void file(final Repository repository, final Resource resource, final String content,
        final boolean initial) throws Exception {
    if (!initial) {
        Assert.assertTrue(resource + " must not exist", repository.exists(resource, Revision.HEAD));
    }//from  w w  w  .  j  av a2 s  .  com

    final Transaction transaction = repository.createTransaction();
    try {
        Assert.assertTrue("transaction must be active", transaction.isActive());
        repository.add(transaction, resource, true, IOUtils.toInputStream(content, AbstractHelper.UTF8));
        Assert.assertTrue("transaction must be active", transaction.isActive());

        final Status expectedStatus = (initial) ? Status.ADDED : Status.MODIFIED;
        Assert.assertEquals("change set must contain: " + resource, expectedStatus,
                transaction.getChangeSet().get(resource));
        repository.commit(transaction, "add " + resource);
        Assert.assertFalse("transaction must not be active", transaction.isActive());
    } finally {
        repository.rollbackIfNotCommitted(transaction);
    }

    final InputStream expected = IOUtils.toInputStream(content, AbstractHelper.UTF8);
    final InputStream actual = repository.download(resource, Revision.HEAD);
    AbstractRepositoryDownloadIT.assertEquals("content must match", expected, actual);
}

From source file:de.micromata.genome.gwiki.utils.PropUtils.java

public static OrderedProperties toProperties(String text) {
    OrderedProperties props = new OrderedProperties();
    if (StringUtils.isEmpty(text) == true) {
        return props;
    }/*  w  ww.  ja  va2 s  . co  m*/
    try {
        props.load(IOUtils.toInputStream(text, PROPS_ENCODING));
        return props;
    } catch (IOException ex) {
        throw new RuntimeIOException("Failed to parse Property file: " + ex.getMessage(), ex);
    }
}

From source file:cleaner.TikaHtmlCleaner.java

@Override
public String CleanText(String html, String encoding) {
    InputStream input;/*from  w ww.ja  v  a 2 s . co  m*/
    try {
        input = IOUtils.toInputStream(html, encoding);
    } catch (IOException e) {
        System.err.println("Error parsing: " + e);
        return null;
    }
    LinkContentHandler linkHandler = new LinkContentHandler();
    BodyContentHandler textHandler = new BodyContentHandler(-1); // -1 disables content limit
    ToHTMLContentHandler toHTMLHandler = new ToHTMLContentHandler();
    TeeContentHandler teeHandler = new TeeContentHandler(linkHandler, textHandler, toHTMLHandler);
    Metadata metadata = new Metadata();
    metadata.set(Metadata.CONTENT_TYPE, "text/html");
    metadata.set(Metadata.CONTENT_ENCODING, encoding);
    ParseContext parseContext = new ParseContext();
    HtmlParser parser = new HtmlParser();
    try {
        parser.parse(input, teeHandler, metadata, parseContext);
    } catch (Exception e) {
        System.err.println("Error parsing: " + e);
        return null;
    }
    /*
    System.out.println("title:\n" + metadata.get("title"));
    System.out.println("links:\n" + linkHandler.getLinks());
    System.out.println("text:\n" + textHandler.toString());
    System.out.println("html:\n" + toHTMLHandler.toString());
    */
    return LeoCleanerUtil.CollapseSpaces(textHandler.toString());
}

From source file:ch.entwine.weblounge.common.impl.security.AccessControlParser.java

public static AccessControlList parseAcl(String serializedForm) throws IOException {
    return parseAcl(IOUtils.toInputStream(serializedForm, "utf-8"));
}

From source file:com.rodrigodev.xgen4j_table_generator.test.common.StringUtils.java

public static InputStream toInputStream(String aValue) {
    return IOUtils.toInputStream(aValue, StandardCharsets.UTF_8);
}