Java Code Examples for org.apache.commons.io.FileUtils#openInputStream()

The following examples show how to use org.apache.commons.io.FileUtils#openInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NfvImageRepoManagement.java    From NFVO with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] getImageFileOfNfvImage(String id) throws NotFoundException, IOException {
  NFVImage image = this.queryById(id);
  if (image == null)
    throw new NotFoundException("No NFVImage with ID " + id + " found in image repository");
  if (!image.isInImageRepo())
    throw new NotFoundException(
        "Did not find an NFVImage with ID " + id + " in the image repository");

  if (!image.isStoredLocally())
    throw new NotFoundException(
        "The NFVImage with ID " + id + " does not have an associated image file");

  String filePath = ensureTrailingSlash(nfvImageDirPath) + image.getId();
  if (!Files.exists(Paths.get(filePath)))
    throw new NotFoundException("Not found the image file to serve for NFVImage with ID " + id);
  File imageFile = new File(filePath);
  InputStream inputStream = FileUtils.openInputStream(imageFile);
  return IOUtils.toByteArray(inputStream);
}
 
Example 2
Source File: ConfigurationManagerTest.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
@Test(priority = 29)
public void testDeleteFileById() throws Exception {

    ResourceType resourceType = configurationManager.addResourceType(getSampleResourceTypeAdd());
    Resource resource = configurationManager.addResource(resourceType.getName(), getSampleResource1Add());

    File sampleResourceFile = new File(getSamplesPath("sample-resource-file.txt"));
    InputStream fileStream = FileUtils.openInputStream(sampleResourceFile);

    ResourceFile resourceFile = configurationManager.addFile(resourceType.getName(),
            resource.getResourceName(), "sample-resource-file", fileStream);

    configurationManager.deleteFileById(resourceType.getName(), resource.getResourceName(), resourceFile.getId());
    Assert.assertFalse(
            "Resource should not contain any files.",
            configurationManager.getResource(resourceType.getName(), resource.getResourceName()).isHasFile()
    );
}
 
Example 3
Source File: CommandHelper.java    From mojito with Apache License 2.0 6 votes vote down vote up
/**
 * Get content from {@link java.nio.file.Path} using UTF8
 *
 * @param path
 * @return
 * @throws CommandException
 */
public String getFileContent(Path path) {
    try {
        File file = path.toFile();
        BOMInputStream inputStream = new BOMInputStream(FileUtils.openInputStream(file), false, boms);
        String fileContent;
        if (inputStream.hasBOM()) {
            fileContent = IOUtils.toString(inputStream, inputStream.getBOMCharsetName());
        } else {
            fileContent = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        }
        return fileContent;
    } catch (IOException e) {
        throw new UncheckedIOException("Cannot get file content for path: " + path.toString(), e);
    }
}
 
Example 4
Source File: PathSeqBuildKmersSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testMaskedHopscotchSetFromFasta() throws Exception {
    final File expectedFile = getTestFile("hg19mini.mask_4_15.hss");
    final File ref = new File(hg19MiniReference);
    final File output = createTempFile("test", ".hss");
    if (!output.delete()) {
        Assert.fail();
    }
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
    args.add(PathSeqBuildKmers.KMER_MASK_LONG_NAME, "4,15");
    args.addOutput(output);
    this.runCommandLine(args.getArgsArray());

    final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
    final Input inputTest = new Input(FileUtils.openInputStream(output));

    final Kryo kryo = new Kryo();
    final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
    final PSKmerSet testKmerLib = kryo.readObject(inputTest, PSKmerSet.class);

    Assert.assertEquals(testKmerLib, expectedKmerLib);
}
 
