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:eu.planets_project.services.utils.ZipUtils.java

private static void readFileEntry(Zip64File zip64File, FileEntry fileEntry, File destFolder) {
    FileOutputStream fileOut;/*  w  w  w  .j ava 2s .c om*/
    File target = new File(destFolder, fileEntry.getName());
    File targetsParent = target.getParentFile();
    if (targetsParent != null) {
        targetsParent.mkdirs();
    }
    try {
        //         boolean madeFolder = target.createNewFile();
        fileOut = new FileOutputStream(target);
        log.info("[readFileEntry] writing entry: " + fileEntry.getName() + " to file: "
                + target.getAbsolutePath());
        EntryInputStream entryReader = zip64File.openEntryInputStream(fileEntry.getName());
        IOUtils.copyLarge(entryReader, fileOut);
        entryReader.close();
        fileOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (ZipException e) {
        log.warning(
                "ATTENTION PLEASE: Some strange, but obviously not serious ZipException occured! Extracted file '"
                        + target.getName() + "' anyway! So don't Panic!" + "\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:autoupdater.FileDAO.java

public File inflateArchive(File source, File destination) throws IOException, ArchiveException {
    ArchiveInputStream stream = new ArchiveStreamFactory()
            .createArchiveInputStream(new BufferedInputStream(new FileInputStream(source)));
    ZipFile zipfile = new ZipFile(source);
    ArchiveEntry entry = null;/*from   w w  w. ja v  a 2  s  . c  o m*/
    FileOutputStream dest = null;
    InputStream inStream = null;
    if ((entry = stream.getNextEntry()) != null) {
        do {
            File destFile = new File(destination, entry.getName());
            if (!destFile.getParentFile().exists()) {
                if (!destFile.getParentFile().mkdirs()) {
                    throw new IOException("could not create the folders to unzip in");
                }
            }
            if (!entry.isDirectory()) {
                try {
                    dest = new FileOutputStream(destFile);
                    inStream = stream;
                    IOUtils.copyLarge(inStream, dest);
                } finally {
                    if (dest != null) {
                        dest.close();
                    }
                    if (inStream != null) {
                        inStream.close();
                    }
                }
            } else {
                if (!destFile.exists()) {
                    if (!destFile.mkdirs()) {
                        throw new IOException("could not create folders to unzip file");
                    }
                }
            }
        } while ((entry = stream.getNextEntry()) != null);
    }
    return destination;
}

From source file:ch.cyberduck.core.onedrive.OneDriveWriteFeatureTest.java

@Test
public void testWriteZeroLength() throws Exception {
    final OneDriveWriteFeature feature = new OneDriveWriteFeature(session);
    final Path container = new OneDriveHomeFinderFeature(session).find();
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(content.length);// www . j a v  a2s  . com
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final HttpResponseOutputStream<Void> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new OneDriveReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new OneDriveDeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:ch.cyberduck.core.b2.B2LargeUploadWriteFeatureTest.java

@Test
public void testWriteZeroLength() 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 B2LargeUploadWriteFeature feature = new B2LargeUploadWriteFeature(session);
    final Path container = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
    final byte[] content = RandomUtils.nextBytes(0);
    final TransferStatus status = new TransferStatus();
    status.setLength(-1L);/*from w  ww .  j  a v  a  2 s  . co  m*/
    final Path file = new Path(container, new AlphanumericRandomStringService().random(),
            EnumSet.of(Path.Type.file));
    final StatusOutputStream<VersionId> out = feature.write(file, status, new DisabledConnectionCallback());
    final ByteArrayInputStream in = new ByteArrayInputStream(content);
    assertEquals(content.length, IOUtils.copyLarge(in, out));
    in.close();
    out.close();
    assertNotNull(out.getStatus());
    assertTrue(new DefaultFindFeature(session).find(file));
    final byte[] compare = new byte[content.length];
    final InputStream stream = new B2ReadFeature(session).read(file,
            new TransferStatus().length(content.length), new DisabledConnectionCallback());
    IOUtils.readFully(stream, compare);
    stream.close();
    assertArrayEquals(content, compare);
    new B2DeleteFeature(session).delete(Collections.singletonList(file), new DisabledLoginCallback(),
            new Delete.DisabledCallback());
}

From source file:net.padaf.preflight.font.AbstractFontValidator.java

/**
 * Type0, Type1 and TrueType FontValidatir call this method to check the
 * FontFile meta data.//  w ww.  java2s  .  com
 * 
 * @param fontDesc
 *          The FontDescriptor which contains the FontFile stream
 * @param fontFile
 *          The font file stream to check
 * @return true if the meta data is valid, false otherwise
 * @throws ValidationException when checking fails
 */
protected boolean checkFontFileMetaData(PDFontDescriptor fontDesc, PDStream fontFile)
        throws ValidationException {
    PDMetadata metadata = fontFile.getMetadata();
    if (metadata != null) {
        // --- Filters are forbidden in a XMP stream
        if (metadata.getFilters() != null && !metadata.getFilters().isEmpty()) {
            fontContainer.addError(new ValidationError(ValidationConstants.ERROR_SYNTAX_STREAM_INVALID_FILTER,
                    "Filter specified in font file metadata dictionnary"));
            return false;
        }

        // --- extract the meta data content
        byte[] mdAsBytes = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream metaDataContent = metadata.createInputStream();
            IOUtils.copyLarge(metaDataContent, bos);
            IOUtils.closeQuietly(metaDataContent);
            IOUtils.closeQuietly(bos);
            mdAsBytes = bos.toByteArray();
        } catch (IOException e) {
            throw new ValidationException("Unable to read font metadata due to : " + e.getMessage(), e);
        }

        try {

            XMPDocumentBuilder xmpBuilder = new XMPDocumentBuilder();
            XMPMetadata xmpMeta = xmpBuilder.parse(mdAsBytes);

            FontMetaDataValidation fontMDval = new FontMetaDataValidation();
            List<ValidationError> ve = new ArrayList<ValidationError>();
            boolean isVal = fontMDval.analyseFontName(xmpMeta, fontDesc, ve);
            isVal = isVal && fontMDval.analyseFontName(xmpMeta, fontDesc, ve);
            for (ValidationError validationError : ve) {
                fontContainer.addError(validationError);
            }
            return isVal;

        } catch (XmpUnknownValueTypeException e) {
            fontContainer.addError(
                    new ValidationError(ValidationConstants.ERROR_METADATA_UNKNOWN_VALUETYPE, e.getMessage()));
            return false;
        } catch (XmpParsingException e) {
            fontContainer
                    .addError(new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT, e.getMessage()));
            return false;
        } catch (XmpSchemaException e) {
            fontContainer
                    .addError(new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT, e.getMessage()));
            return false;
        } catch (XmpExpectedRdfAboutAttribute e) {
            fontContainer.addError(new ValidationError(
                    ValidationConstants.ERROR_METADATA_RDF_ABOUT_ATTRIBUTE_MISSING, e.getMessage()));
            return false;
        } catch (BadFieldValueException e) {
            fontContainer.addError(new ValidationError(
                    ValidationConstants.ERROR_METADATA_CATEGORY_PROPERTY_INVALID, e.getMessage()));
            return false;
        } catch (XmpXpacketEndException e) {
            throw new ValidationException("Unable to parse font metadata due to : " + e.getMessage(), e);
        }
    }

    // --- No MetaData, valid
    return true;
}

From source file:com.gitblit.manager.FilestoreManager.java

@Override
public FilestoreModel.Status uploadBlob(String oid, long size, UserModel user, RepositoryModel repo,
        InputStream streamIn) {//  w w w .j a v a2 s  .c  o m

    //Access control and object logic
    Status state = addObject(oid, size, user, repo);

    if (state != Status.Upload_Pending) {
        return state;
    }

    FilestoreModel model = fileCache.get(oid);

    if (!model.actionUpload(user)) {
        return Status.Upload_In_Progress;
    } else {
        long actualSize = 0;
        File file = getStoragePath(oid);

        try {
            file.getParentFile().mkdirs();
            file.createNewFile();

            try (FileOutputStream streamOut = new FileOutputStream(file)) {

                actualSize = IOUtils.copyLarge(streamIn, streamOut);

                streamOut.flush();
                streamOut.close();

                if (model.getSize() != actualSize) {
                    model.setStatus(Status.Error_Size_Mismatch, user);

                    logger.warn(MessageFormat.format(
                            "Failed to upload blob {0} due to size mismatch, expected {1} got {2}", oid,
                            model.getSize(), actualSize));
                } else {
                    String actualOid = "";

                    try (FileInputStream fileForHash = new FileInputStream(file)) {
                        actualOid = DigestUtils.sha256Hex(fileForHash);
                        fileForHash.close();
                    }

                    if (oid.equalsIgnoreCase(actualOid)) {
                        model.setStatus(Status.Available, user);
                    } else {
                        model.setStatus(Status.Error_Hash_Mismatch, user);

                        logger.warn(MessageFormat.format(
                                "Failed to upload blob {0} due to hash mismatch, got {1}", oid, actualOid));
                    }
                }
            }
        } catch (Exception e) {

            model.setStatus(Status.Error_Unknown, user);
            logger.warn(MessageFormat.format("Failed to upload blob {0}", oid), e);
        } finally {
            saveFilestoreModel(model);
        }

        if (model.isInErrorState()) {
            file.delete();
            model.removeRepository(repo.name);
        }
    }

    return model.getStatus();
}

From source file:com.altoukhov.svsync.fileviews.LocalFileSpace.java

@Override
public boolean writeFile(InputStream fileStream, FileSnapshot file) {

    if (fileStream == null)
        return false;

    OutputStream out = null;//w  w w .  ja  v a2  s  .  c o m
    long bytesWritten = 0;
    boolean setTimestamp = false;

    try {
        out = getOutputFileStream(file.getRelativePath());
        if (out == null) {
            System.out.println("Failed to open file for write: " + file.getRelativePath());
            return false;
        }

        if (file.isLargeFile()) {
            bytesWritten = IOUtils.copyLarge(fileStream, out);
        } else {
            bytesWritten = IOUtils.copy(fileStream, out);
        }
    } catch (IOException ex) {
        System.out.println("Failed to copy file: " + ex.getMessage());
    } finally {
        try {
            if (fileStream != null)
                fileStream.close();
            if (out != null)
                out.close();
        } catch (IOException ex) {
            System.out.println("Failed to close stream: " + ex.getMessage());
        }
    }

    setTimestamp = setFileTimestamp(file.getRelativePath(), file.getModifiedTimestamp());
    return setTimestamp && (bytesWritten == file.getFileSize());
}

From source file:de.uni_luebeck.inb.knowarc.usecases.invocation.local.LocalUseCaseInvocation.java

private String setOneBinaryInput(ReferenceService referenceService, T2Reference t2Reference, ScriptInput input,
        String targetSuffix) throws InvocationException {

    if (input.isFile() || input.isTempFile()) {
        // Try to get it as a file
        String target = tempDir.getAbsolutePath() + "/" + targetSuffix;
        FileReference fileRef = getAsFileReference(referenceService, t2Reference);
        if (fileRef != null) {

            if (!input.isForceCopy()) {
                if (linkCommand != null) {
                    String source = fileRef.getFile().getAbsolutePath();
                    String actualLinkCommand = getActualOsCommand(linkCommand, source, targetSuffix, target);
                    logger.info("Link command is " + actualLinkCommand);
                    String[] splitCmds = actualLinkCommand.split(" ");
                    ProcessBuilder builder = new ProcessBuilder(splitCmds);
                    builder.directory(tempDir);
                    try {
                        int code = builder.start().waitFor();
                        if (code == 0) {
                            return target;
                        } else {
                            logger.error("Link command gave errorcode: " + code);
                        }/*ww  w .  j a v  a  2s  .co m*/

                    } catch (InterruptedException e) {
                        // go through
                    } catch (IOException e) {
                        // go through
                    }

                }
            }
        }

        InputStream is = null;
        OutputStream os = null;
        is = getAsStream(referenceService, t2Reference);

        try {
            os = new FileOutputStream(target);
        } catch (FileNotFoundException e) {
            throw new InvocationException(e);
        }

        try {
            IOUtils.copyLarge(is, os);
        } catch (IOException e) {
            throw new InvocationException(e);
        }
        try {
            is.close();
            os.close();
        } catch (IOException e) {
            throw new InvocationException(e);
        }
        return target;
    } else {
        String value = (String) referenceService.renderIdentifier(t2Reference, String.class, this.getContext());
        return value;
    }
}

From source file:com.gargoylesoftware.htmlunit.html.XmlSerializer.java

private Map<String, DomAttr> getAttributesFor(final BaseFrameElement frame) throws IOException {
    final Map<String, DomAttr> map = createAttributesCopyWithClonedAttribute(frame, "src");
    final DomAttr srcAttr = map.get("src");
    if (srcAttr == null) {
        return map;
    }/*w  ww.java  2s  .com*/

    final Page enclosedPage = frame.getEnclosedPage();
    final String suffix = getFileExtension(enclosedPage);
    final File file = createFile(srcAttr.getValue(), "." + suffix);

    if (enclosedPage != null) {
        if (enclosedPage.isHtmlPage()) {
            file.delete(); // TODO: refactor as it is stupid to create empty file at one place
            // and then to complain that it already exists
            ((HtmlPage) enclosedPage).save(file);
        } else {
            final InputStream is = enclosedPage.getWebResponse().getContentAsStream();
            final FileOutputStream fos = new FileOutputStream(file);
            IOUtils.copyLarge(is, fos);
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(fos);
        }
    }

    srcAttr.setValue(file.getParentFile().getName() + FILE_SEPARATOR + file.getName());
    return map;
}

From source file:io.uploader.drive.util.FileUtils.java

public static void saveInputStreamInFile(InputStream input, File target, boolean closeInputStream)
        throws IOException {
    if (input == null || target == null)
        throw new AssertionError();
    FileOutputStream output = new FileOutputStream(target);
    try {//w w  w .ja v a  2s  . c om
        IOUtils.copyLarge(input, output);
    } finally {
        if (output != null)
            output.close();
        if (closeInputStream)
            input.close();
    }
}