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) throws IOException 

Source Link

Document

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

Usage

From source file:com.junoyoon.BullsUtil.java

public static void copyResource(String toDir, String fileName) throws IOException {
    File file = new File(toDir);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();/*w ww . j  a v a  2  s.  c  o m*/
    }
    try {
        InputStream is = BullsUtil.class.getClassLoader().getResourceAsStream(fileName);
        FileOutputStream fs = new FileOutputStream(toDir);
        IOUtils.copy(is, fs);
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(fs);
    } catch (Exception e) {
        BullsHtml.printErrorAndExit(e);
    }
}

From source file:io.swagger.samples.inflector.dropwizard.controllers.SampleController.java

public ResponseContext uploadFile(RequestContext request, Long petId, String additionalMetadata,
        java.io.InputStream file) {
    ByteArrayOutputStream outputStream;
    try {//from   w ww . j  a  v a  2s  .  c o  m
        outputStream = new ByteArrayOutputStream();
        IOUtils.copy(file, outputStream);
        outputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:co.kademi.kdom.KParser.java

public KDocument parse(InputStream in) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(in, bout);
    String s = bout.toString("UTF8");
    final KDocument doc = new KDocument();
    List<String> beginTagTextStack = new ArrayList<>();
    List<List<KDocument.KDomNode>> stack = new ArrayList();

    RegexTokenizer.TokenHandler handler = new RegexTokenizer.TokenHandler() {
        List<KDocument.KDomNode> currentChildren;

        @Override/*from w  ww.  j  av  a 2 s .  c  o  m*/
        public void onToken(RegexTokenizer.TokenType type, String text) {
            KDocument.KDomElement newEl;
            String beginTagText;
            switch (type) {
            case DECLARATION:
                // ignore for now
                break;
            case OPEN_TAG:
                beginTagTextStack.add(0, text);
                currentChildren = new ArrayList<>();
                stack.add(0, currentChildren);
                break;
            case CLOSE_TAG:
                text = text.substring(2, text.length() - 1);
                if (beginTagTextStack.isEmpty()) {
                    throw new RuntimeException("Unbalanced tags: " + text);
                }
                beginTagText = beginTagTextStack.remove(0);
                beginTagText = beginTagText.substring(1, beginTagText.length() - 1);
                if (!beginTagText.startsWith(text)) {
                    throw new RuntimeException("Unbalanced tags. Start=" + beginTagText + " finished: " + text);
                }
                newEl = createAndAddElement(beginTagText, currentChildren, doc);
                stack.remove(0);
                if (!stack.isEmpty()) {
                    currentChildren = stack.get(0);
                    currentChildren.add(newEl);
                } else {
                    doc.setRoot(newEl);
                }
                break;
            case SELF_CLOSE_TAG:
                text = text.substring(1, text.length() - 2);
                newEl = createAndAddElement(text, Collections.EMPTY_LIST, doc);
                currentChildren.add(newEl);
                break;
            case MUSTACHE:
                String s = text.substring(2, text.length() - 2);
                Expression expr = jexl.createExpression(s);
                try {
                    KDocument.KDomExpressionNode n = doc.createExpressionNode(text, expr);
                    currentChildren.add(n);
                } catch (Exception e) {
                    throw new RuntimeException("Couldnt parse: " + text, e);
                }

                break;
            case MUSTACHE_OPEN:
                beginTagTextStack.add(0, text);
                currentChildren = new ArrayList<>();
                stack.add(0, currentChildren);
                break;
            case MUSTACHE_CLOSE:
                beginTagText = beginTagTextStack.remove(0);

                String startTag = beginTagText.substring(3, beginTagText.length() - 2);
                int i = startTag.indexOf(" ");
                String helper = null;
                String exprText = startTag;
                if (i > 0) {
                    helper = startTag.substring(0, i);
                    exprText = startTag.substring(i);
                    if (!text.startsWith("{{/" + helper)) {
                        throw new RuntimeException(
                                "Unbalanced tags. Start=" + beginTagText + " finished: " + text);
                    }

                } else {
                    helper = null;
                    exprText = startTag;
                    if (!text.startsWith("{{/" + exprText)) {
                        throw new RuntimeException(
                                "Unbalanced tags. Start=" + beginTagText + " finished: " + text);
                    }

                }

                try {
                    expr = jexl.createExpression(exprText);
                } catch (Exception e) {
                    throw new RuntimeException("Couldnt parse: " + exprText, e);
                }
                KDocument.KDomExpressionSection section;
                if (helper != null) {
                    section = doc.createHelperExpressionSection(helper, exprText, expr, currentChildren);
                } else {
                    section = doc.createExpressionSection(startTag, expr, currentChildren);
                }
                stack.remove(0);
                if (stack.isEmpty()) {
                    throw new RuntimeException("Unclosed tag: " + text);
                }
                currentChildren = stack.get(0);
                currentChildren.add(section);
                break;

            case TEXT:
                KDocument.KDomTextNode tn = doc.createTextNode(text);
                currentChildren.add(tn);
                break;

            }

        }
    };

    tokenizer.tokenize(s, handler);
    if (doc.getRoot() == null) {
        throw new RuntimeException("Did not get a root element");
    }
    return doc;
}

From source file:com.datatorrent.lib.appdata.schemas.SchemaUtils.java

/**
 * This is a utility method which loads the contents of a resource file into a string.
 * @param resource The resource file whose contents need to be loaded.
 * @return The contents of the specified resource file.
 *//*ww  w. j a  va 2  s.  c  om*/
public static String jarResourceFileToString(String resource) {
    StringWriter stringWriter = new StringWriter();
    try {
        InputStream is = SchemaUtils.class.getClassLoader().getResourceAsStream(resource);
        Preconditions.checkArgument(is != null, resource + " could not be found in the resources.");

        IOUtils.copy(is, stringWriter);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    return stringWriter.toString();
}

From source file:com.compomics.pladipus.core.control.updates.ProcessingBeanUpdater.java

private static void copyFromResources() {
    //loads the codontable from within the jar...
    if (beanXMLDefinitionFile != null && !beanXMLDefinitionFile.exists()) {
        try {//from  ww w .  j  a v  a 2  s .  c o m
            beanXMLDefinitionFile.getParentFile().mkdirs();
            beanXMLDefinitionFile.createNewFile();
            InputStream inputStream = new ClassPathResource("processing-beans.xml").getInputStream();
            OutputStream outputStream = new FileOutputStream(beanXMLDefinitionFile);
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException ex) {
            LOGGER.error(ex);
        }
    }
}

From source file:com.epam.wilma.gepard.testclient.compression.gzip.GzipCompressor.java

/**
 * Compress the input stream with GZIP.// ww  w.j ava2s.co m
 *
 * @param inputStream is the input
 * @return with compressed format
 * @throws IOException if problem occurs during the compression
 */
public InputStream compress(final InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(baos);
    //... Code to read from your original uncompressed data and write to gout.
    IOUtils.copy(inputStream, gout);
    gout.finish();
    //Convert to InputStream.
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:com.jeffy.hdfs.HDFSReadFile.java

/**
 * HadoopFileSystem API??/*from w  ww.  jav  a 2  s. co  m*/
 * 
 * @param path
 * @throws IOException 
 */
public static void readDataUseFileSystem(String path) throws IOException {

    Configuration config = new Configuration();
    /**
     * ?FileSystem????????
     *  public static FileSystem get(Configuration conf) throws IOException
     *   public static FileSystem get(URI uri, Configuration conf) throws IOException
     *   public static FileSystem get(URI uri, Configuration conf, String user)
     *   throws IOException
     */
    FileSystem fs = FileSystem.get(URI.create(path), config);
    //??FSDataInputStream,DataInputStream,????
    //open4KB
    try (InputStream in = fs.open(new Path(path))) {
        IOUtils.copy(in, System.out);
    }
}

From source file:com.boozallen.cognition.test.utils.TestResourceUtilsTest.java

@Test
public void testGetResource() throws Exception {
    URL resourceUrl = getResource(this.getClass(), "test-file.txt");
    try (InputStream stream = resourceUrl.openStream()) {
        StringWriter stringWriter = new StringWriter();
        IOUtils.copy(stream, stringWriter);
        assertThat(stringWriter.toString(), is(TEST_FILE_TEXT));
    }/*from www .ja v  a2  s .co m*/
}

From source file:com.sitewhere.configuration.FileSystemGlobalConfigurationResolver.java

/**
 * Get contents of a file as a byte array.
 * //from   w  w w  . j av a 2s .  c  o m
 * @param file
 * @return
 * @throws SiteWhereException
 */
public static byte[] getFileQuietly(File file) throws SiteWhereException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        FileInputStream in = new FileInputStream(file);
        IOUtils.copy(in, out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        return out.toByteArray();
    } catch (FileNotFoundException e) {
        throw new SiteWhereException(e);
    } catch (IOException e) {
        throw new SiteWhereException(e);
    }
}

From source file:com.adaptris.core.services.GzipService.java

/**
 *  @see com.adaptris.core.Service#doService(AdaptrisMessage)
 *//*from  w w  w  .java 2 s. c o m*/
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    InputStream in = null;
    GZIPOutputStream out = null;
    try {
        in = msg.getInputStream();
        out = new GZIPOutputStream(msg.getOutputStream());
        IOUtils.copy(in, out);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}