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.archive.io.GenericReplayCharSequence.java

/**
 * Converts the first <code>Integer.MAX_VALUE</code> characters from the
 * file <code>backingFilename</code> from encoding <code>encoding</code> to
 * encoding <code>WRITE_ENCODING</code> and saves as
 * <code>this.decodedFile</code>, which is named <code>backingFilename
 * + "." + WRITE_ENCODING</code>./*from www.j  a  v  a2  s  . co m*/
 * 
 * @throws IOException
 */
protected void decode(InputStream inStream, int prefixMax, String backingFilename, Charset charset)
        throws IOException {

    this.charset = charset;

    // TODO: consider if BufferedReader is helping any
    // TODO: consider adding TBW 'LimitReader' to stop reading at 
    // Integer.MAX_VALUE characters because of charAt(int) limit
    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, charset));

    logger.fine("backingFilename=" + backingFilename + " encoding=" + charset + " decodedFile=" + decodedFile);

    this.prefixBuffer = CharBuffer.allocate(prefixMax);

    long count = 0;
    while (count < prefixMax) {
        int read = reader.read(prefixBuffer);
        if (read < 0) {
            break;
        }
        count += read;
    }

    int ch = reader.read();
    if (ch >= 0) {
        count++;

        // more to decode to file overflow
        this.decodedFile = new File(backingFilename + "." + WRITE_ENCODING);

        FileOutputStream fos;
        try {
            fos = new FileOutputStream(this.decodedFile);
        } catch (FileNotFoundException e) {
            // Windows workaround attempt
            System.gc();
            System.runFinalization();
            this.decodedFile = new File(decodedFile.getAbsolutePath() + ".win");
            logger.info("Windows 'file with a user-mapped section open' "
                    + "workaround gc/finalization/name-extension performed.");
            // try again
            fos = new FileOutputStream(this.decodedFile);
        }

        Writer writer = new OutputStreamWriter(fos, WRITE_ENCODING);
        writer.write(ch);
        count += IOUtils.copyLarge(reader, writer);
        writer.close();
        reader.close();
    }

    this.length = Ints.saturatedCast(count);
    if (count > Integer.MAX_VALUE) {
        logger.warning("input stream is longer than Integer.MAX_VALUE="
                + NumberFormat.getInstance().format(Integer.MAX_VALUE) + " characters -- only first "
                + NumberFormat.getInstance().format(Integer.MAX_VALUE)
                + " are accessible through this GenericReplayCharSequence");
    }

    logger.fine("decode: decoded " + count + " characters" + ((decodedFile == null) ? ""
            : " (" + (count - prefixBuffer.length()) + " to " + decodedFile + ")"));
}

From source file:org.artifactory.request.ArtifactoryResponseBase.java

@Override
public void sendStream(InputStream is) throws IOException {
    OutputStream os = getOutputStream();
    setStatus(status);/*from  w w w.j  ava 2s.com*/
    try {
        long bytesCopied = IOUtils.copyLarge(is, os);
        if (bytesCopied == 0 && getContentLength() > 0) {
            log.warn("Zero bytes sent to client but expected {} bytes.", getContentLength());
        } else {
            long expectedLength = getContentLength();
            if (expectedLength > 0 && bytesCopied != expectedLength) {
                log.warn("Actual bytes sent to client ({}) are different than expected ({}).", bytesCopied,
                        expectedLength);
            } else {
                log.debug("{} bytes sent to client.", bytesCopied);
            }
        }
        sendSuccess();
    } catch (Exception e) {
        sendInternalError(e, log);
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(is);
    }
}

From source file:org.awesomeapp.messenger.ui.StickerActivity.java

