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

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

Introduction

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

Prototype

T getInput() throws IOException;

Source Link

Document

Returns an object that encapsulates a readable resource.

Usage

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

public void storeTypes(InputSupplier<? extends InputStream> resource) {
    TransactionBuilder tx = newTransaction(null, "Initialize OAuth Type Definitions");

    ArtifactId artifactId = OAUTH_TYPES_ARTIFACT;
    if (!typesExist()) {
        tx.createArtifact(CoreArtifactTypes.OseeTypeDefinition, artifactId.getName(), artifactId.getGuid());
    }//from  w  w  w.  j  a v a 2 s  .c  o  m
    InputStream stream = null;
    try {
        stream = resource.getInput();
        tx.setSoleAttributeFromStream(artifactId, CoreAttributeTypes.UriGeneralStringData, stream);
    } catch (IOException ex) {
        throw new OseeCoreException(ex);
    } finally {
        Lib.close(stream);
    }
    tx.commit();
    reloadTypes();
}

From source file:bammerbom.ultimatecore.bukkit.resources.utils.AttributeUtil.java

/**
 * Load the content of a file from a stream.
 * <p>//from w w w .j  a v a 2 s .com
 * Use {@link Files#newInputStreamSupplier(java.io.File)} to provide a
 * stream from a file.
 *
 * @param stream - the stream supplier.
 * @param option - whether or not to decompress the input stream.
 * @return The decoded NBT compound.
 * @throws IOException If anything went wrong.
 */
public static NbtCompound fromStream(InputSupplier<? extends InputStream> stream, StreamOptions option)
        throws IOException {
    InputStream input = null;
    DataInputStream data = null;
    boolean suppress = true;

    try {
        input = stream.getInput();
        data = new DataInputStream(new BufferedInputStream(
                option == StreamOptions.GZIP_COMPRESSION ? new GZIPInputStream(input) : input));

        NbtCompound result = fromCompound(get().LOAD_COMPOUND.loadNbt(data));
        suppress = false;
        return result;

    } finally {
        if (data != null) {
            Closeables.close(data, suppress);
        } else if (input != null) {
            Closeables.close(input, suppress);
        }
    }
}

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

private List<List<SpanAndParseIdx>> loadFromCandidateFile(List<String> frameElementLines,
        InputSupplier<? extends InputStream> candidateInputSupplier) throws IOException {
    final List<List<SpanAndParseIdx>> candidateLines = Lists.newArrayList();
    final Scanner canScanner = new Scanner(candidateInputSupplier.getInput());
    for (String ignored : frameElementLines) {
        final List<SpanAndParseIdx> spanList = Lists.newArrayList();
        final String spanTokens[] = canScanner.nextLine().trim().split("\t|:");
        for (int i = 0; i < spanTokens.length; i += 2) {
            final Range0Based span = createSpanRange(parseInt(spanTokens[i]), parseInt(spanTokens[i + 1]));
            spanList.add(new SpanAndParseIdx(span, 0));
        }//from ww w  .ja  v a2 s .  com
        //add null span to candidates
        spanList.add(EMPTY_SPAN_AND_PARSE_IDX);
        candidateLines.add(spanList);
    }
    return candidateLines;
}

From source file:com.netflix.exhibitor.core.index.LogIndexer.java

public LogIndexer(Exhibitor exhibitor, InputSupplier<InputStream> source, String sourceName, long sourceLength,
        File indexDirectory) throws Exception {
    this.exhibitor = exhibitor;
    if (!indexDirectory.exists() && !indexDirectory.mkdirs()) {
        throw new IOException("Could not make: " + indexDirectory);
    }// w ww .j  av  a  2  s  .c o m
    this.sourceLength = sourceLength;
    this.sourceName = sourceName;

    this.indexDirectory = indexDirectory;
    inputStream = new CountingInputStream(new BufferedInputStream(source.getInput()));

    IndexWriter localWriter = null;
    NIOFSDirectory localDirectory = null;

    logParser = new ZooKeeperLogParser(inputStream);
    if (logParser.isValid()) {
        IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_35, new KeywordAnalyzer())
                .setOpenMode(IndexWriterConfig.OpenMode.CREATE);

        localDirectory = new NIOFSDirectory(indexDirectory, new SingleInstanceLockFactory());
        localWriter = new IndexWriter(localDirectory, conf);
    }
    writer = localWriter;
    directory = localDirectory;
}

From source file:com.cloudera.gertrude.space.AvroExperimentSpaceDeserializer.java

