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.adaptris.core.stubs.MessageHelper.java

public static AdaptrisMessage createMessage(AdaptrisMessageFactory factory, String filename)
        throws IOException {
    AdaptrisMessage m = factory.newMessage();
    if (m instanceof FileBackedMessage) {
        ((FileBackedMessage) m).initialiseFrom(new File(filename));
    } else {//from   w  ww  . j av a  2  s.c  om
        OutputStream out = null;
        InputStream in = null;
        try {
            in = new FileInputStream(new File(filename));
            out = m.getOutputStream();
            IOUtils.copy(in, out);
        } finally {
            IOUtils.closeQuietly(out);
            IOUtils.closeQuietly(in);
        }
    }
    return m;
}

From source file:com.yattatech.dbtc.util.DbUtil.java

/**
 * Copies the application database to given folder, it' very useful
 * for debugging purpose only./*from ww  w . j a v a  2  s  .c o m*/
 * 
 * @param path
 * 
 */
public static void copyDatabase(String path) {
    final File folder = new File(path);
    final Context context = DBTCApplication.sApplicationContext;
    if (!folder.exists()) {
        folder.exists();
    }
    for (String database : context.databaseList()) {
        final File dbFile = context.getDatabasePath(database);
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(dbFile);
            out = new FileOutputStream(new File(folder, database));
            IOUtils.copy(in, out);
        } catch (IOException ioe) {
            Debug.d(TAG, "Failed:", ioe);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
}

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

/**
 * java.net.URL?HDFS??/*  w  ww.  j  av  a2  s  .  c  o m*/
 * 
 * @param path  hdfs://master:8020/tmp/user.csv
 */
public static void readDataUseURL(String path) {
    try (InputStream in = new URL(path).openStream()) {
        IOUtils.copy(in, System.out);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

public static int copy(Reader reader, Writer writer) throws IOException {
    try {/*w  w  w . j av a  2  s  . c om*/
        return IOUtils.copy(reader, writer);
    } finally {
        try {
            reader.close();
        } catch (Exception ignore) {
        }
        try {
            writer.close();
        } catch (Exception ignore) {
        }
    }
}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static void writeResponse(HttpServletResponse response, String data) {
    OutputStream output = null;/*w  ww  .  j a v  a 2 s . c  o  m*/
    InputStream input = null;
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        output = response.getOutputStream();
        input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
        IOUtils.copy(input, output);
        output.flush();
    } catch (Exception e) {
        throw new RuntimeException("?(flushResponse):" + e.getMessage());
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}

From source file:net.ontopia.utils.EncryptionUtils.java

/**
 * INTERNAL: Reads the file into memory, encrypting it in the
 * process, then writes the encrypted data back out to the file.
 *///w w w.j  a v a2 s . c om
public static void encrypt(File file) throws IOException {
    FileInputStream in = new FileInputStream(file);
    ByteArrayOutputStream tmpout = new ByteArrayOutputStream();
    encrypt(in, tmpout);
    in.close();

    FileOutputStream out = new FileOutputStream(file);
    ByteArrayInputStream src = new ByteArrayInputStream(tmpout.toByteArray());
    IOUtils.copy(src, out);
    out.close();
}

From source file:fitnesse.http.InputStreamResponse.java

public void readyToSend(ResponseSender sender) throws IOException {
    try {//from  w  w w.j a  v  a2 s .co  m
        addStandardHeaders();
        sender.send(makeHttpHeaders().getBytes());
        IOUtils.copy(input, sender.getOutputStream());
    } finally {
        sender.close();
    }
}

From source file:com.scorpio4.util.io.StreamCopy.java

public static void copy(InputStream input, OutputStream output) throws IOException {
    IOUtils.copy(input, output);
    output.flush();
}

From source file:net.sf.dsig.helpers.UserHomeSettingsParser.java

public static Properties parse() {
    try {//ww  w .  ja v a 2s .com
        String userHome = System.getProperty("user.home");
        File dsigFolder = new File(userHome, ".dsig");
        if (!dsigFolder.exists() && !dsigFolder.mkdir()) {
            throw new IOException("Could not create .dsig folder in user home directory");
        }

        File settingsFile = new File(dsigFolder, "settings.properties");
        if (!settingsFile.exists()) {
            InputStream is = UserHomeSettingsParser.class.getResourceAsStream("/defaultSettings.properties");
            if (is != null) {
                IOUtils.copy(is, new FileOutputStream(settingsFile));
            }
        }

        if (settingsFile.exists()) {
            Properties p = new Properties();
            FileInputStream fis = new FileInputStream(settingsFile);
            p.load(fis);
            IOUtils.closeQuietly(fis);
            return p;
        }
    } catch (IOException e) {
        logger.warn("Error while initialize settings", e);
    }

    return null;
}

From source file:com.technofovea.packbsp.PackbspUtil.java

/**
 * Efficiently copies a given file, returning a temporary copy which will be
 * removed when execution finishes./*ww w. j a  v a 2  s . c o  m*/
 * 
 * @param source Source file to copy.
 * @return The copied file.
 * @throws IOException
 */
public static final File createTempCopy(File source) throws IOException {
    String ext = FilenameUtils.getExtension(source.getName());
    File dest = File.createTempFile("packbsp_temp_", "." + ext);
    dest.deleteOnExit();
    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(dest);
    IOUtils.copy(fis, fos);
    fis.close();
    fos.close();
    return dest;
}