Example usage for com.google.common.io InputSupplier InputSupplier

List of usage examples for com.google.common.io InputSupplier InputSupplier

Introduction

In this page you can find the example usage for com.google.common.io InputSupplier InputSupplier.

Prototype

InputSupplier

Source Link

Usage

From source file:com.jeffplaisance.util.fingertree.bytestring.ByteString.java

@Override
public boolean equals(final Object obj) {
    if (obj == null || !(obj instanceof ByteString)) {
        return false;
    }/*from ww  w .  j a va 2s .  c o m*/
    try {
        return ByteStreams.equal(new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return newInput();
            }
        }, new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return ((ByteString) obj).newInput();
            }
        });
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.complexible.common.io.IOSuppliers.java

/**
 * Return an {@link InputSupplier} which will supply {@link FileChannel} objects for the given {@link File}
 * @param theFile   the file to read from
 * @return          a supplier of FileChannels for the File
 *///from   w  w w.  j ava2  s . com
public static InputSupplier<FileChannel> newFileChannelSupplier(final File theFile) {
    return new InputSupplier<FileChannel>() {
        /**
         * @inheritDoc
         */
        @Override
        public FileChannel getInput() throws IOException {
            return new FileInputStream(theFile).getChannel();
        }
    };
}

From source file:org.eclipse.osee.orcs.account.admin.internal.oauth.ClientStorageProvider.java

private InputSupplier<InputStream> newTypesSupplier() {
    return new InputSupplier<InputStream>() {

        @Override/* ww w .  jav  a  2 s.  c  o m*/
        public InputStream getInput() throws IOException {
            URL resource = getClass().getResource(OAUTH_TYPES_DEFITIONS);
            return new BufferedInputStream(resource.openStream());
        }
    };
}

From source file:de.sep2011.funckit.controller.listener.HelpActionListener.java

private static String getPdfPath(String resourcePath) throws IOException {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File temporaryFile = new File(tempDir, "funckit-help.pdf");
    temporaryFile.deleteOnExit();//from   w  w w . j a  v a2  s.  c  om
    final InputStream templateStream = resourcePath.getClass().getResourceAsStream(resourcePath);
    Files.copy(new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            return templateStream;
        }
    }, temporaryFile);

    String absolutePath = temporaryFile.getAbsolutePath();

    return absolutePath;

}

From source file:io.druid.segment.data.ByteBufferWriter.java

public InputSupplier<InputStream> combineStreams() {
    return ByteStreams.join(Iterables.transform(Arrays.asList("header", "value"),
            new Function<String, InputSupplier<InputStream>>() {
                @Override/*from w w  w.  j  a  v  a  2s  .com*/
                public InputSupplier<InputStream> apply(final String input) {
                    return new InputSupplier<InputStream>() {
                        @Override
                        public InputStream getInput() throws IOException {
                            return ioPeon.makeInputStream(makeFilename(input));
                        }
                    };
                }
            }));
}

From source file:co.cask.cdap.common.lang.jar.BundleJarUtil.java

/**
 * Returns an {@link InputSupplier} for a given entry. This avoids unjar the whole file to just get one entry.
 * However, to get many entries, unjar would be more efficient. Also, the jar file is scanned every time the
 * {@link InputSupplier#getInput()} is invoked.
 *
 * @param jarLocation Location of the jar file.
 * @param entryName Name of the entry to fetch
 * @return An {@link InputSupplier}./*from   w  ww  . j  a  v a  2s  . co  m*/
 */
public static InputSupplier<InputStream> getEntry(final Location jarLocation, final String entryName)
        throws IOException {
    Preconditions.checkArgument(jarLocation != null);
    Preconditions.checkArgument(entryName != null);
    final URI uri = jarLocation.toURI();

    // Small optimization if the location is local
    if ("file".equals(uri.getScheme())) {
        return new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                final JarFile jarFile = new JarFile(new File(uri));
                ZipEntry entry = jarFile.getEntry(entryName);
                if (entry == null) {
                    throw new IOException("Entry not found for " + entryName);
                }
                return new FilterInputStream(jarFile.getInputStream(entry)) {
                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            jarFile.close();
                        }
                    }
                };
            }
        };
    }

    // Otherwise, use JarInputStream
    return new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            JarInputStream is = new JarInputStream(jarLocation.getInputStream());
            JarEntry entry = is.getNextJarEntry();
            while (entry != null) {
                if (entryName.equals(entry.getName())) {
                    return is;
                }
                entry = is.getNextJarEntry();
            }
            Closeables.closeQuietly(is);
            throw new IOException("Entry not found for " + entryName);
        }
    };
}