Example 5
Source File: IOHelper.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
public static void printTarGzFile(File tarFile) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(FileUtils.openInputStream(tarFile));
    CompressorInputStream cis = new GzipCompressorInputStream(bin);

    try (TarArchiveInputStream tais = new TarArchiveInputStream(cis)) {
        TarArchiveEntry entry;
        while ((entry = tais.getNextTarEntry()) != null) {
            if (entry.isDirectory()) {
                LOGGER.warn("dir:{}", entry.getName());
            } else {
                int size = (int) entry.getSize();
                byte[] content = new byte[size];
                int readCount = tais.read(content, 0, size);
                LOGGER.info("fileName:{}", entry.getName());
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content, 0, readCount);
                try (LineIterator iterator = IOUtils.lineIterator(byteArrayInputStream, Charset.forName("utf-8"))) {
                    while (iterator.hasNext()) {
                        LOGGER.info("line:{}", iterator.nextLine());
                    }
                }
            }
        }
        LOGGER.info("===============finish===============");
    }
}
 
Example 6
Source File: DefaultStorageClientTest.java    From fastdfs-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void uploadFileTest() throws IOException {
    File file = new File("F:\\123456.txt");
    FileInputStream fileInputStream = FileUtils.openInputStream(file);
    StorePath storePath = storageClient.uploadFile("group1", fileInputStream, file.length(), "txt");
    fileInputStream.close();
    logger.info("#####===== " + storePath);
}
 
Example 7
Source File: IngestionFileEntry.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public InputStream getFileStream() throws IOException {
    File parentFile = new File(getParentZipFileOrDirectory());
    if (parentFile.isDirectory()) {
        return new BufferedInputStream(FileUtils.openInputStream(new File(parentFile, getFileName())));
    } else {
        return ZipFileUtil.getInputStreamForFile(parentFile, getFileName());
    }
}
 
Example 8
Source File: PathSeqBuildKmersSparkIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testBloomFilterFromFasta() throws Exception {

    final String libraryPath = publicTestDir + PathSeqBuildKmers.class.getPackage().getName().replace(".", "/") + "/hg19mini.hss";
    final File expectedFile = new File(libraryPath);
    final File ref = new File(hg19MiniReference);
    final File output = createTempFile("test", ".bfi");
    if (!output.delete()) {
        Assert.fail();
    }
    final ArgumentsBuilder args = new ArgumentsBuilder();
    args.add(PathSeqBuildKmers.REFERENCE_LONG_NAME, ref);
    args.add(PathSeqBuildKmers.BLOOM_FILTER_FALSE_POSITIVE_P_LONG_NAME, Double.toString(BLOOM_FPP));
    args.addOutput(output);
    this.runCommandLine(args.getArgsArray());

    final Input inputExpected = new Input(FileUtils.openInputStream(expectedFile));
    final Input inputTest = new Input(FileUtils.openInputStream(output));

    final Kryo kryo = new Kryo();
    final PSKmerSet expectedKmerLib = kryo.readObject(inputExpected, PSKmerSet.class);
    final PSKmerBloomFilter testKmerLib = kryo.readObject(inputTest, PSKmerBloomFilter.class);

    final LongIterator itr = expectedKmerLib.iterator();
    while (itr.hasNext()) {
        Assert.assertTrue(testKmerLib.contains(new SVKmerShort(itr.next())));
    }

    final Random rand = new Random(72939);
    int numFP = 0;
    for (int i = 0; i < NUM_FPP_TRIALS; i++) {
        final long randomValue = rand.nextLong() >>> 2;
        if (testKmerLib.contains(new SVKmerShort(randomValue)) && !expectedKmerLib.contains(new SVKmerShort(randomValue))) {
            numFP++;
        }
    }
    Assert.assertTrue(numFP < 1.2 * NUM_FPP_TRIALS * BLOOM_FPP);
}
 
Example 9
Source File: AbstractConfig.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
public boolean load(File configFile) throws IOException, ConfigurationException {
    if (configFile.canRead()) {
        try (InputStream input = FileUtils.openInputStream(configFile)) {
            return load(input);
        }
    }
    return false;
}
 
Example 10
Source File: JavaInputStreamToXUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public final void whenConvertingInputStreamToFile_thenCorrect4() throws IOException {
    final InputStream initialStream = FileUtils.openInputStream(new File("src/test/resources/sample.txt"));

    final File targetFile = new File("src/test/resources/targetFile.tmp");

    FileUtils.copyInputStreamToFile(initialStream, targetFile);
}
 
