Example usage for java.nio.file Files newInputStream

List of usage examples for java.nio.file Files newInputStream

Introduction

In this page you can find the example usage for java.nio.file Files newInputStream.

Prototype

public static InputStream newInputStream(Path path, OpenOption... options) throws IOException 

Source Link

Document

Opens a file, returning an input stream to read from the file.

Usage

From source file:de.elomagic.carafile.client.CaraFileUtils.java

/**
 * Creates a meta file from the given file.
 *
 * @param path Path of the file//  w ww .ja  v a2s  .  c om
 * @param filename Real name of the file because it can differ from path parameter
 * @return
 * @throws IOException
 * @throws GeneralSecurityException
 */
public static final MetaData createMetaData(final Path path, final String filename)
        throws IOException, GeneralSecurityException {
    if (Files.notExists(path)) {
        throw new FileNotFoundException("File " + path.toString() + " not found!");
    }

    if (Files.isDirectory(path)) {
        throw new IllegalArgumentException("Not a file: " + path.toString());
    }

    MetaData md = new MetaData();
    md.setSize(Files.size(path));
    md.setFilename(filename);
    md.setCreationDate(new Date());
    md.setChunkSize(DEFAULT_PIECE_SIZE);

    try (InputStream in = Files.newInputStream(path, StandardOpenOption.READ);
            BufferedInputStream bin = new BufferedInputStream(in)) {
        MessageDigest mdComplete = MessageDigest.getInstance(MessageDigestAlgorithms.SHA_1);

        byte[] buffer = new byte[DEFAULT_PIECE_SIZE];
        int bytesRead;

        while ((bytesRead = bin.read(buffer)) > 0) {
            mdComplete.update(buffer, 0, bytesRead);

            ChunkData chunk = new ChunkData(
                    DigestUtils.sha1Hex(new ByteArrayInputStream(buffer, 0, bytesRead)));
            md.addChunk(chunk);
        }

        String sha1 = Hex.encodeHexString(mdComplete.digest());
        md.setId(sha1);
    }

    return md;
}

From source file:org.cryptomator.ui.settings.Settings.java

public static synchronized Settings load() {
    if (INSTANCE == null) {
        try {/*  ww  w  . jav a 2  s.  com*/
            Files.createDirectories(SETTINGS_DIR);
            final Path settingsFile = SETTINGS_DIR.resolve(SETTINGS_FILE);
            final InputStream in = Files.newInputStream(settingsFile, StandardOpenOption.READ);
            INSTANCE = JSON_OM.readValue(in, Settings.class);
            return INSTANCE;
        } catch (IOException e) {
            LOG.warn("Failed to load settings, creating new one.");
            INSTANCE = Settings.defaultSettings();
        }
    }
    return INSTANCE;
}

From source file:de.dentrassi.pm.rpm.internal.RpmExtractor.java

@Override
public void extractMetaData(final Context context, final Map<String, String> metadata) throws Exception {
    final Path path = context.getPath();

    try (RpmInputStream in = new RpmInputStream(
            new BufferedInputStream(Files.newInputStream(path, StandardOpenOption.READ)))) {
        final RpmInformation info = makeInformation(in);
        if (info == null) {
            return;
        }//from   w w  w.j a  v  a 2 s  .  c  om

        metadata.put("artifactLabel", "RPM Package");

        metadata.put("name", asString(in.getPayloadHeader().getTag(RpmTag.NAME)));
        metadata.put("version", asString(in.getPayloadHeader().getTag(RpmTag.VERSION)));
        metadata.put("os", asString(in.getPayloadHeader().getTag(RpmTag.OS)));
        metadata.put("arch", asString(in.getPayloadHeader().getTag(RpmTag.ARCH)));

        metadata.put(Constants.KEY_INFO.getKey(), info.toJson());
    }
}

From source file:com.ignorelist.kassandra.steam.scraper.PathResolver.java

