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.talend.dataprep.command.CommandHelper.java

public static StreamingResponseBody toStreaming(final HystrixCommand<InputStream> command) {
    return outputStream -> {
        final Observable<InputStream> stream = command.toObservable();
        stream.toBlocking().subscribe(inputStream -> {
            try {
                IOUtils.copyLarge(inputStream, outputStream);
                outputStream.flush();//from  w  w w  .  j  a  va 2 s  . c  o  m
            } catch (IOException e) {
                LOGGER.error("Unable to fully copy command result '{}'.", command.getClass(), e);
            }
        });
    };
}

From source file:org.talend.dataprep.command.CommandHelper.java

public static ResponseEntity<StreamingResponseBody> toStreaming(final GenericCommand<InputStream> command) {
    final Observable<InputStream> stream = command.toObservable();
    return stream.map(is -> {
        // Content for the response entity
        final StreamingResponseBody body = outputStream -> {
            try {
                IOUtils.copyLarge(is, outputStream);
                outputStream.flush();//w w w . j  a va 2s .  c om
            } catch (IOException e) {
                LOGGER.error("Unable to fully copy command result '{}'.", command.getClass(), e);
            }
        };
        // copy all headers from the command response so that the mime-type is correctly forwarded. Command has
        // the correct headers due to call to toBlocking() below.
        final MultiValueMap<String, String> headers = new HttpHeaders();
        final HttpStatus status = command.getStatus();
        for (Header header : command.getCommandResponseHeaders()) {
            headers.put(header.getName(), Collections.singletonList(header.getValue()));
        }
        return new ResponseEntity<>(body, headers, status == null ? HttpStatus.OK : status);
    }).toBlocking().first();
}

From source file:org.warcbase.data.ArcRecordUtils.java

public static byte[] toBytes(ARCRecord record) throws IOException {
    ARCRecordMetaData meta = record.getMetaData();

    String metaline = meta.getUrl() + " " + meta.getIp() + " " + meta.getDate() + " " + meta.getMimetype() + " "
            + (int) meta.getLength();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(baos);
    dout.write(metaline.getBytes());/* ww w .j a v a  2s.  c o m*/
    dout.write("\n".getBytes());

    long recordLength = meta.getLength();
    long len = IOUtils.copyLarge(new BoundedInputStream(record, recordLength), dout);
    if (len != recordLength) {
        LOG.error("Read " + len + " bytes but expected " + recordLength + " bytes. Continuing...");
    }
    return baos.toByteArray();
}

From source file:org.wisdom.monitor.extensions.jcr.backup.ModeshapeJcrBackupExtension.java

@Route(method = HttpMethod.POST, uri = "/restore")
@Async//  w  w  w.java2 s .co  m
public Result restore(@FormParameter("upload") FileItem fileToRestore) throws IOException, RepositoryException {
    File backupToRestoreFile = File.createTempFile("restore-" + new Date().getTime(), ".zip");
    InputStream inputStream = fileToRestore.stream();
    FileOutputStream outputStream = new FileOutputStream(backupToRestoreFile);
    IOUtils.copyLarge(inputStream, outputStream);
    inputStream.close();
    outputStream.close();
    File backupToRestoreFolder = File.createTempFile("restore-" + new Date().getTime(), "");
    backupToRestoreFolder.delete();
    backupToRestoreFolder.mkdir();
    unzip(backupToRestoreFile, backupToRestoreFolder);
    Problems problems = null;
    try {
        problems = ((Session) jcrRepository.getSession()).getWorkspace().getRepositoryManager()
                .restoreRepository(backupToRestoreFolder);
    } catch (RepositoryException e) {
        return internalServerError("1-" + e.getMessage());
    }
    return ok(render(indexTemplate));
}

From source file:org.wso2.carbon.connector.FileCopy.java

/**
 * Copy files/*from   w ww .ja  va 2  s. com*/
 *
 * @param source         Location of the file
 * @param destination    new file location
 * @param filePattern    pattern of the file
 * @param messageContext The message context that is generated for processing the file
 * @param opts           FileSystemOptions
 * @return return a resultStatus
 */
