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

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

Introduction

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

Prototype

public static long copyLarge(Reader input, Writer output) throws IOException 

Source Link

Document

Copy chars from a large (over 2GB) Reader to a Writer.

Usage

From source file:org.akubraproject.rmi.client.ClientConnection.java

@Override
public Blob getBlob(InputStream in, long estimatedSize, Map<String, String> hints) throws IOException {
    ensureOpen();//  w ww  .ja v a2 s  . co m
    if (in == null)
        throw new NullPointerException();

    RemoteBlobCreator bc = remote.getBlobCreator(estimatedSize, hints);
    RemoteBlob rb = null;
    try {
        OutputStream out = new ClientOutputStream(bc);
        IOUtils.copyLarge(in, out);
        out.close();
        rb = bc.shutDown(false);
        bc = null;
    } finally {
        try {
            if (bc != null)
                bc.shutDown(true);
        } catch (Throwable t) {
            log.warn("Failed to shutdown remote blob creator", t);
        }
    }

    if (rb == null)
        throw new NullPointerException();

    return new ClientBlob(this, streamManager, rb);
}

From source file:org.akubraproject.rmi.TransactionalStoreTest.java

@Test
public void testMTStress() throws InterruptedException, ExecutionException {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);
    List<Future<Void>> futures = new ArrayList<Future<Void>>();

    for (int loop = 0; loop < 30; loop++) {
        futures.add(executor.submit(new Callable<Void>() {
            public Void call() throws Exception {
                for (int i = 0; i < 10; i++) {
                    doInTxn(new Action() {
                        public void run(BlobStoreConnection con) throws Exception {
                            for (int j = 0; j < 3; j++) {
                                URI id = URI.create("urn:mt:" + UUID.randomUUID());
                                byte[] buf = new byte[4096];
                                Blob b;
                                b = con.getBlob(id, null);
                                OutputStream out;
                                IOUtils.copyLarge(new ByteArrayInputStream(buf),
                                        out = b.openOutputStream(buf.length, true));
                                out.close();

                                InputStream in;
                                assertEquals(buf, IOUtils.toByteArray(in = b.openInputStream()));
                                in.close();
                                b.delete();
                            }/* w w w  .j  av  a2  s .  c om*/
                        }
                    }, true);
                }
                return null;
            }
        }));
    }

    for (Future<Void> res : futures)
        res.get();
}

From source file:org.apache.accumulo.start.Test.java

private void copyJar(URL jar) throws Exception {
    if (destJar.exists()) {
        destJar.delete();//from   w w w .  ja  v  a2 s  .  co m
    }
    // make sure the new jar has a different timestamp
    // must sleep at least 1 sec between creating jars, because java caches zip files
    // based on last mod time... the granularity of last mod time is only to the
    // second! If you create, deleted, create a jar file in the same second java will
    // not pick up the new jar (even if using a new URLClassLoader because caching is in
    // the native zip code and is based on file name and last access time)

    destJar.createNewFile();
    destJar.deleteOnExit();
    OutputStream os = new FileOutputStream(destJar);

    File jarFile = new File(jar.toURI());
    InputStream is = new FileInputStream(jarFile);

    IOUtils.copyLarge(is, os);
    is.close();
    os.close();
    // give the class loader time to pick up the new jar
    Thread.sleep(1500);
}

From source file:org.apache.accumulo.tserver.log.TestUpgradePathForWALogs.java

@Test
public void testUpgradeOf15WALog() throws IOException {
    InputStream walogStream = null;
    OutputStream walogInHDFStream = null;

    try {/*from ww  w. j  a va2  s. c  om*/

        walogStream = getClass().getResourceAsStream(WALOG_FROM_15);
        walogInHDFStream = new FileOutputStream(new File(root.getRoot().getAbsolutePath() + WALOG_FROM_15));

        IOUtils.copyLarge(walogStream, walogInHDFStream);
        walogInHDFStream.flush();
        walogInHDFStream.close();
        walogInHDFStream = null;

        LogSorter logSorter = new LogSorter(null, fs, SiteConfiguration.getInstance());
        LogSorter.LogProcessor logProcessor = logSorter.new LogProcessor();

        logProcessor.sort(WALOG_FROM_15, new Path("file://" + root.getRoot().getAbsolutePath() + WALOG_FROM_15),
                "file://" + root.getRoot().getAbsolutePath() + "/manyMaps");

    } finally {
        if (walogStream != null) {
            walogStream.close();
        }

        if (walogInHDFStream != null) {
            walogInHDFStream.close();
        }
    }
}