Example 11
Source File: FileStorage.java    From cuba with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream openStream(FileDescriptor fileDescr) throws FileStorageException {
    checkFileDescriptor(fileDescr);

    File[] roots = getStorageRoots();
    if (roots.length == 0) {
        log.error("No storage directories available");
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString());
    }

    InputStream inputStream = null;
    for (File root : roots) {
        File dir = getStorageDir(root, fileDescr);

        File file = new File(dir, getFileName(fileDescr));
        if (!file.exists()) {
            log.error("File " + file + " not found");
            continue;
        }

        try {
            inputStream = FileUtils.openInputStream(file);
            break;
        } catch (IOException e) {
            log.error("Error opening input stream for " + file, e);
        }
    }
    if (inputStream != null)
        return inputStream;
    else
        throw new FileStorageException(FileStorageException.Type.FILE_NOT_FOUND, fileDescr.getId().toString());
}
 
Example 12
Source File: FastDfsAutoConfigure.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Override
protected R<File> merge(List<java.io.File> files, String path, String fileName, FileChunksMergeDTO info) throws IOException {
    StorePath storePath = null;

    long start = System.currentTimeMillis();
    for (int i = 0; i < files.size(); i++) {
        java.io.File file = files.get(i);

        FileInputStream in = FileUtils.openInputStream(file);
        if (i == 0) {
            storePath = storageClient.uploadAppenderFile(null, in,
                    file.length(), info.getExt());
        } else {
            storageClient.appendFile(storePath.getGroup(), storePath.getPath(),
                    in, file.length());
        }
    }
    if (storePath == null) {
        return R.fail("上传失败");
    }

    long end = System.currentTimeMillis();
    log.info("上传耗时={}", (end - start));
    String url = new StringBuilder(fileProperties.getUriPrefix())
            .append(storePath.getFullPath())
            .toString();
    File filePo = File.builder()
            .url(url)
            .group(storePath.getGroup())
            .path(storePath.getPath())
            .build();
    return R.success(filePo);
}
 
Example 13
Source File: SignupUIBaseBean.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Send a file for download. 
 * 
 * @param filePath
 * 
 */
protected void sendDownload(String filePath, String mimeType) {

	FacesContext fc = FacesContext.getCurrentInstance();
	ServletOutputStream out = null;
	FileInputStream in = null;
	
	String filename = StringUtils.substringAfterLast(filePath, File.separator);
	
	try {
		HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
		
		response.reset();
		response.setHeader("Pragma", "public");
		response.setHeader("Cache-Control","public, must-revalidate, post-check=0, pre-check=0, max-age=0"); 
		response.setContentType(mimeType);
		response.setHeader("Content-disposition", "attachment; filename=" + filename);
		
		in = FileUtils.openInputStream(new File(filePath));
		out = response.getOutputStream();

		IOUtils.copy(in, out);

		out.flush();
	} catch (IOException ex) {
		log.warn("Error generating file for download:" + ex.getMessage());
	} finally {
		IOUtils.closeQuietly(in);
		IOUtils.closeQuietly(out);
	}
	fc.responseComplete();
	
}
 
Example 14
Source File: FileArchive.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public InputStream openInputStream(Entry entry) throws IOException {
    File file = entry == null ? null : ((OsEntry) entry).file;
    if (file == null || !file.isFile() || !file.canRead()) {
        return null;
    }
    return FileUtils.openInputStream(file);
}
 
Example 15
Source File: DefaultStorageClientTest.java    From fastdfs-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void uploadSlaveFileTest() throws IOException {
    File file = new File("F:\\数据提取-乐山市.xlsx");
    FileInputStream fileInputStream = FileUtils.openInputStream(file);
    StorePath storePath = storageClient.uploadSlaveFile("group1", "M00/00/00/wKgKgFgzZxuATInzAACM0xlEIJM55.xlsx", fileInputStream, file.length(), "-1", "xlsx");
    fileInputStream.close();
    logger.info("#####===== " + storePath);
}
 
Example 16
Source File: TaskInfoBuilder.java    From logstash with Apache License 2.0 5 votes vote down vote up
private FileInputStream getFileInputStream(File file) {
    try {
        return FileUtils.openInputStream(file);
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to open file: " + file.getAbsolutePath(), e);
    }
}
 