private boolean copyFile(String source, String destination, String filePattern, MessageContext messageContext,
        FileSystemOptions opts) {
    boolean resultStatus = false;
    StandardFileSystemManager manager = null;
    try {
        manager = FileConnectorUtils.getManager();
        FileObject souFile = manager.resolveFile(source, opts);
        FileObject destFile = manager.resolveFile(destination, opts);
        if (StringUtils.isNotEmpty(filePattern)) {
            FileObject[] children = souFile.getChildren();
            for (FileObject child : children) {
                if (child.getType() == FileType.FILE) {
                    copy(source, destination, filePattern, opts);
                } else if (child.getType() == FileType.FOLDER) {
                    String newSource = source + File.separator + child.getName().getBaseName();
                    copyFile(newSource, destination, filePattern, messageContext, opts);
                }
            }
            resultStatus = true;
        } else {
            if (souFile.exists()) {
                if (souFile.getType() == FileType.FILE) {
                    InputStream fileIn = null;
                    OutputStream fileOut = null;
                    try {
                        String name = souFile.getName().getBaseName();
                        FileObject outFile = manager.resolveFile(destination + File.separator + name, opts);
                        //TODO make parameter sense
                        fileIn = souFile.getContent().getInputStream();
                        fileOut = outFile.getContent().getOutputStream();
                        IOUtils.copyLarge(fileIn, fileOut);
                        resultStatus = true;
                    } catch (FileSystemException e) {
                        log.error("Error while copying a file " + e.getMessage());
                    } finally {
                        try {
                            if (fileOut != null) {
                                fileOut.close();
                            }
                        } catch (Exception e) {
                            log.error("Error while closing OutputStream: " + e.getMessage(), e);
                        }
                        try {
                            if (fileIn != null) {
                                fileIn.close();
                            }
                        } catch (Exception e) {
                            log.error("Error while closing InputStream: " + e.getMessage(), e);
                        }
                    }
                } else if (souFile.getType() == FileType.FOLDER) {
                    destFile.copyFrom(souFile, Selectors.SELECT_ALL);
                    resultStatus = true;
                }
                if (log.isDebugEnabled()) {
                    log.debug("File copying completed from " + source + "to" + destination);
                }
            } else {
                log.error("The File Location does not exist.");
                resultStatus = false;
            }
        }
        return resultStatus;
    } catch (IOException e) {
        handleException("Unable to copy a file/folder", e, messageContext);
    } finally {
        if (manager != null) {
            manager.close();
        }
    }
    return resultStatus;
}

From source file:org.wso2.carbon.connector.FileCopyInStream.java

/**
 * Copy the large files/*from  ww w. ja  v a  2s . com*/
 * 
 * @param fileLocation
 * @param filename
 * @param newFileLocation
 * @return
 */
private boolean copyLargeFiles(String fileLocation, String filename, String newFileLocation)
        throws IOException {

    String sftpURL = newFileLocation + filename.toString();
    FileSystemOptions opts;
    boolean resultStatus = false;

    opts = FTPSiteUtils.createDefaultOptions();

    FileSystemManager manager = VFS.getManager();
    FileObject localFile = manager.resolveFile(fileLocation + filename, opts);
    FileObject remoteFile = manager.resolveFile(sftpURL, opts);

    InputStream fin = null;
    OutputStream fout = null;
    try {
        fin = localFile.getContent().getInputStream();
        fout = remoteFile.getContent().getOutputStream();
        IOUtils.copyLarge(fin, fout);
    } finally {
        if (fout != null) {
            fout.close();
        }
        if (fin != null) {
            fin.close();
        }
    }

    resultStatus = true;

    if (log.isDebugEnabled()) {
        log.info("File copying completed..." + filename.toString());
    }

    return resultStatus;
}

From source file:org.xwiki.icon.internal.DefaultIconSetLoaderTest.java