From source file:com.docdoku.server.resourcegetters.FileConverter.java

public synchronized InputStream convertToPDF(String sourceName, final InputStream streamToConvert)
        throws IOException {
    File tmpDir = com.google.common.io.Files.createTempDir();
    File fileToConvert = new File(tmpDir, sourceName);

    Files.copy(new InputSupplier<InputStream>() {
        @Override/* w  w  w.  j  a  v a  2  s . c  om*/
        public InputStream getInput() throws IOException {
            return streamToConvert;
        }
    }, fileToConvert);

    File pdfFile = convertToPDF(fileToConvert);

    //clean-up
    tmpDir.deleteOnExit();

    return new FileInputStream(pdfFile);
}

From source file:co.cask.cdap.common.io.Locations.java

/**
 * Creates a new {@link InputSupplier} that can provides {@link SeekableInputStream} from the given location.
 *
 * @param location Location for the input stream.
 * @return A {@link InputSupplier}.//from  ww  w .  j  a v a  2s  . co  m
 */
public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final Location location) {
    return new InputSupplier<SeekableInputStream>() {
        @Override
        public SeekableInputStream getInput() throws IOException {
            InputStream input = location.getInputStream();
            try {
                if (input instanceof FileInputStream) {
                    return new FileSeekableInputStream((FileInputStream) input);
                }
                if (input instanceof FSDataInputStream) {
                    FSDataInputStream dataInput = (FSDataInputStream) input;
                    LocationFactory locationFactory = location.getLocationFactory();

                    // It should be HDFSLocationFactory
                    if (locationFactory instanceof HDFSLocationFactory) {
                        FileSystem fs = ((HDFSLocationFactory) locationFactory).getFileSystem();
                        return new DFSSeekableInputStream(dataInput,
                                createDFSStreamSizeProvider(fs, new Path(location.toURI()), dataInput));
                    } else {
                        // This shouldn't happen
                        return new DFSSeekableInputStream(dataInput, new StreamSizeProvider() {
                            @Override
                            public long size() throws IOException {
                                // Assumption is if the FS is not a HDFS fs, the location length tells the stream size
                                return location.length();
                            }
                        });
                    }
                }

                throw new IOException("Failed to create SeekableInputStream from location " + location.toURI());
            } catch (Throwable t) {
                Closeables.closeQuietly(input);
                Throwables.propagateIfInstanceOf(t, IOException.class);
                throw new IOException(t);
            }
        }
    };
}

From source file:com.facebook.buck.android.ExtractResourcesStep.java

@Override
public int execute(ExecutionContext context) {
    File outputDirectory = new File(extractedResourcesDir);
    for (String path : pathsToThirdPartyJars) {
        try (final JarFile jar = new JarFile(path)) {
            for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
                final JarEntry entry = entries.nextElement();
                String name = entry.getName();

                // No directories.
                if (entry.isDirectory()) {
                    continue;
                }/*from   www  .  ja  v a  2s  .com*/

                // No META-INF nonsense.
                if (name.startsWith("META-INF/")) {
                    continue;
                }

                // No .class files.
                if (name.endsWith(".class")) {
                    continue;
                }

                // No .java files, either.
                if (name.endsWith(".java")) {
                    continue;
                }

                File outputFile = new File(outputDirectory, name);
                Files.createParentDirs(outputFile);
                Files.copy(new InputSupplier<InputStream>() {
                    @Override
                    public InputStream getInput() throws IOException {
                        return jar.getInputStream(entry);
                    }
                }, outputFile);
            }
        } catch (IOException e) {
            e.printStackTrace(context.getStdErr());
            return 1;
        }
    }

    return 0;
}

From source file:co.cask.cdap.internal.app.program.ProgramBundle.java

private static InputSupplier<InputStream> getInputSupplier(final ApplicationSpecification appSpec) {
    return new InputSupplier<InputStream>() {
        @Override/*from   w  w  w  .  ja  va  2 s .  c  o m*/
        public InputStream getInput() throws IOException {
            String json = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator())
                    .toJson(appSpec);
            return new ByteArrayInputStream(json.getBytes(Charsets.UTF_8));
        }
    };
}