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:ch.cyberduck.core.b2.B2LargeUploadServiceTest.java

@Test
public void testAppendSecondPart() throws Exception {
    final B2Session session = new B2Session(new Host(new B2Protocol(), new B2Protocol().getDefaultHostname(),
            new Credentials(System.getProperties().getProperty("b2.user"),
                    System.getProperties().getProperty("b2.key"))));
    session.open(new DisabledHostKeyCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final Path test = new Path(bucket, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final int length = 102 * 1024 * 1024;
    final byte[] content = RandomUtils.nextBytes(length);
    IOUtils.write(content, local.getOutputStream(false));
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);//w  w  w .  j  a v a 2 s. co  m
    final AtomicBoolean interrupt = new AtomicBoolean();
    try {
        new B2LargeUploadService(session, new B2WriteFeature(session), 100L * 1024L * 1024L, 1).upload(test,
                local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener() {
                    long count;

                    @Override
                    public void sent(final long bytes) {
                        count += bytes;
                        if (count >= 101L * 1024L * 1024L) {
                            throw new RuntimeException();
                        }
                    }
                }, status, new DisabledLoginCallback());
    } catch (BackgroundException e) {
        // Expected
        interrupt.set(true);
    }
    assertTrue(interrupt.get());
    assertEquals(100L * 1024L * 1024L, status.getOffset(), 0L);
    assertFalse(status.isComplete());

    final TransferStatus append = new TransferStatus().append(true).length(2L * 1024L * 1024L)
            .skip(100L * 1024L * 1024L);
    new B2LargeUploadService(session, new B2WriteFeature(session), 100L * 1024L * 1024L, 1).upload(test, local,
            new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), append,
            new DisabledLoginCallback());
    assertEquals(102L * 1024L * 1024L, append.getOffset(), 0L);
    assertTrue(append.isComplete());
    assertTrue(new B2FindFeature(session).find(test));
    assertEquals(102L * 1024L * 1024L, new B2AttributesFinderFeature(session).find(test).getSize(), 0L);
    final byte[] buffer = new byte[content.length];
    final InputStream in = new B2ReadFeature(session).read(test, new TransferStatus(),
            new DisabledConnectionCallback());
    IOUtils.readFully(in, buffer);
    in.close();
    assertArrayEquals(content, buffer);
    new B2DeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
    local.delete();
    session.close();
}

From source file:eionet.gdem.qa.XQueryTask.java

/**
 * Run XQuery script: steps: - download the source from URL - run XQuery - store the result in a text file.
 *///from w  w w.j a  va 2 s .  c  o m