@Test
public void loadIconSetFromWikiDocument() throws Exception {
    DocumentReference iconSetRef = new DocumentReference("xwiki", "IconThemes", "Default");
    DocumentReference iconClassRef = new DocumentReference("wikiId", "IconThemesCode", "IconThemeClass");
    when(documentAccessBridge.getProperty(eq(iconSetRef), eq(iconClassRef), eq("name")))
            .thenReturn("MyIconTheme");
    DocumentModelBridge doc = mock(DocumentModelBridge.class);
    when(documentAccessBridge.getDocument(iconSetRef)).thenReturn(doc);

    StringWriter content = new StringWriter();
    IOUtils.copyLarge(new InputStreamReader(getClass().getResourceAsStream("/test.iconset")), content);
    when(doc.getContent()).thenReturn(content.toString());

    // Test// ww  w  .ja  v  a2s.c o  m
    IconSet result = mocker.getComponentUnderTest().loadIconSet(iconSetRef);

    // Verify
    verifies(result);
    assertEquals("MyIconTheme", result.getName());
}

From source file:org.yestech.publish.publisher.AmazonS3Publisher.java

private String saveToDisk(String artifactDirectoryName, InputStream artifact, String uniqueFileName) {
    File fullPath = new File(getTempDirectory() + File.separator + artifactDirectoryName);
    if (!fullPath.exists()) {
        fullPath.mkdirs();/*from ww  w.j  a v  a 2  s  .c  o m*/
    }
    String location = fullPath.getAbsolutePath() + File.separator + uniqueFileName;
    FileOutputStream outputStream = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Saving file: " + location);
        }
        outputStream = openOutputStream(new File(location));
        IOUtils.copyLarge(artifact, outputStream);
        outputStream.flush();
        if (logger.isDebugEnabled()) {
            logger.debug("Saved file: " + location);
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(artifact);
        IOUtils.closeQuietly(outputStream);
    }
    return location;
}

From source file:org.yestech.publish.publisher.LocalFileSystemPublisher.java

@Override
public void publish(IFileArtifact artifact) {
    IFileArtifactMetaData metaData = artifact.getArtifactMetaData();
    InputStream artifactStream = artifact.getStream();
    Pair<String, String> names = metaData.getUniqueNames();
    String uniquePath = generateUniqueIdentifier(metaData.getArtifactOwner());
    if (names != null) {
        String path = names.getFirst();
        if (StringUtils.isNotBlank(path)) {
            uniquePath = path;//  w ww. j  a  va 2s.c o  m
        }
    }
    File fullPath = new File(directory + File.separator + uniquePath);
    if (!fullPath.exists()) {
        fullPath.mkdirs();
    }
    String uniqueFileName = generateUniqueIdentifier(metaData);
    if (names != null) {
        String fileName = names.getSecond();
        if (StringUtils.isNotBlank(fileName)) {
            uniqueFileName = fileName;
        }
    }
    String location = fullPath.getAbsolutePath() + File.separator + uniqueFileName;
    FileOutputStream outputStream = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Saving file: " + location);
        }
        outputStream = openOutputStream(new File(location));
        IOUtils.copyLarge(artifactStream, outputStream);
        outputStream.flush();
        if (logger.isDebugEnabled()) {
            logger.debug("Saved file: " + location);
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(artifactStream);
        IOUtils.closeQuietly(outputStream);
        PublishUtils.reset(artifact);
    }

    if (StringUtils.isBlank(metaData.getLocation())) {
        metaData.setLocation(location);
    }
}

From source file:org.yestech.publish.util.PublishUtils.java

public static String saveTempFile(File tempDirectory, InputStream inputStream, IFileArtifact artifact) {
    String fqn = "";
    FileOutputStream fileOutputStream = null;
    try {/*  w  w w .  j a va 2s  .c  o m*/
        String ownerId = generateUniqueIdentifier(artifact.getArtifactMetaData().getArtifactOwner());
        String directory = tempDirectory + File.separator + ownerId + File.separator;

        File dir = new File(directory);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        fqn = directory + artifact.getArtifactMetaData().getFileName();
        final File newFile = new File(fqn);

        fileOutputStream = FileUtils.openOutputStream(newFile);
        IOUtils.copyLarge(inputStream, fileOutputStream);
        fileOutputStream.flush();
        artifact.getArtifactMetaData().setSize(newFile.length());
        artifact.setStream(new FileInputStream(newFile));
    } catch (IOException e) {
        logger.error("error saving streaming data: " + e);
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
    return fqn;
}