@Override
protected Optional<ExperimentSpace> deserialize(ExperimentSpace.Serialized serialized) throws IOException {
    ExperimentDeployment merged = null;//from   w  ww  . j  av  a 2 s .c  o  m
    ExperimentDeployment curr = null;
    if (avroFileInput) {
        for (InputSupplier<? extends InputStream> is : serialized.getSerializedData()) {
            SeekableInput si = new SeekableByteArrayInput(ByteStreams.toByteArray(is));
            FileReader<ExperimentDeployment> dfr = DataFileReader.openReader(si, reader);
            while (dfr.hasNext()) {
                merged = merge(merged, dfr.next(curr));
            }
        }
    } else {
        BinaryDecoder decoder = null;
        for (InputSupplier<? extends InputStream> is : serialized.getSerializedData()) {
            decoder = DecoderFactory.get().binaryDecoder(is.getInput(), decoder);
            merged = merge(merged, reader.read(curr, decoder));
        }
    }

    return merged == null ? Optional.<ExperimentSpace>absent()
            : Optional.fromNullable(load(merged, serialized.getVersionIdentifier()));
}

From source file:co.cask.cdap.internal.app.runtime.adapter.AdapterService.java

private ApplicationTemplateInfo getTemplateInfo(File jarFile)
        throws InterruptedException, ExecutionException, TimeoutException, IOException {
    ApplicationTemplateInfo existing = fileToTemplateMap.get().get(jarFile.getAbsoluteFile());
    HashCode fileHash = Files.hash(jarFile, Hashing.md5());
    // if the file is the same, just return
    if (existing != null && fileHash.equals(existing.getFileHash())) {
        return existing;
    }/*  w  w  w. j a v a 2  s . c  om*/

    // instantiate the template application and call configure() on it to determine it's specification
    InMemoryConfigurator configurator = new InMemoryConfigurator(
            new LocalLocationFactory().create(jarFile.toURI()), null);
    ListenableFuture<ConfigResponse> result = configurator.config();
    ConfigResponse response = result.get(2, TimeUnit.MINUTES);
    InputSupplier<? extends Reader> configSupplier = response.get();
    if (response.getExitCode() != 0 || configSupplier == null) {
        throw new IllegalArgumentException("Failed to get template info");
    }
    ApplicationSpecification spec;
    try (Reader configReader = configSupplier.getInput()) {
        spec = GSON.fromJson(configReader, ApplicationSpecification.class);
    }

    // verify that the name is ok
    Id.Application.from(Constants.DEFAULT_NAMESPACE_ID, spec.getName());

    // determine the program type of the template
    ProgramType programType;
    int numWorkflows = spec.getWorkflows().size();
    int numWorkers = spec.getWorkers().size();
    if (numWorkers == 0 && numWorkflows == 1) {
        programType = ProgramType.WORKFLOW;
    } else if (numWorkers == 1 && numWorkflows == 0) {
        programType = ProgramType.WORKER;
    } else {
        throw new IllegalArgumentException(
                "An application template must contain exactly one worker or one workflow.");
    }

    return new ApplicationTemplateInfo(jarFile, spec.getName(), spec.getDescription(), programType, fileHash);
}

From source file:com.eucalyptus.bootstrap.BootstrapClassLoader.java

public void loadResourceBinding(String path) throws JiBXException, IOException {
    if (this.isBound) {
        throw new IllegalStateException("Call not allowed after bindings compiled");
    } else {//from ww  w .ja va 2 s.  c o  m
        URL url = Resources.getResource(path);
        InputSupplier<InputStream> inSupplier = Resources.newInputStreamSupplier(url);
        if (inSupplier == null) {
            throw new IOException("Resource " + path + " not found");
        } else {
            String fname = path;
            int split = fname.lastIndexOf('/');
            if (split >= 0) {
                fname = fname.substring(split + 1);
            }
            String defaultBindingName = Utility.bindingFromFileName(fname);
            try {
                ValidationContext vctx = BindingElement.newValidationContext();
                BindingElement root = BindingElement.validateBinding(fname, url, inSupplier.getInput(), vctx);
                if (vctx.getErrorCount() == 0 && vctx.getFatalCount() == 0) {
                    ClassFile classFile = findMappedClass(root);
                    String tpack = root.getTargetPackage();
                    if (tpack == null && classFile != null) {
                        tpack = classFile.getPackage();
                    }
                    String bindingName = root.getName();
                    UnmarshallingContext uctx = new UnmarshallingContext();
                    uctx.setDocument(inSupplier.getInput(), fname, null);
                    if (classFile != null) {
                        bindingName = (bindingName == null ? defaultBindingName : bindingName);
                        BoundClass.setModify(classFile.getRoot(), tpack, bindingName);
                    }
                    BindingDefinition bindingDefinition = BindingBuilder.unmarshalBindingDefinition(uctx,
                            defaultBindingName, url);
                    File rootFile = null;
                    if (tpack == null) {
                        tpack = bindingDefinition.getDefaultPackage();
                    }
                    if (classFile == null) {
                        rootFile = ClassCache.getModifiablePath();
                        if (root == null) {
                            throw new IllegalStateException(
                                    "Need modifiable directory on classpath for storing generated factory class file");
                        }
                        if (tpack == null) {
                            tpack = "";
                        }
                    } else {
                        rootFile = classFile.getRoot();
                        if (tpack == null) {
                            tpack = classFile.getPackage();
                        }
                    }
                    bindingDefinition.setFactoryLocation(tpack, rootFile);
                }
            } catch (JiBXException ex) {
                LOG.error("Unable to process binding " + url, ex);
            }
        }
    }
}

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