@Override
public void run() {
    try {
        LOGGER.info("Job ID=  " + jobId + " started getting source file.");

        String srcFile = null;
        srcFile = url;

        // status to -processing
        changeStatus(Constants.XQ_PROCESSING);

        // Do validation
        if (queryID.equals(String.valueOf(Constants.JOB_VALIDATION))) {
            LOGGER.info("Job ID=" + jobId + " Validation started");

            try {
                // validate only the first XML Schema
                if (scriptFile.contains(" ")) {
                    scriptFile = StringUtils.substringBefore(scriptFile, " ");
                }
                LOGGER.info("** XQuery starts, ID=" + jobId + " schema: " + scriptFile
                        + " result will be stored to " + resultFile);
                ValidationService vs = new ValidationService();

                // XML Schema should be in schemaLocation attribute
                String result = vs.validateSchema(srcFile, scriptFile);

                LOGGER.debug("Validation proceeded, now store to the result file");

                Utils.saveStrToFile(resultFile, result, null);
            } catch (Exception e) {
                handleError("Error during validation:" + e.toString(), true);
                return;
            }
        } else {
            // Do xq job
            LOGGER.info("Job ID=" + jobId + " XQ processing started");

            // read query info from DB.
            Map query = getQueryInfo(queryID);
            String contentType = null;
            Schema schema = null;
            boolean schemaExpired = false;
            boolean isNotLatestReleasedDDSchema = false;

            if (query != null && query.containsKey("content_type")) {
                contentType = (String) query.get("content_type");
            }
            // get script type if it comes from T_QUERY table
            if (query != null && query.containsKey("script_type")) {
                scriptType = (String) query.get("script_type");
            }

            // stylesheet - to check if it is expired
            if (query != null && query.containsKey("xml_schema")) {
                // set schema if exists:
                schema = getSchema((String) query.get("xml_schema"));
                schemaExpired = (schema != null && schema.isExpired());
                isNotLatestReleasedDDSchema = DataDictUtil.isDDSchemaAndNotLatestReleased(schema.getSchema());

            }

            // get script type if it stored in filesystem and we have to
            // guess it by file extension
            if (Utils.isNullStr(scriptType)) {
                scriptType = scriptFile.endsWith(XQScript.SCRIPT_LANG_XSL) ? XQScript.SCRIPT_LANG_XSL
                        : scriptFile.endsWith(XQScript.SCRIPT_LANG_XGAWK) ? XQScript.SCRIPT_LANG_XGAWK
                                : XQScript.SCRIPT_LANG_XQUERY1;
            }
            String[] xqParam = { Constants.XQ_SOURCE_PARAM_NAME + "=" + srcFile };

            try {
                if (scriptFile.contains(" ")) {
                    scriptFile = StringUtils.substringBefore(scriptFile, " ");
                }
                LOGGER.info("** XQuery starts, ID=" + jobId + " params: "
                        + (xqParam == null ? "<< no params >>" : xqParam[0]) + " result will be stored to "
                        + resultFile);
                LOGGER.debug("Script: \n" + scriptFile);
                XQScript xq = new XQScript(null, xqParam, contentType);
                xq.setScriptFileName(scriptFile);
                xq.setScriptType(scriptType);
                xq.setSrcFileUrl(srcFile);
                xq.setSchema(schema);

                if (XQScript.SCRIPT_LANG_FME.equals(scriptType)) {
                    if (query != null && query.containsKey("url")) {
                        xq.setScriptSource((String) query.get("url"));
                    }
                }

                FileOutputStream out = null;
                try {
                    // if result type is HTML and schema is expired parse
                    // result (add warning) before writing to file
                    if ((schemaExpired || isNotLatestReleasedDDSchema)
                            && contentType.equals(XQScript.SCRIPT_RESULTTYPE_HTML)) {
                        String res = xq.getResult();
                        Utils.saveStrToFile(resultFile, res, null);
                    } else {
                        out = new FileOutputStream(new File(resultFile));
                        xq.getResult(out);
                    }
                } catch (IOException ioe) {
                    throw new GDEMException(ioe.toString());
                } catch (GDEMException e) {
                    // store error in feedback, it could be XML processing error
                    StringBuilder errBuilder = new StringBuilder();
                    errBuilder.append(
                            "<div class=\"feedbacktext\"><span id=\"feedbackStatus\" class=\"BLOCKER\" style=\"display:none\">Unexpected error occured!</span><h2>Unexpected error occured!</h2>");
                    errBuilder.append(Utils.escapeXML(e.toString()));
                    errBuilder.append("</div>");
                    IOUtils.write(errBuilder.toString(), out);
                } finally {
                    IOUtils.closeQuietly(out);
                }

                LOGGER.debug("Script proceeded, now store to the result file");

            } catch (Exception e) {
                handleError("Error processing QA script:" + e.toString(), true);
                return;

            }
        }

        changeStatus(Constants.XQ_READY);

        // TODO: Change to failed if script has failed
        LOGGER.info("Job ID=" + jobId + " succeeded");

        // all done, thread stops here, job is waiting for pulling from the
        // client side

    } catch (Exception ee) {
        handleError("Error in thread run():" + ee.toString(), true);
    }
}

From source file:ch.cyberduck.core.dav.DAVReadFeatureTest.java

@Test
public void testReadRangeUnknownLength() throws Exception {
    final Host host = new Host(new DAVProtocol(), "test.cyberduck.ch",
            new Credentials(System.getProperties().getProperty("webdav.user"),
                    System.getProperties().getProperty("webdav.password")));
    host.setDefaultPath("/dav/basic");
    final DAVSession session = new DAVSession(host);
    session.open(new DisabledHostKeyCallback());
    session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
    final Path test = new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(),
            EnumSet.of(Path.Type.file));
    session.getFeature(Touch.class).touch(test, new TransferStatus());

    final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    final byte[] content = new RandomStringGenerator.Builder().build().generate(1000).getBytes();
    final OutputStream out = local.getOutputStream(false);
    assertNotNull(out);/*from  www . j  av a 2 s  .co m*/
    IOUtils.write(content, out);
    out.close();
    new DAVUploadFeature(new DAVWriteFeature(session)).upload(test, local,
            new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);
    status.setAppend(true);
    status.setOffset(100L);
    final InputStream in = new DAVReadFeature(session).read(test, status, new DisabledConnectionCallback());
    assertNotNull(in);
    final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length - 100);
    new StreamCopier(status, status).transfer(in, buffer);
    final byte[] reference = new byte[content.length - 100];
    System.arraycopy(content, 100, reference, 0, content.length - 100);
    assertArrayEquals(reference, buffer.toByteArray());
    in.close();
    new DAVDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
    session.close();
}