Example 17
Source File: AliOssAutoConfigure.java    From zuihou-admin-boot with Apache License 2.0 4 votes vote down vote up
@Override
protected R<File> merge(List<java.io.File> files, String path, String fileName, FileChunksMergeDTO info) throws IOException {
    FileServerProperties.Ali ali = fileProperties.getAli();
    String bucketName = ali.getBucketName();
    OSS ossClient = new OSSClientBuilder().build(ali.getEndpoint(), ali.getAccessKeyId(),
            ali.getAccessKeySecret());

    //日期文件夹
    String relativePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
    // web服务器存放的绝对路径
    String relativeFileName = relativePath + StrPool.SLASH + fileName;

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentDisposition("attachment;fileName=" + info.getSubmittedFileName());
    metadata.setContentType(info.getContextType());
    //步骤1:初始化一个分片上传事件。
    InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, relativeFileName, metadata);
    InitiateMultipartUploadResult result = ossClient.initiateMultipartUpload(request);
    // 返回uploadId,它是分片上传事件的唯一标识,您可以根据这个ID来发起相关的操作,如取消分片上传、查询分片上传等。
    String uploadId = result.getUploadId();

    // partETags是PartETag的集合。PartETag由分片的ETag和分片号组成。
    List<PartETag> partETags = new ArrayList<PartETag>();
    for (int i = 0; i < files.size(); i++) {
        java.io.File file = files.get(i);
        FileInputStream in = FileUtils.openInputStream(file);

        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(relativeFileName);
        uploadPartRequest.setUploadId(uploadId);
        uploadPartRequest.setInputStream(in);
        // 设置分片大小。除了最后一个分片没有大小限制,其他的分片最小为100KB。
        uploadPartRequest.setPartSize(file.length());
        // 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出这个范围,OSS将返回InvalidArgument的错误码。
        uploadPartRequest.setPartNumber(i + 1);

        // 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
        UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);

        // 每次上传分片之后,OSS的返回结果会包含一个PartETag。PartETag将被保存到partETags中。
        partETags.add(uploadPartResult.getPartETag());
    }

    /* 步骤3:完成分片上传。 */
    // 排序。partETags必须按分片号升序排列。
    partETags.sort(Comparator.comparingInt(PartETag::getPartNumber));

    // 在执行该操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
    CompleteMultipartUploadRequest completeMultipartUploadRequest =
            new CompleteMultipartUploadRequest(bucketName, relativeFileName, uploadId, partETags);

    CompleteMultipartUploadResult uploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);

    String url = new StringBuilder(ali.getUriPrefix())
            .append(relativePath)
            .append(StrPool.SLASH)
            .append(fileName)
            .toString();
    File filePo = File.builder()
            .relativePath(relativePath)
            .group(uploadResult.getETag())
            .path(uploadResult.getRequestId())
            .url(StringUtils.replace(url, "\\\\", StrPool.SLASH))
            .build();

    // 关闭OSSClient。
    ossClient.shutdown();
    return R.success(filePo);
}
 
