List of usage examples for com.google.common.io InputSupplier InputSupplier
InputSupplier
From source file:com.metamx.druid.indexing.coordinator.ForkingTaskRunner.java
@Override public Optional<InputSupplier<InputStream>> streamTaskLog(final String taskid, final long offset) { final ProcessHolder processHolder; synchronized (tasks) { final ForkingTaskRunnerWorkItem taskWorkItem = tasks.get(taskid); if (taskWorkItem != null && taskWorkItem.processHolder != null) { processHolder = taskWorkItem.processHolder; } else {//from w w w. j av a 2 s. com return Optional.absent(); } } return Optional.<InputSupplier<InputStream>>of(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { final RandomAccessFile raf = new RandomAccessFile(processHolder.logFile, "r"); final long rafLength = raf.length(); if (offset > 0) { raf.seek(offset); } else if (offset < 0 && offset < rafLength) { raf.seek(rafLength + offset); } return Channels.newInputStream(raf.getChannel()); } }); }
From source file:co.cask.cdap.internal.app.runtime.distributed.AbstractDistributedProgramRunner.java
/** * Copies the program jar to a local temp file and return a {@link Program} instance * with {@link Program#getJarLocation()} points to the local temp file. *///from w w w . j av a 2s . c om private Program copyProgramJar(final Program program, File tempDir, File programDir) throws IOException { File tempJar = File.createTempFile(program.getName(), ".jar", tempDir); Files.copy(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return program.getJarLocation().getInputStream(); } }, tempJar); return Programs.createWithUnpack(Locations.toLocation(tempJar), programDir); }
From source file:io.fabric8.process.manager.service.ProcessManagerService.java
protected DownloadStrategy createDeafultDownloadStrategy() { return new DownloadStrategy() { @Override/* w ww .j av a2s . c o m*/ public File downloadContent(final URL sourceUrl, final File installDir) throws IOException { // copy the URL to the install dir File archive = new File(installDir, INSTALLED_BINARY); Files.copy(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return sourceUrl.openStream(); } }, archive); return archive; } }; }
From source file:com.twitter.elephanttwin.util.HdfsUtils.java
/** * Same as {@link openInputStream}, except with a supplier as an extra level of indirection. * * @param dataFile/*from w ww .j a v a 2 s.c o m*/ * @return InputSupplier that provides the input stream * @throws IOException */ public static InputSupplier<InputStream> getInputStreamSupplier(String dataFile) throws IOException { Preconditions.checkNotNull(dataFile); final InputStream in; if (dataFile.endsWith(".lzo")) { // Properly handle compressed files. LzopCodec codec = new LzopCodec(); codec.setConf(new Configuration()); in = codec.createInputStream(openFile(dataFile)); } else if (dataFile.endsWith(".gz")) { GzipCodec codec = new GzipCodec(); codec.setConf(new Configuration()); in = codec.createInputStream(openFile(dataFile)); } else { in = openFile(dataFile); } return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { // TODO(Jimmy Lin): JohnS noted that this breaks the contract of InputSupplier // on 2 counts---the open is not lazy/initiated by the caller of getInput and // getInput does not return a fresh stream on each call. return in; } }; }
From source file:com.twitter.elephanttwin.util.HdfsUtils.java
/** * Returns a InputSupplier that returns a stream which is the concatenation of all source * streams. Note that this method may not make sense for certain binary-encoded streams, but * is convenient for processing multiple Hadoop part files in a line-delimited record format. * * @param streams source streams//from w w w . jav a2 s. co m * @return InputSupplier supplying a concatenated stream * @throws IOException */ public static InputSupplier<InputStream> getInputStreamSupplier(InputStream... streams) throws IOException { // TODO(Jimmy Lin): JohnS noted that this breaks the contract of InputSupplier // on 2 counts---the open is not lazy/initiated by the caller of getInput and // getInput does not return a fresh stream on each call. // Furthermore: This only makes sense with InputSupplier<InputStream> inputs and then // ByteStreams.join already has this covered. return ByteStreams.join(Iterables.transform(Lists.newArrayList(streams), new Function<InputStream, InputSupplier<InputStream>>() { @Override public InputSupplier<InputStream> apply(final InputStream stream) { return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return stream; } }; } })); }
From source file:org.akraievoy.couch.CouchDao.java
public InputSupplier<InputStream> couchFileGet(final Squab.Path path, final String fileName) { try {//from w w w .j ava2s .co m final URL url = new URL( couchSetup.getCouchDbUrl() + dbNameDynamic() + "/" + url(path.id()) + "/" + url(fileName)); return new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setUseCaches(true); connection.setDoInput(true); connection.setDoOutput(false); return new FilterInputStream(connection.getInputStream()) { @Override public void close() throws IOException { connection.disconnect(); super.close(); } }; } }; } catch (IOException e) { throw new IllegalStateException("failed to construct URL", e); } }
From source file:com.twitter.elephanttwin.util.HdfsUtils.java
/** * Returns a InputSupplier that returns a stream which is the concatenation of all source * files. Note that this method may not make sense for certain binary-encoded streams, but * is convenient for processing multiple Hadoop part files in a line-delimited record format. * * @param files source files/*from ww w.j av a 2s . c o m*/ * @return InputSupplier supplying a concatenated stream * @throws IOException */ public static InputSupplier<InputStream> getInputStreamSupplier(List<String> files) throws IOException { return ByteStreams.join(Iterables.transform(files, new Function<String, InputSupplier<InputStream>>() { @Override public InputSupplier<InputStream> apply(final String file) { return new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return openInputStream(file); } }; } })); }
From source file:elw.web.ControllerElw.java
protected static InputSupplier<InputStream> supplierForRequest(final HttpServletRequest myReq) { return new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { return myReq.getInputStream(); }/*from ww w . jav a2s . c om*/ }; }
From source file:elw.web.ControllerElw.java
protected static InputSupplier<InputStream> supplierForFileItem(final FileItemStream item) { return new InputSupplier<InputStream>() { public InputStream getInput() throws IOException { return item.openStream(); }//w w w . j a v a2s . c o m }; }
From source file:com.vmware.thinapp.common.util.AfUtil.java
/** * Attempt to download the icon specified by the given URL. If the resource at the URL * has a content type of image/*, the binary data for this resource will be downloaded. * * @param iconUrlStr URL of the image resource to access * @return the binary data and content type of the image resource at the given URL, null * if the URL is invalid, the resource does not have a content type starting with image/, or * on some other failure.// w w w . ja v a 2s .c om */ public static final @Nullable IconInfo getIconInfo(String iconUrlStr) { if (!StringUtils.hasLength(iconUrlStr)) { log.debug("No icon url exists."); return null; } URL iconUrl = null; try { // Need to encode any invalid characters before creating the URL object iconUrl = new URL(UriUtils.encodeHttpUrl(iconUrlStr, "UTF-8")); } catch (MalformedURLException ex) { log.debug("Malformed icon URL string: {}", iconUrlStr, ex); return null; } catch (UnsupportedEncodingException ex) { log.debug("Unable to encode icon URL string: {}", iconUrlStr, ex); return null; } // Open a connection with the given URL final URLConnection conn; final InputStream inputStream; try { conn = iconUrl.openConnection(); inputStream = conn.getInputStream(); } catch (IOException ex) { log.debug("Unable to open connection to URL: {}", iconUrlStr, ex); return null; } String contentType = conn.getContentType(); int sizeBytes = conn.getContentLength(); try { // Make sure the resource has an appropriate content type if (!conn.getContentType().startsWith("image/")) { log.debug("Resource at URL {} does not have a content type of image/*.", iconUrlStr); return null; // Make sure the resource is not too large } else if (sizeBytes > MAX_ICON_SIZE_BYTES) { log.debug("Image resource at URL {} is too large: {}", iconUrlStr, sizeBytes); return null; } else { // Convert the resource to a byte array byte[] iconBytes = ByteStreams.toByteArray(new InputSupplier<InputStream>() { @Override public InputStream getInput() throws IOException { return inputStream; } }); return new IconInfo(iconBytes, contentType); } } catch (IOException e) { log.debug("Error reading resource data.", e); return null; } finally { Closeables.closeQuietly(inputStream); } }