Example usage for org.apache.commons.io IOUtils write

List of usage examples for org.apache.commons.io IOUtils write

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils write.

Prototype

public static void write(StringBuffer data, OutputStream output) throws IOException 

Source Link

Document

Writes chars from a StringBuffer to bytes on an OutputStream using the default character encoding of the platform.

Usage

From source file:com.adaptris.core.services.jdbc.types.StringColumnTranslator.java

@Override
public void write(JdbcResultRow rs, String columnName, OutputStream out) throws SQLException, IOException {
    IOUtils.write(toString(rs.getFieldValue(columnName)), out);
}

From source file:com.canoo.webtest.util.FileUtil.java

/**
 * Writes a String to a file.//w  w w  .j  av  a2  s  .c o  m
 *
 * @param file
 * @param content
 * @param step
 * @throws StepExecutionException if something goes wrong
 */
public static void writeStringToFile(final File file, final String content, final Step step) {
    String canonicalPath = null;
    FileOutputStream outputStream = null;
    try {
        canonicalPath = file.getCanonicalPath();
        outputStream = new FileOutputStream(file);
        IOUtils.write(content, outputStream);
    } catch (IOException e) {
        throw new StepExecutionException("Could not find/write \"" + canonicalPath + "\".", step);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

From source file:eu.scidipes.toolkits.palibrary.utils.zip.ZipUtils.java

/**
 * Zips one or more {@link ByteArrayZipEntry} objects together and returns the archive as a base64-encoded
 * <code>String</code>/*from   w ww .jav a 2 s.  co  m*/
 * 
 * @param entries
 * @return a base64-encoded <code>String</code> which is the zip archive of the passed entries
 * 
 * @throws IOException
 *             in the event of an exception writing to the zip output stream
 * @throws IllegalArgumentException
 *             if passed entries is null or empty
 */
public static String byteArrayZipEntriesToBase64(final Set<? extends ByteArrayZipEntry> entries)
        throws IOException {
    if (entries == null || entries.isEmpty()) {
        throw new IllegalArgumentException("entries cannot be null or empty");
    }

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try (final ZipOutputStream zos = new ZipOutputStream(bos)) {
        for (final ByteArrayZipEntry entry : entries) {
            zos.putNextEntry(entry.getZipEntry());
            IOUtils.write(entry.getBytes(), zos);
            zos.closeEntry();
        }
    }

    return Base64.encodeBase64String(bos.toByteArray());
}

From source file:edu.cmu.cs.diamond.android.Filter.java

public Filter(int resourceId, Context context, String name, String[] args, byte[] blob) throws IOException {
    Resources r = context.getResources();
    String resourceName = r.getResourceEntryName(resourceId);
    File f = context.getFileStreamPath(resourceName);

    if (!f.exists()) {
        InputStream ins = r.openRawResource(resourceId);
        byte[] buf = IOUtils.toByteArray(ins);
        FileOutputStream fos = context.openFileOutput(resourceName, Context.MODE_PRIVATE);
        IOUtils.write(buf, fos);
        context.getFileStreamPath(resourceName).setExecutable(true);
        fos.close();//from  w w  w .j  a  v  a  2 s . co m
    }

    ProcessBuilder pb = new ProcessBuilder(f.getAbsolutePath());
    Map<String, String> env = pb.environment();
    tempDir = File.createTempFile("filter", null, context.getCacheDir());
    tempDir.delete(); // Delete file and create directory.
    if (!tempDir.mkdir()) {
        throw new IOException("Unable to create temporary directory.");
    }
    env.put("TEMP", tempDir.getAbsolutePath());
    env.put("TMPDIR", tempDir.getAbsolutePath());
    proc = pb.start();
    is = proc.getInputStream();
    os = proc.getOutputStream();

    sendInt(1);
    sendString(name);
    sendStringArray(args);
    sendBinary(blob);

    while (this.getNextToken().tag != TagEnum.INIT)
        ;
    Log.d(TAG, "Filter initialized.");
}

From source file:com.fluidops.iwb.cms.util.OpenUp.java

public static List<Statement> extract(String text, URI uri) throws IOException {
    List<Statement> res = new ArrayList<Statement>();
    String textEncoded = URLEncoder.encode(text, "UTF-8");

    String send = "format=rdfxml&text=" + textEncoded;

    // service limit is 10000 chars
    if (mode() == Mode.demo)
        send = stripToDemoLimit(send);/*w ww.j  a  v a 2  s.  c  o m*/

    // GET only works for smaller texts
    // URL url = new URL("http://openup.tso.co.uk/des/enriched-text?text=" + textEncoded + "&format=rdfxml");

    URL url = new URL(getConfig().getOpenUpUrl());
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    IOUtils.write(send, conn.getOutputStream());
    Repository repository = new SailRepository(new MemoryStore());
    try {
        repository.initialize();
        RepositoryConnection con = repository.getConnection();
        con.add(conn.getInputStream(), uri.stringValue(), RDFFormat.RDFXML);
        RepositoryResult<Statement> iter = con.getStatements(null, null, null, false);
        while (iter.hasNext()) {
            Statement s = iter.next();
            res.add(s);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return res;
}

From source file:ch.cyberduck.core.cryptomator.ContentWriter.java

public void write(final Path file, final byte[] content, final TransferStatus status)
        throws BackgroundException {
    final Write<?> write = session._getFeature(Write.class);
    status.setLength(content.length);/*from w  ww  . jav  a 2  s.  com*/
    status.setChecksum(write.checksum(file).compute(new ByteArrayInputStream(content), status));
    final StatusOutputStream<?> out = write.write(file, status, new DisabledConnectionCallback());
    try {
        IOUtils.write(content, out);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    } finally {
        new DefaultStreamCloser().close(out);
    }
}

From source file:ctrus.pa.bow.core.UnWeightedBagOfWords.java

public final void writeTo(OutputStream out) throws IOException {

    Iterator<String> terms = _terms.iterator();
    while (terms.hasNext())
        IOUtils.write(terms.next() + " ", out);
    IOUtils.write("\n", out);
    out.flush();/*from  w w w  .j  a v  a 2s  . com*/
}

From source file:mase.jbot.PresetCreator.java

private static void fillWeights(File file, double[] weights) throws IOException {
    StringBuilder sb = new StringBuilder();
    sb.append("weights=(");
    for (int i = 0; i < weights.length - 1; i++) {
        sb.append(weights[i]).append(",");
    }//from   w w w.j  a v a  2s. c  o  m
    sb.append(weights[weights.length - 1]).append(")");

    String descr = "description=(" + file.getName().replace("preset_", "").replace(".conf", "") + ")";

    String content = IOUtils.toString(new FileInputStream(file));
    content = content.replaceAll("weights\\s*=\\s*\\(.*\\)", sb.toString());
    content = content.replaceAll("description\\s*=\\s*\\(.*\\)", descr);
    System.out.println("Writting: " + file.getAbsolutePath() + " " + weights.length + " weights.");
    IOUtils.write(content, new FileOutputStream(file));
}

From source file:de.knurt.fam.template.controller.json.PublicDocController.java

@Override
public ModelAndView handleRequest(HttpServletRequest rq, HttpServletResponse rs) {
    PrintWriter pw = null;/*from   ww  w . j  a  v a 2 s  . c om*/
    try {
        rs.setContentType("application/json");
        pw = rs.getWriter();
        String result = "{}";
        if (rq.getParameter("doc") != null && this.isAllowedToShow(rq, rq.getParameter("doc"))) {
            result = FamCouchDBDao.getInstance().getContentAsString(rq.getParameter("doc"));
        }
        IOUtils.write(result, pw);
    } catch (IOException ex) {
        FamLog.exception(ex, 201204191241l);
    } finally {
        IOUtils.closeQuietly(pw);
    }
    return null;
}

From source file:b2s.idea.mavenize.AppTest.java

@Before
public void setUp() throws Exception {
    output = new ByteArrayOutputStream();

    App.jarCombiner = combiner;//from ww w  .  j a v  a  2s  .  com
    originalSysOut = System.out;
    System.setOut(new PrintStream(output));

    ideaFolder = temporaryFolder.newFolder();
    outputFolder = temporaryFolder.newFolder();
    ideaLibFolder = new File(ideaFolder, "lib");
    IOUtils.write("build-version", new FileOutputStream(new File(ideaFolder, "build.txt")));
}