Example 18
Source File: CmdImportCli.java    From jackrabbit-filevault with Apache License 2.0 4 votes vote down vote up
protected void doExecute(VaultFsApp app, CommandLine cl) throws Exception {
    String localPath = (String) cl.getValue(argLocalPath);
    String jcrPath = (String) cl.getValue(argJcrPath);

    boolean verbose = cl.hasOption(OPT_VERBOSE);
    /*
    List excludeList = cl.getValues(optExclude);
    String[] excludes = Constants.EMPTY_STRING_ARRAY;
    if (excludeList != null && excludeList.size() > 0) {
        excludes = (String[]) excludeList.toArray(new String[excludeList.size()]);
    }
    */
    String root = (String) cl.getValue(argMountpoint);
    RepositoryAddress addr = new RepositoryAddress(root);

    if (jcrPath == null) {
        jcrPath = "/";
    }
    File localFile = app.getPlatformFile(localPath, false);

    VltContext vCtx = app.createVaultContext(localFile);
    vCtx.setVerbose(cl.hasOption(OPT_VERBOSE));


    if (cl.hasOption(optSysView)) {
        if (!localFile.isFile()) {
            VaultFsApp.log.error("--sysview specified but local path does not point to a file.");
            return;
        }
        // todo: move to another location
        try (InputStream ins = FileUtils.openInputStream(localFile)) {
            Session session = vCtx.getFileSystem(addr).getAggregateManager().getSession();
            session.getWorkspace().importXML(jcrPath, ins, ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
            return;
        }
    }


    VaultFile vaultFile = vCtx.getFileSystem(addr).getFile(jcrPath);

    VaultFsApp.log.info("Importing {} to {}", localFile.getCanonicalPath(), vaultFile.getPath());
    Archive archive;
    if (localFile.isFile()) {
        archive = new ZipArchive(localFile);
    } else {
        if (cl.hasOption(optSync)) {
            VaultFsApp.log.warn("--sync is not supported yet");
        }
        archive = new FileArchive(localFile);
    }
    archive.open(false);
    try {
        Importer importer = new Importer();
        if (verbose) {
            importer.getOptions().setListener(new DefaultProgressListener());
        }
        Session s = vaultFile.getFileSystem().getAggregateManager().getSession();
        importer.run(archive, s, vaultFile.getPath());
    } finally {
        archive.close();
    }
    VaultFsApp.log.info("Importing done.");
}
 
Example 19
Source File: ReportTemplateImpl.java    From yarg with Apache License 2.0 4 votes vote down vote up
public ReportTemplateImpl(String code, String documentName, String documentPath, ReportOutputType reportOutputType) throws IOException {
    this(code, documentName, documentPath, FileUtils.openInputStream(new File(documentPath)), reportOutputType);

    validate();
}
 
Example 20
Source File: BitbakeExtractor.java    From hub-detect with Apache License 2.0 4 votes vote down vote up
public Extraction extract(final ExtractionId extractionId, final File buildEnvScript, final File sourcePath, String[] packageNames, File bash) {
    final File outputDirectory = directoryManager.getExtractionOutputDirectory(extractionId);
    final File bitbakeBuildDirectory = new File(outputDirectory, "build");

    final List<DetectCodeLocation> detectCodeLocations = new ArrayList<>();
    for (final String packageName : packageNames) {
        final File dependsFile = executeBitbakeForRecipeDependsFile(outputDirectory, bitbakeBuildDirectory, buildEnvScript, packageName, bash);
        final String targetArchitecture = executeBitbakeForTargetArchitecture(outputDirectory, buildEnvScript, packageName, bash);

        try {
            if (dependsFile == null) {
                throw new IntegrationException(
                    String.format("Failed to find %s. This may be due to this project being a version of The Yocto Project earlier than 2.3 (Pyro) which is the minimum version for Detect", RECIPE_DEPENDS_FILE_NAME));
            }
            if (StringUtils.isBlank(targetArchitecture)) {
                throw new IntegrationException("Failed to find a target architecture");
            }

            logger.debug(FileUtils.readFileToString(dependsFile, Charset.defaultCharset()));
            final InputStream recipeDependsInputStream = FileUtils.openInputStream(dependsFile);
            final GraphParser graphParser = new GraphParser(recipeDependsInputStream);
            final DependencyGraph dependencyGraph = graphParserTransformer.transform(graphParser, targetArchitecture);
            final ExternalId externalId = new ExternalId(Forge.YOCTO);
            final DetectCodeLocation detectCodeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.BITBAKE, sourcePath.getCanonicalPath(), externalId, dependencyGraph).build();

            detectCodeLocations.add(detectCodeLocation);
        } catch (final IOException | IntegrationException | NullPointerException e) {
            logger.error(String.format("Failed to extract a Code Location while running Bitbake against package '%s'", packageName));
            logger.debug(e.getMessage(), e);
        }
    }

    final Extraction extraction;

    if (detectCodeLocations.isEmpty()) {
        extraction = new Extraction.Builder()
                         .failure("No Code Locations were generated during extraction")
                         .build();
    } else {
        extraction = new Extraction.Builder()
                         .success(detectCodeLocations)
                         .build();
    }

    return extraction;
}