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:de.micromata.genome.util.runtime.Log4JInitializer.java

/**
 * Copies the log4j.properties load from cp into file defined in localsettings if not exists
 *///from   w  w w . j  a  v a2s.c o  m
public static void copyLogConfigFileFromCp() {
    LocalSettings ls = LocalSettings.get();
    File log4jfile = new File(ls.get(LOG4J_PROPERTY_FILE, LOG4J_PROPERTY_FILE));
    if (log4jfile.exists() == true) {
        return;
    }
    try (InputStream is = Log4JInitializer.class.getClassLoader().getResourceAsStream(LOG4J_PROPERTY_FILE)) {
        if (is != null) {
            try (OutputStream os = new FileOutputStream(log4jfile)) {
                IOUtils.copy(is, os);
            }
        }
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }

}

From source file:com.rabbitmq.client.impl.ValueWriter.java

/** Public API - encodes a long string from a LongString. */
public final void writeLongstr(LongString str) throws IOException {
    writeLong((int) str.length());
    IOUtils.copy(str.getStream(), out);
}

From source file:com.android.email.mail.store.imap.ImapTempFileLiteral.java

ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException {
    mSize = stream.getLength();/* w  w  w  .j  a  v a 2 s .c  o m*/
    mFile = File.createTempFile("imap", ".tmp", Email.getTempDirectory());

    // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random
    // so it'd simply cause a memory leak.
    // deleteOnExit() simply adds filenames to a static list and the list will never shrink.
    // mFile.deleteOnExit();
    OutputStream out = new FileOutputStream(mFile);
    IOUtils.copy(stream, out);
    out.close();
}

From source file:FeatureExtraction.FeatureExtractorOOXMLStructuralPathsInputStream.java

private void ExtractStructuralFeaturesInMemory(InputStream fileInputStream,
        Map<String, Integer> structuralPaths) {
    String path;//w w w. j  av  a2 s. c  o  m
    String directoryPath;
    String fileExtension;
    try {
        File file = new File(fileInputStream.hashCode() + "");
        try (FileOutputStream fos = new FileOutputStream(file)) {
            IOUtils.copy(fileInputStream, fos);
        }

        ZipFile zipFile = new ZipFile(file);

        for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            path = entry.getName();
            directoryPath = FilenameUtils.getFullPath(path);
            fileExtension = FilenameUtils.getExtension(path);

            AddStructuralPath(directoryPath, structuralPaths);
            AddStructuralPath(path, structuralPaths);

            if (fileExtension.equals("rels") || fileExtension.equals("xml")) {
                AddXMLStructuralPaths(zipFile.getInputStream(entry), path, structuralPaths);
            }
        }
    } catch (IOException ex) {
        Console.PrintException(String.format("Error extracting OOXML structural features from file in memory"),
                ex);
    }
}

From source file:gov.nih.nci.caintegrator.web.action.StudyLogoResult.java

/**
 * {@inheritDoc}//from   w  w  w.j a v  a2 s .  c o m
 */
public void execute(ActionInvocation invocation) throws IOException {
    StudyLogo studyLogo = SessionHelper.getInstance().getDisplayableUserWorkspace().getStudyLogo();
    HttpServletResponse response = ServletActionContext.getResponse();
    HttpServletRequest request = ServletActionContext.getRequest();
    String imagePath = "";
    imagePath = retrieveImagePath(request, response, studyLogo);
    OutputStream outputStream = response.getOutputStream();
    File imageFile = new File(imagePath);
    FileInputStream inputStream = new FileInputStream(imageFile);
    IOUtils.copy(inputStream, outputStream);
    outputStream.close();
}

From source file:com.gallatinsystems.framework.servlet.MultiReadHttpServletRequest.java

private void cacheInputStream() throws IOException {
    cachedBytes = new ByteArrayOutputStream();
    IOUtils.copy(super.getInputStream(), cachedBytes);
}

From source file:com.ibm.jaggr.core.util.CopyUtil.java

public static void copy(String str, Writer writer) throws IOException {
    Reader reader = new StringReader(str);
    try {/* w w  w.jav  a  2 s .c  om*/
        IOUtils.copy(reader, writer);
    } finally {
        try {
            reader.close();
        } catch (Exception ignore) {
        }
        try {
            writer.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:net.lshift.diffa.assets.JstTemplatesPreProcessor.java

public void process(Resource resource, Reader input, Writer output) throws IOException {
    if (resource.getUri().endsWith(".jst")) {
        String content = IOUtils.toString(input);
        String name = removeSuffix(removePrefix(resource.getUri(), "/js/templates/"), ".jst");

        output.write("window.JST = (window.JST || {});\n");
        output.write(String.format("window.JST['%s'] = _.template(\n", name));

        List<String> result = new ArrayList<String>();
        for (String l : content.split("\n")) {
            result.add("\"" + StringEscapeUtils.escapeJava(l) + "\"");
        }// ww  w  .ja  v  a2  s.  c  o m
        output.write(StringUtils.join(result.iterator(), " + \n"));
        output.write(");\n");
    } else {
        IOUtils.copy(input, output);
    }
}

From source file:de.onyxbits.raccoon.net.ResourceHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    response.setContentType("application/octet-stream");
    response.setStatus(HttpServletResponse.SC_OK);
    if (target.toLowerCase().endsWith("png")) {
        response.setContentType("image/png");
    }//  w  w  w. j  a  v a 2s . co  m
    if (target.toLowerCase().endsWith("css")) {
        response.setContentType("text/css");
    }

    // Prevent path traversal -> all resources must be in /web, no sub
    // directories.
    String tmp = target.substring(target.lastIndexOf('/'), target.length());

    InputStream in = getClass().getResourceAsStream("/web" + tmp);
    OutputStream out = response.getOutputStream();

    IOUtils.copy(in, out);
    in.close();
    out.flush();
    out.close();

    baseRequest.setHandled(true);
}

From source file:com.twitter.common.text.token.TokenStream2LuceneTokenizerWrapper.java

@Override
public void reset(Reader input) throws IOException {
    Preconditions.checkNotNull(input);/*from   w  w  w.ja v  a  2s.c o  m*/
    StringWriter writer = new StringWriter();
    IOUtils.copy(input, writer);
    stream.reset(writer.toString());
}