private void exportAsset(Uri mediaUri, String name) {
    if (checkPermissions()) {
        try {//from w  w  w.java  2 s . co  m

            String mimeType = "image/png";
            java.io.File exportPath = new File(Environment.getExternalStorageDirectory(), name + ".png");

            java.io.InputStream fis = getResources().getAssets().open(mediaUri.getPath());
            java.io.FileOutputStream fos = new java.io.FileOutputStream(exportPath, false);

            IOUtils.copyLarge(fis, fos);

            fos.close();
            fis.close();

            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportPath));
            shareIntent.setType(mimeType);
            startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.export_media)));
        } catch (IOException e) {
            Toast.makeText(this, "Export Failed " + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

From source file:org.canova.api.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to//from w w  w  .java 2  s  .  co  m
 * @param dest the destination directory
 * @throws java.io.IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(dest + File.separator + fileName);

            if (ze.isDirectory()) {
                newFile.mkdirs();
                zis.closeEntry();
                ze = zis.getNextEntry();
                continue;
            }

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.flush();
            fos.close();
            zis.closeEntry();
            ze = zis.getNextEntry();
        }

        zis.close();

    }

    else if (file.endsWith(".tar")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();
                ;

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

}

From source file:org.codice.alliance.imaging.chip.transformer.CatalogOutputAdapter.java

NitfSegmentsFlow getNitfSegmentsFlow(InputStream resourceInputStream) throws NitfFormatException, IOException {
    notNull(resourceInputStream, "resourceInputStream must be non-null");

    NitfSegmentsFlow nitfSegmentsFlow;/*from w  w  w. j a v  a  2s. c o  m*/
    try (TemporaryFileBackedOutputStream tfbos = createTemporaryFileBackedOutputStream()) {

        IOUtils.copyLarge(resourceInputStream, tfbos);

        try (InputStream is = tfbos.asByteSource().openBufferedStream()) {
            nitfSegmentsFlow = new NitfParserInputFlowImpl().inputStream(is).allData();
        }
    }

    return nitfSegmentsFlow;
}

From source file:org.codice.alliance.test.itests.ImagingTest.java

@Test
public void testImageNitfChipCreationNitf() throws Exception {
    String id = ingestNitfFile(TEST_IMAGE_NITF);

    String chippingUrl = SECURE_ROOT + HTTPS_PORT.getPort() + "/chipping/chipping.html?id=" + id
            + "&source=Alliance";
    given().get(chippingUrl).then().assertThat().statusCode(HttpStatus.SC_OK);

    final int width = 350;
    final int height = 240;

    String chippedImageUrl = SERVICE_ROOT + "/catalog/" + id + "?transform=nitf-chip&qualifier=overview&x="
            + 300 + "&y=" + 200 + "&w=" + width + "&h=" + height;
    InputStream chippedImageStream = given().get(chippedImageUrl).asInputStream();

    List<ImageSegment> imageSegments = new LinkedList<>();

    try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {

        IOUtils.copyLarge(chippedImageStream, tfbos);

        NitfSegmentsFlow nitfSegmentsFlow = new NitfParserInputFlowImpl()
                .inputStream(tfbos.asByteSource().openBufferedStream()).allData();

        nitfSegmentsFlow.forEachImageSegment(imageSegments::add);
    }//w  ww .j av  a2 s .c o  m

    assertThat(imageSegments, hasSize(1));
    assertThat(imageSegments.get(0).getNumberOfColumns(), is((long) width));
    assertThat(imageSegments.get(0).getNumberOfRows(), is((long) height));
}

From source file:org.codice.ddf.catalog.async.data.impl.LazyProcessResourceImpl.java

/**
 * {@inheritDoc}//from w  w w .j a  v  a 2s  .c  o  m
 *
 * <p>This will load the resource.
 */