From source file:org.apache.accumulo.tserver.log.TestUpgradePathForWALogs.java

@Test
public void testBasic16WALogRead() throws IOException {
    String walogToTest = WALOG_FROM_16;

    InputStream walogStream = null;
    OutputStream walogInHDFStream = null;

    try {/*from ww w. jav  a  2s. c  o  m*/

        walogStream = getClass().getResourceAsStream(walogToTest);
        walogInHDFStream = new FileOutputStream(new File(root.getRoot().getAbsolutePath() + walogToTest));

        IOUtils.copyLarge(walogStream, walogInHDFStream);
        walogInHDFStream.flush();
        walogInHDFStream.close();
        walogInHDFStream = null;

        LogSorter logSorter = new LogSorter(null, fs, SiteConfiguration.getInstance());
        LogSorter.LogProcessor logProcessor = logSorter.new LogProcessor();

        logProcessor.sort(walogToTest, new Path("file://" + root.getRoot().getAbsolutePath() + walogToTest),
                "file://" + root.getRoot().getAbsolutePath() + "/manyMaps");

    } finally {
        if (walogStream != null) {
            walogStream.close();
        }

        if (walogInHDFStream != null) {
            walogInHDFStream.close();
        }
    }
}

From source file:org.apache.fop.afp.apps.FontPatternExtractor.java

/**
 * Extracts the Type1 PFB file from the given AFP outline font.
 * @param file the AFP file to read from
 * @param targetDir the target directory where the PFB file is to be placed.
 * @throws IOException if an I/O error occurs
 *///  w  w w.j a v a  2 s  . c o  m
