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.ning.billing.osgi.bundles.analytics.AnalyticsTestSuiteWithEmbeddedDB.java

public static String toString(final InputStream stream) throws IOException {
    final InputSupplier<InputStream> inputSupplier = new InputSupplier<InputStream>() {
        @Override/*from  w w w.jav  a  2s .  c  o m*/
        public InputStream getInput() throws IOException {
            return stream;
        }
    };

    return CharStreams.toString(CharStreams.newReaderSupplier(inputSupplier, Charsets.UTF_8));
}

From source file:ch.fancytools.ballbotsche.restresource.RegisterResource.java

@GET
@Produces(MediaType.TEXT_HTML)/*from  www .j av a2 s . c om*/
public String deliverRegisterPage() {
    try {
        InputSupplier<? extends InputStream> supplier = new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                return getClass().getClassLoader().getResourceAsStream("register.html");
            }
        };
        return CharStreams.toString(CharStreams.newReaderSupplier(supplier, Charset.forName("UTF-8")));
    } catch (IOException ex) {
        java.util.logging.Logger.getLogger(RegisterResource.class.getName()).log(Level.SEVERE, null, ex);
        return "failed, hard.";
    }
}

From source file:com.golf.mvc.multipart.FileStream.java

public FileStream(int fileThreshold, boolean resetOnFinalize) {
    this.fileThreshold = fileThreshold;
    this.resetOnFinalize = resetOnFinalize;
    memory = new MemoryOutput();
    out = memory;/*from   ww  w .ja v a  2  s . c  o  m*/

    if (resetOnFinalize) {
        supplier = new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return openStream();
            }

            @Override
            protected void finalize() {
                try {
                    reset();
                } catch (Throwable t) {
                    t.printStackTrace(System.err);
                }
            }
        };
    } else {
        supplier = new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return openStream();
            }
        };
    }
}

From source file:org.b1.pack.standard.writer.CompressedFormatDetector.java

private static void readResource(final URL url, ImmutableSet.Builder<String> builder) throws IOException {
    StringTokenizer tokenizer = new StringTokenizer(CharStreams.toString(new InputSupplier<Reader>() {
        @Override/*ww w  . j ava  2  s  .  c  o  m*/
        public Reader getInput() throws IOException {
            return new InputStreamReader(url.openStream(), Charsets.UTF_8);
        }
    }));
    while (tokenizer.hasMoreTokens()) {
        builder.add(tokenizer.nextToken().toLowerCase());
    }
}

From source file:com.comphenix.protocol.compat.guava.Guava10.java

@Override
@SuppressWarnings("unchecked")
public DataInputStream addHeader(final DataInputStream input, final PacketType type) {
    InputSupplier<InputStream> header = new InputSupplier<InputStream>() {
        @Override/*from  w  w  w. jav  a 2 s . c o  m*/
        public InputStream getInput() throws IOException {
            byte[] data = new byte[] { (byte) type.getLegacyId() };
            return new ByteArrayInputStream(data);
        }
    };

    InputSupplier<InputStream> data = new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            return input;
        }
    };

    // Combine them into a single stream
    try {
        return new DataInputStream(ByteStreams.join(header, data).getInput());
    } catch (IOException e) {
        throw new RuntimeException("Cannot add header.", e);
    }
}

From source file:edu.cmu.cs.lti.ark.fn.parsing.FEDict.java

private static Map<String, THashSet<String>> readInput(final InputSupplier<? extends InputStream> inputSupplier)
        throws LoadingException {
    try {/*www .  j a  va  2s .c  om*/
        return SerializedObjects.readObject(new InputSupplier<ObjectInputStream>() {
            @Override
            public ObjectInputStream getInput() throws IOException {
                return new ObjectInputStream(new BufferedInputStream(inputSupplier.getInput()));
            }
        });
    } catch (Exception e) {
        throw new LoadingException(e);
    }
}

From source file:co.cask.cdap.shell.CLIConfig.java

private static String tryGetVersion() {
    try {//from   w  w w  .j  a v  a2  s.  c  om
        InputSupplier<? extends InputStream> versionFileSupplier = new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return VersionCommand.class.getClassLoader().getResourceAsStream("VERSION");
            }
        };
        return CharStreams.toString(CharStreams.newReaderSupplier(versionFileSupplier, Charsets.UTF_8));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

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

/**
 * Creates a new {@link InputSupplier} that can provides {@link SeekableInputStream} of the given path.
 *
 * @param fs The {@link org.apache.hadoop.fs.FileSystem} for the given path.
 * @param path The path to create {@link co.cask.cdap.common.io.SeekableInputStream} when requested.
 * @return A {@link InputSupplier}./*from w  w w  .  j  av  a2  s  .  com*/
 */
public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final FileSystem fs,
        final Path path) {
    return new InputSupplier<SeekableInputStream>() {
        @Override
        public SeekableInputStream getInput() throws IOException {
            FSDataInputStream input = fs.open(path);
            try {
                return new DFSSeekableInputStream(input, createDFSStreamSizeProvider(fs, path, input));
            } catch (Throwable t) {
                Closeables.closeQuietly(input);
                Throwables.propagateIfInstanceOf(t, IOException.class);
                throw new IOException(t);
            }
        }
    };
}

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

@Override
public int hashCode() {
    try {//from ww w. j a v a2  s . c  o  m
        return ByteStreams.hash(new InputSupplier<InputStream>() {
            @Override
            public InputStream getInput() throws IOException {
                return newInput();
            }
        }, Hashing.murmur3_32()).asInt();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.cinchapi.concourse.util.Resources.java

/**
 * Finds a resource with a given name. The rules for searching resources
 * associated with a given class are implemented by the defining
 * {@linkplain ClassLoader class loader} of the class. This method
 * delegates to this object's class loader. If this object was loaded by
 * the bootstrap class loader, the method delegates to
 * {@link ClassLoader#getSystemResourceAsStream}.
 * /*from   ww w  .  j a va 2s.  co  m*/
 * <p>
 * Before delegation, an absolute resource name is constructed from the
 * given resource name using this algorithm:
 * 
 * <ul>
 * 
 * <li>If the {@code name} begins with a {@code '/'} (<tt>'&#92;u002f'</tt>
 * ), then the absolute name of the resource is the portion of the
 * {@code name} following the {@code '/'}.
 * 
 * <li>Otherwise, the absolute name is of the following form:
 * 
 * <blockquote> {@code modified_package_name/name} </blockquote>
 * 
 * <p>
 * Where the {@code modified_package_name} is the package name of this
 * object with {@code '/'} substituted for {@code '.'} (
 * <tt>'&#92;u002e'</tt>).
 * 
 * </ul>
 * 
 * @param name name of the desired resource
 * @return A {@link java.io.InputStream} object or {@code null} if
 *         no resource with this name is found
 * @throws NullPointerException If {@code name} is {@code null}
 * @since JDK1.1
 */
public static URL get(final String name) {
    File temp;
    try {
        temp = File.createTempFile("java-resource", "tmp");
        Files.copy(new InputSupplier<InputStream>() {

            @Override
            public InputStream getInput() throws IOException {
                return this.getClass().getResourceAsStream(name);
            }

        }, temp);
        return temp.toURI().toURL();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

}