public Set<Path> findAllLibraryDirectories() throws IOException, RecognitionException {
    Path steamApps = findSteamApps();
    Set<Path> libraryDirectories = new LinkedHashSet<>();
    libraryDirectories.add(steamApps);// w w w. j a v  a  2  s.c  o  m
    Path directoryDescriptor = steamApps.resolve("libraryfolders.vdf");
    if (Files.exists(directoryDescriptor)) {
        InputStream vdfStream = Files.newInputStream(directoryDescriptor, StandardOpenOption.READ);
        VdfRoot vdfRoot = VdfParser.parse(vdfStream);
        IOUtils.closeQuietly(vdfStream);
        final VdfNode nodeLibrary = Iterables.getFirst(vdfRoot.getChildren(), null);
        if (null != nodeLibrary) {
            for (VdfAttribute va : nodeLibrary.getAttributes()) {
                //System.err.println(va);
                try {
                    Integer.parseInt(va.getName());
                    Path libraryDirectory = Paths.get(va.getValue());
                    libraryDirectories.add(resolveAppsDirectory(libraryDirectory));
                } catch (NumberFormatException nfe) {
                } catch (IllegalStateException ise) {
                    System.err.println(ise);
                }
            }
        }
    }

    return libraryDirectories;
}

From source file:org.eclipse.packagedrone.repo.adapter.rpm.internal.RpmExtractor.java

@Override
public void extractMetaData(final Context context, final Map<String, String> metadata) {
    final Path path = context.getPath();

    try (RpmInputStream in = new RpmInputStream(
            new BufferedInputStream(Files.newInputStream(path, StandardOpenOption.READ)))) {
        final RpmInformation info = makeInformation(in);
        if (info == null) {
            return;
        }//from   ww  w.  ja  va 2s. c  o m

        metadata.put("artifactLabel", "RPM Package");

        metadata.put("name", asString(in.getPayloadHeader().getTag(RpmTag.NAME)));
        metadata.put("version", asString(in.getPayloadHeader().getTag(RpmTag.VERSION)));
        metadata.put("os", asString(in.getPayloadHeader().getTag(RpmTag.OS)));
        metadata.put("arch", asString(in.getPayloadHeader().getTag(RpmTag.ARCH)));

        metadata.put(Constants.KEY_INFO.getKey(), info.toJson());
    } catch (final Exception e) {
        // ignore ... not an RPM file
    }
}

From source file:onl.area51.httpd.util.PathEntity.java

@Override
public InputStream getContent() throws IOException {
    return Files.newInputStream(file, StandardOpenOption.READ);
}

From source file:com.arvato.thoroughly.util.security.impl.DefaultEncryptionKeyRetrievalStrategy.java

private SecretKeySpec getKeySpecFromPath(final Path path) throws IOException, Exception {
    final InputStream stream = Files.newInputStream(path, new OpenOption[0]);
    try {/*from w w w  .  j  av  a2s .com*/
        final SecretKeySpec localSecretKeySpec = convertStreamToKey(stream);
        return localSecretKeySpec;
    } catch (final Throwable localThrowable4) {
        LOGGER.error(localThrowable4.getMessage());
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (final IOException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
    }
    return null;
}

From source file:com.ignorelist.kassandra.steam.scraper.SharedConfig.java

public synchronized VdfRoot getRootNode() throws IOException, RecognitionException {
    if (null == rootNode) {
        InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ);
        try {/*from   w  w w.  j  a  va 2 s .  co  m*/
            rootNode = VdfParser.parse(inputStream);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }
    return rootNode;
}

From source file:org.cloudfoundry.dependency.resource.InAction.java

private String getSha256(Path artifact) throws IOException {
    try (InputStream in = Files.newInputStream(artifact, StandardOpenOption.READ)) {
        return DigestUtils.sha256Hex(in);
    }//from w  w  w  .j  a v  a 2 s  . c o m
}

From source file:com.github.horrorho.inflatabledonkey.chunk.store.disk.DiskChunk.java

@Override
public InputStream inputStream() throws UncheckedIOException {
    try {//from  w w  w  .j  a va  2s  . com
        return Files.newInputStream(file, READ);

    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}