public void extract(File file, File targetDir) throws IOException {
    InputStream in = new java.io.FileInputStream(file);
    try {
        MODCAParser parser = new MODCAParser(in);
        ByteArrayOutputStream baout = new ByteArrayOutputStream();
        UnparsedStructuredField strucField;
        while ((strucField = parser.readNextStructuredField()) != null) {
            if (strucField.getSfTypeID() == 0xD3EE89) {
                byte[] sfData = strucField.getData();
                println(strucField.toString());
                HexDump.dump(sfData, 0, printStream, 0);
                baout.write(sfData);
            }
        }

        ByteArrayInputStream bin = new ByteArrayInputStream(baout.toByteArray());
        DataInputStream din = new DataInputStream(bin);
        long len = din.readInt() & 0xFFFFFFFFL;
        println("Length: " + len);
        din.skip(4); //checksum
        int tidLen = din.readUnsignedShort() - 2;
        byte[] tid = new byte[tidLen];
        din.readFully(tid);
        String filename = new String(tid, "ISO-8859-1");
        int asciiCount1 = countUSAsciiCharacters(filename);
        String filenameEBCDIC = new String(tid, "Cp1146");
        int asciiCount2 = countUSAsciiCharacters(filenameEBCDIC);
        println("TID: " + filename + " " + filenameEBCDIC);

        if (asciiCount2 > asciiCount1) {
            //Haven't found an indicator if the name is encoded in EBCDIC or not
            //so we use a trick.
            filename = filenameEBCDIC;
        }
        if (!filename.toLowerCase().endsWith(".pfb")) {
            filename = filename + ".pfb";
        }
        println("Output filename: " + filename);
        File out = new File(targetDir, filename);

        OutputStream fout = new java.io.FileOutputStream(out);
        try {
            IOUtils.copyLarge(din, fout);
        } finally {
            IOUtils.closeQuietly(fout);
        }

    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

private Object readCOSStream(COSStream originalStream, Object keyBase) throws IOException {
    InputStream in;/*  www.  j  a va  2s . com*/
    Set filter;
    if (pdfDoc.isEncryptionActive() || (originalStream.containsKey(COSName.DECODE_PARMS)
            && !originalStream.containsKey(COSName.FILTER))) {
        in = originalStream.getUnfilteredStream();
        filter = FILTER_FILTER;
    } else {
        //transfer encoded data (don't reencode)
        in = originalStream.getFilteredStream();
        filter = Collections.EMPTY_SET;
    }
    PDFStream stream = new PDFStream();
    OutputStream out = stream.getBufferOutputStream();
    IOUtils.copyLarge(in, out);
    transferDict(originalStream, stream, filter);
    return cacheClonedObject(keyBase, stream);
}

From source file:org.apache.fop.render.pdf.PDFRenderingUtil.java

/**
 * Adds an embedded file to the PDF file.
 * @param embeddedFile the object representing the embedded file to be added
 * @throws IOException if an I/O error occurs
 *//*from   w  w  w.jav  a2  s.c o  m*/
public void addEmbeddedFile(PDFEmbeddedFileExtensionAttachment embeddedFile) throws IOException {
    this.pdfDoc.getProfile().verifyEmbeddedFilesAllowed();
    PDFNames names = this.pdfDoc.getRoot().getNames();
    if (names == null) {
        //Add Names if not already present
        names = this.pdfDoc.getFactory().makeNames();
        this.pdfDoc.getRoot().setNames(names);
    }

    //Create embedded file
    PDFEmbeddedFile file = new PDFEmbeddedFile();
    this.pdfDoc.registerObject(file);
    Source src = getUserAgent().resolveURI(embeddedFile.getSrc());
    InputStream in = ImageUtil.getInputStream(src);
    if (in == null) {
        throw new FileNotFoundException(embeddedFile.getSrc());
    }
    try {
        OutputStream out = file.getBufferOutputStream();
        IOUtils.copyLarge(in, out);
    } finally {
        IOUtils.closeQuietly(in);
    }
    PDFDictionary dict = new PDFDictionary();
    dict.put("F", file);
    String filename = PDFText.toPDFString(embeddedFile.getFilename(), '_');
    PDFFileSpec fileSpec = new PDFFileSpec(filename);
    fileSpec.setEmbeddedFile(dict);
    if (embeddedFile.getDesc() != null) {
        fileSpec.setDescription(embeddedFile.getDesc());
    }
    this.pdfDoc.registerObject(fileSpec);

    //Make sure there is an EmbeddedFiles in the Names dictionary
    PDFEmbeddedFiles embeddedFiles = names.getEmbeddedFiles();
    if (embeddedFiles == null) {
        embeddedFiles = new PDFEmbeddedFiles();
        this.pdfDoc.assignObjectNumber(embeddedFiles);
        this.pdfDoc.addTrailerObject(embeddedFiles);
        names.setEmbeddedFiles(embeddedFiles);
    }

    //Add to EmbeddedFiles in the Names dictionary
    PDFArray nameArray = embeddedFiles.getNames();
    if (nameArray == null) {
        nameArray = new PDFArray();
        embeddedFiles.setNames(nameArray);
    }
    String name = PDFText.toPDFString(filename);
    nameArray.add(name);
    nameArray.add(new PDFReference(fileSpec));
}

From source file:org.apache.fop.util.BitmapImageUtilTestCase.java

private String toHex(byte[] byteArray) throws IOException {
    InputStream in = new java.io.ByteArrayInputStream(byteArray);
    StringWriter writer = new StringWriter();
    WriterOutputStream wo = new WriterOutputStream(writer, "US-ASCII");
    ASCIIHexOutputStream hex = new ASCIIHexOutputStream(wo);
    IOUtils.copyLarge(in, hex);
    return writer.toString();
}

From source file:org.apache.gobblin.runtime.crypto.DecryptCli.java

@Override
public void run(String[] args) {
    try {// w  w  w  .j a  v  a2s .  co  m
        Options options = new Options();
        options.addOption(KEYSTORE_LOCATION);
        options.addOption(INPUT_LOCATION);
        options.addOption(OUTPUT_LOCATION);

        CommandLine cli;
        try {
            CommandLineParser parser = new DefaultParser();
            cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length));
        } catch (ParseException pe) {
            System.out.println("Command line parse exception: " + pe.getMessage());
            printUsage(options);
            return;
        }

        Map<String, Object> encryptionProperties = ImmutableMap.<String, Object>of(
                EncryptionConfigParser.ENCRYPTION_ALGORITHM_KEY, "aes_rotating",
                EncryptionConfigParser.ENCRYPTION_KEYSTORE_PATH_KEY,
                cli.getOptionValue(KEYSTORE_LOCATION.getOpt()),
                EncryptionConfigParser.ENCRYPTION_KEYSTORE_PASSWORD_KEY, getPasswordFromConsole());

        InputStream fIs = new BufferedInputStream(
                new FileInputStream(new File(cli.getOptionValue(INPUT_LOCATION.getOpt()))));
        InputStream cipherStream = EncryptionFactory.buildStreamCryptoProvider(encryptionProperties)
                .decodeInputStream(fIs);

        OutputStream out = new BufferedOutputStream(
                new FileOutputStream(cli.getOptionValue(OUTPUT_LOCATION.getOpt())));

        long bytes = IOUtils.copyLarge(cipherStream, out);
        out.flush();
        out.close();
        System.out.println("Copied " + String.valueOf(bytes) + " bytes.");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}