/**
 * Reads conll sentences, parses them, and writes the json-serialized results.
 *
 * @param inputSupplier where to read conll sentences from
 * @param outputSupplier where to write the results to
 * @param numThreads the number of threads to use
 * @throws IOException/*from   w  w w  .j a  va  2 s  .co  m*/
 * @throws InterruptedException
 */
public void runParser(final InputSupplier<? extends Readable> inputSupplier,
        final OutputSupplier<? extends Writer> outputSupplier, final int numThreads)
        throws IOException, InterruptedException {
    // use the producer-worker-consumer pattern to parse all sentences in multiple threads, while keeping
    // output in order.
    final BlockingQueue<Future<Optional<SemaforParseResult>>> results = Queues
            .newLinkedBlockingDeque(5 * numThreads);
    final ExecutorService workerThreadPool = newFixedThreadPool(numThreads);
    // try to shutdown gracefully. don't worry too much if it doesn't work
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                workerThreadPool.shutdown();
                workerThreadPool.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException ignored) {
            }
        }
    }));

    final PrintWriter output = new PrintWriter(outputSupplier.getOutput());
    try {
        // Start thread to fetch computed results and write to file
        final Thread consumer = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        final Optional<SemaforParseResult> oResult = results.take().get();
                        if (!oResult.isPresent())
                            break; // got poison pill. we're done
                        output.println(oResult.get().toJson());
                        output.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        consumer.start();
        // in main thread, put placeholders on results queue (so results stay in order), then
        // tell a worker thread to fill up the placeholder
        final SentenceCodec.SentenceIterator sentences = ConllCodec.readInput(inputSupplier.getInput());
        try {
            int i = 0;
            while (sentences.hasNext()) {
                final Sentence sentence = sentences.next();
                final int sentenceId = i;
                results.put(workerThreadPool.submit(new Callable<Optional<SemaforParseResult>>() {
                    @Override
                    public Optional<SemaforParseResult> call() throws Exception {
                        final long start = System.currentTimeMillis();
                        try {
                            final SemaforParseResult result = parseSentence(sentence);
                            final long end = System.currentTimeMillis();
                            System.err.printf("parsed sentence %d in %d millis.%n", sentenceId, end - start);
                            return Optional.of(result);
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw e;
                        }
                    }
                }));
                i++;
            }
            // put a poison pill on the queue to signal that we're done
            results.put(workerThreadPool.submit(new Callable<Optional<SemaforParseResult>>() {
                @Override
                public Optional<SemaforParseResult> call() throws Exception {
                    return Optional.absent();
                }
            }));
            workerThreadPool.shutdown();
        } finally {
            closeQuietly(sentences);
        }
        // wait for consumer to finish
        consumer.join();
    } finally {
        closeQuietly(output);
    }
    System.err.println("Done.");
}

From source file:co.cask.cdap.gateway.handlers.AppFabricHttpHandler.java

@Nullable
private SessionInfo retrieve(String accountId) {
    try {/*ww  w  .  ja  v  a 2  s  . c o m*/
        final Location outputDir = locationFactory.create(archiveDir + "/" + accountId);
        if (!outputDir.exists()) {
            return null;
        }
        final Location sessionInfoFile = outputDir.append("session.json");
        InputSupplier<Reader> reader = new InputSupplier<Reader>() {
            @Override
            public Reader getInput() throws IOException {
                return new InputStreamReader(sessionInfoFile.getInputStream(), "UTF-8");
            }
        };

        Gson gson = new GsonBuilder().registerTypeAdapter(Location.class, new LocationCodec(locationFactory))
                .create();
        Reader r = reader.getInput();
        try {
            return gson.fromJson(r, SessionInfo.class);
        } finally {
            Closeables.closeQuietly(r);
        }
    } catch (IOException e) {
        LOG.warn("Failed to retrieve session info for account.");
    }
    return null;
}