@Override
public synchronized InputStream getInputStream() throws IOException {
    if (resourceDataCache == null) {
        loadResource();

        if (inputStream == null) {
            throw new IOException(String.format("Tried to get input stream for %s but was null", name));
        }
        resourceDataCache = new TemporaryFileBackedOutputStream(FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
        IOUtils.copyLarge(inputStream, resourceDataCache);
        IOUtils.closeQuietly(inputStream);
        streamCloser.register(resourceDataCache);
    }
    InputStream newInputStream = resourceDataCache.asByteSource().openStream();
    streamCloser.register(newInputStream);

    return newInputStream;
}

From source file:org.codice.ddf.catalog.async.data.impl.ProcessResourceImpl.java

@Override
public synchronized InputStream getInputStream() throws IOException {
    if (resourceDataCache == null) {
        resourceDataCache = new TemporaryFileBackedOutputStream(FILE_BACKED_OUTPUT_STREAM_THRESHOLD);
        IOUtils.copyLarge(inputStream, resourceDataCache);
        IOUtils.closeQuietly(inputStream);
        streamCloser.register(resourceDataCache);
    }/*  w w  w  . j a va2  s . c o m*/
    InputStream newInputStream = resourceDataCache.asByteSource().openStream();
    streamCloser.register(newInputStream);

    return newInputStream;
}

From source file:org.codice.ddf.catalog.async.processingframework.impl.InMemoryProcessingFramework.java

private <T extends ProcessResourceItem> void storeProcessRequest(ProcessRequest<T> processRequest) {
    LOGGER.trace("Storing update request post processing change(s)");

    Map<String, ContentItem> contentItemsToUpdate = new HashMap<>();
    Map<String, Metacard> metacardsToUpdate = new HashMap<>();
    List<TemporaryFileBackedOutputStream> tfbosToCleanUp = new ArrayList<>();

    for (T item : processRequest.getProcessItems()) {
        final ProcessResource processResource = item.getProcessResource();
        if ((processResource == null || !processResource.isModified()) && item.isMetacardModified()) {
            metacardsToUpdate.put(item.getMetacard().getId(), item.getMetacard());
        }//from w ww  .j a  v  a2  s  .  co m

        TemporaryFileBackedOutputStream tfbos = null;
        if (processResource != null && processResource.isModified()
                && !contentItemsToUpdate.containsKey(getContentItemKey(item.getMetacard(), processResource))) {
            try {
                tfbos = new TemporaryFileBackedOutputStream();
                long numberOfBytes = IOUtils.copyLarge(processResource.getInputStream(), tfbos);
                LOGGER.debug("Copied {} bytes to TemporaryFileBackedOutputStream.", numberOfBytes);
                ByteSource byteSource = tfbos.asByteSource();

                ContentItem contentItem = new ContentItemImpl(item.getMetacard().getId(),
                        processResource.getQualifier(), byteSource, processResource.getMimeType(),
                        processResource.getName(), processResource.getSize(), item.getMetacard());

                contentItemsToUpdate.put(getContentItemKey(item.getMetacard(), processResource), contentItem);
                tfbosToCleanUp.add(tfbos);
            } catch (IOException | RuntimeException e) {
                LOGGER.debug("Unable to store process request", e);
                if (tfbos != null) {
                    close(tfbos);
                }
            }
        }
    }

    storeContentItemUpdates(contentItemsToUpdate, processRequest.getProperties());
    storeMetacardUpdates(metacardsToUpdate, processRequest.getProperties());
    closeTfbos(tfbosToCleanUp);
}

From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java

private SourceResponse doQueryById(QueryRequest queryRequest, WebClient restWebClient)
        throws UnsupportedQueryException {
    InputStream responseStream = performRequest(restWebClient);

    try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
        IOUtils.copyLarge(responseStream, fileBackedOutputStream);
        InputTransformer inputTransformer;
        try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
            inputTransformer = getInputTransformer(inputStream);
        }// w w w  .  j  a  va2  s  . c o m

        try (InputStream inputStream = fileBackedOutputStream.asByteSource().openStream()) {
            final Metacard metacard = inputTransformer.transform(inputStream);
            metacard.setSourceId(getId());
            ResultImpl result = new ResultImpl(metacard);
            List<Result> resultQueue = new ArrayList<>();
            resultQueue.add(result);
            return new SourceResponseImpl(queryRequest, resultQueue);
        }
    } catch (IOException | CatalogTransformerException e) {
        throw new UnsupportedQueryException("Problem with transformation.", e);
    }
}