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.esri.gpt.framework.robots.BotsParserTest.java

@Before
public void setUp() throws IOException {
    InputStream inputRobots = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("com/esri/gpt/framework/robots/robots.txt");
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    IOUtils.copy(inputRobots, buffer);
    IOUtils.closeQuietly(inputRobots);/* w w  w.java  2  s  .c o m*/
    IOUtils.closeQuietly(buffer);

    robotsTxt = buffer.toString("UTF-8");
}

From source file:com.github.horrorho.inflatabledonkey.util.LZFSEExtInputStream.java

static void pipe(InputStream is, OutputStream os, Consumer<IOException> error) {
    logger.trace("<< pipe() - is: {} os: {} error: {}", is, os, error);
    try {/*from ww  w  . j  av a 2s .  c  o  m*/
        IOUtils.copy(is, os);
    } catch (IOException ex) {
        error.accept(ex);
    } finally {
        try {
            is.close();
        } catch (IOException ex) {
            error.accept(ex);
        }
        try {
            os.close();
        } catch (IOException ex) {
            error.accept(ex);
        }
        logger.trace(">> pipe()");
    }
}

From source file:com.talis.storage.s3.ExternalizableS3ObjectTest.java

public static void assertObjectsEqual(S3Object first, S3Object second) {
    try {//w  w w .  ja v  a  2s  .c  o m
        ByteArrayOutputStream b0 = new ByteArrayOutputStream();
        IOUtils.copy(first.getDataInputStream(), b0);
        ByteArrayOutputStream b1 = new ByteArrayOutputStream();
        IOUtils.copy(first.getDataInputStream(), b1);
        assertTrue(Arrays.equals(b0.toByteArray(), b1.toByteArray()));
    } catch (Exception e) {
        fail("Caught an Exception when reading entity bytes");
    }

    assertEquals(first.getContentType(), second.getContentType());
    assertEquals(first.getKey(), second.getKey());
    assertEquals(first.getBucketName(), second.getBucketName());
    assertEquals(first.getAcl(), second.getAcl());
    assertEquals(first.isMetadataComplete(), second.isMetadataComplete());
    assertEquals(first.getMetadataMap(), second.getMetadataMap());

}

From source file:com.mirth.connect.donkey.util.Base64Util.java

/**
 * Encodes binary data using the base64 algorithm and chunks the encoded output into 76
 * character blocks.//  w w  w .java2  s .c  o  m
 * This method sets the initial output buffer to a value that is "guaranteed" to be slightly
 * larger than necessary.
 * Therefore the buffer should never need to be expanded, making the maximum memory requirements
 * much lower than
 * using Base64.encodeBase64Chunked.
 * 
 * @param bytes
 * @param chunked
 * @return
 * @throws IOException
 */
public static byte[] encodeBase64(byte[] bytes, boolean chunked) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    // Set the size of the buffer to minimize peak memory usage
    // A Base64 encoded message takes roughly 1.333 times the memory of the raw binary, so use 1.4 to be safe.
    ByteArrayOutputStream baos = new ByteArrayOutputStream((int) (bytes.length * 1.4));
    Base64OutputStream b64os = null;
    if (chunked) {
        b64os = new Base64OutputStream(baos);
    } else {
        b64os = new Base64OutputStream(baos, true, 0, null);
    }

    // Perform the encoding
    IOUtils.copy(bais, b64os);

    // Free up any memory from the input
    b64os.close();

    return baos.toByteArray();
}

From source file:io.apicurio.studio.fe.servlet.servlets.ReadyServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///w w  w . java  2  s.com
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setHeader("Content-Type", "application/json");
    IOUtils.copy(new StringReader("{ \"up\": true }"), resp.getOutputStream());
    resp.getOutputStream().flush();
}

From source file:com.maydesk.base.util.ByteArrayImageReference.java

@Override
public void render(OutputStream out) throws IOException {
    try {// w ww.ja  va  2 s .co m
        IOUtils.copy(new ByteArrayInputStream(data), out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.autentia.web.rest.wadl.zipper.Zip.java

public void add(String name, InputStream inputStream) throws IOException {
    zipOutputStream.putNextEntry(new ZipEntry(name));

    IOUtils.copy(inputStream, zipOutputStream);

    zipOutputStream.closeEntry();/*  w w  w. java2 s.c  o  m*/
}

From source file:be.isl.desamouryv.sociall.service.FileServiceImpl.java

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Override//www.  j  a va  2 s.  c  o m
public String storeFile(InputStream input, String fileName, String uploadPath) throws IOException {
    logger.log(Level.INFO, "uploadPath: {0}", uploadPath);

    String prefix = FilenameUtils.getBaseName(fileName).replaceAll(" ", "");
    String suffix = FilenameUtils.getExtension(fileName);

    File tempFile = File.createTempFile(prefix + "-", "." + suffix, new File(uploadPath));
    OutputStream output = new FileOutputStream(tempFile);

    try {
        IOUtils.copy(input, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
    logger.log(Level.INFO, "file uploaded at: {0}", tempFile.getAbsolutePath());
    return tempFile.getName();
}

From source file:net.adamcin.oakpal.testing.TestPackageUtil.java

public static File prepareTestPackage(final String filename) throws IOException {
    File file = new File(testPackagesRoot, filename);
    if (file.exists()) {
        file.delete();/*  w  w  w. j a  va2  s .c o m*/
    }
    try (InputStream is = TestPackageUtil.class.getResourceAsStream(testPackagesSrc + filename);
            FileOutputStream fos = new FileOutputStream(file)) {
        IOUtils.copy(is, fos);
    }
    return file;
}

From source file:com.stratio.ingestion.morphline.commons.ContainsAnyOfTest.java

protected Config parse(String file, Config... overrides) throws IOException {
    File tmpFile = File.createTempFile("morphlines_", ".conf");
    IOUtils.copy(getClass().getResourceAsStream(file), new FileOutputStream(tmpFile));
    Config config = new org.kitesdk.morphline.base.Compiler().parse(tmpFile, overrides);
    config = config.getConfigList("morphlines").get(0);
    Preconditions.checkNotNull(config);/*w  w  w  .j a  v  a2 s.c  om*/
    return config;
}