com.google.common.io.Files#asByteSource ( )源码实例Demo

下面列出了com.google.common.io.Files#asByteSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: singer   文件: ConfigFileWatcher.java
/**
 * Adds a watch on the specified file. The file must exist, otherwise a FileNotFoundException
 * is returned. If the file is deleted after a watch is established, the watcher will log errors
 * but continue to monitor it, and resume watching if it is recreated.
 *
 * @param filePath path to the file to watch.
 * @param onUpdate function to call when a change is detected to the file. The entire contents
 *                 of the file will be passed in to the function. Note that onUpdate will be
 *                 called once before this call completes, which facilities initial load of data.
 *                 This callback is executed synchronously on the watcher thread - it is
 *                 important that the function be non-blocking.
 */
public synchronized void addWatch(String filePath, Function<byte[], Void> onUpdate)
    throws IOException {
  MorePreconditions.checkNotBlank(filePath);
  Preconditions.checkNotNull(onUpdate);

  // Read the file and make the initial onUpdate call.
  File file = new File(filePath);
  ByteSource byteSource = Files.asByteSource(file);
  onUpdate.apply(byteSource.read());

  // Add the file to our map if it isn't already there, and register the new change watcher.
  ConfigFileInfo configFileInfo = watchedFileMap.get(filePath);
  if (configFileInfo == null) {
    configFileInfo = new ConfigFileInfo(file.lastModified(), byteSource.hash(HASH_FUNCTION));
    watchedFileMap.put(filePath, configFileInfo);
  }
  configFileInfo.changeWatchers.add(onUpdate);
}
 
源代码2 项目: pinlater   文件: ConfigFileWatcher.java
/**
 * Adds a watch on the specified file. The file must exist, otherwise a FileNotFoundException
 * is returned. If the file is deleted after a watch is established, the watcher will log errors
 * but continue to monitor it, and resume watching if it is recreated.
 *
 * @param filePath path to the file to watch.
 * @param onUpdate function to call when a change is detected to the file. The entire contents
 *                 of the file will be passed in to the function. Note that onUpdate will be
 *                 called once before this call completes, which facilities initial load of data.
 *                 This callback is executed synchronously on the watcher thread - it is
 *                 important that the function be non-blocking.
 */
public synchronized void addWatch(String filePath, Function<byte[], Void> onUpdate)
    throws IOException {
  MorePreconditions.checkNotBlank(filePath);
  Preconditions.checkNotNull(onUpdate);

  // Read the file and make the initial onUpdate call.
  File file = new File(filePath);
  ByteSource byteSource = Files.asByteSource(file);
  onUpdate.apply(byteSource.read());

  // Add the file to our map if it isn't already there, and register the new change watcher.
  ConfigFileInfo configFileInfo = watchedFileMap.get(filePath);
  if (configFileInfo == null) {
    configFileInfo = new ConfigFileInfo(file.lastModified(), byteSource.hash(HASH_FUNCTION));
    watchedFileMap.put(filePath, configFileInfo);
  }
  configFileInfo.changeWatchers.add(onUpdate);
}
 
源代码3 项目: Mixin   文件: MainAttributes.java
private static Attributes getDirAttributes(File dir) {
    File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
    if (manifestFile.isFile()) {
        ByteSource source = Files.asByteSource(manifestFile);
        InputStream inputStream = null;
        try {
            inputStream = source.openBufferedStream();
            Manifest manifest = new Manifest(inputStream);
            return manifest.getMainAttributes();
        } catch (IOException ex) {
            // be quiet checkstyle
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                // ignore
            }
        }
    }
    
    return null;
}
 
/**
 * Upload an object from a File using the Swift API.
 */
private void uploadObjectFromFile() throws IOException {
   System.out.format("Upload Object From File%n");

   String filename = "uploadObjectFromFile";
   String suffix = ".txt";

   File tempFile = File.createTempFile(filename, suffix);

   try {
      Files.write("uploadObjectFromFile", tempFile, Charsets.UTF_8);

      ByteSource byteSource = Files.asByteSource(tempFile);
      Payload payload = Payloads.newByteSourcePayload(byteSource);

      cloudFiles.getObjectApi(REGION, CONTAINER)
         .put(filename + suffix, payload);

      System.out.format("  %s%s%n", filename, suffix);
   } finally {
      tempFile.delete();
   }
}
 
源代码5 项目: jclouds-examples   文件: UploadLargeObject.java
/**
 * Upload a large object from a File using the BlobStore API.
 *
 * @throws ExecutionException
 * @throws InterruptedException
 */
private void uploadLargeObjectFromFile(File largeFile) throws InterruptedException, ExecutionException {
   System.out.format("Upload Large Object From File%n");

   ByteSource source = Files.asByteSource(largeFile);
   // create the payload and set the content length
   Payload payload = Payloads.newByteSourcePayload(source);
   payload.getContentMetadata().setContentLength(largeFile.length());

   Blob blob = blobStore.blobBuilder(largeFile.getName())
         .payload(payload)
         .build();

   // configure the blobstore to use multipart uploading of the file
   String eTag = blobStore.putBlob(CONTAINER, blob, multipart());

   System.out.format("  Uploaded %s eTag=%s", largeFile.getName(), eTag);
}
 
源代码6 项目: jclouds-examples   文件: CloudFilesPublish.java
/**
 * This method will put a plain text object into the container.
 */
private void createObjectFromFile() throws IOException {
   System.out.format("Create Object From File%n");

   File tempFile = File.createTempFile(FILENAME, SUFFIX);

   try {
      Files.write("Hello Cloud Files", tempFile, Charsets.UTF_8);

      ObjectApi objectApi = cloudFiles.getObjectApi(REGION, CONTAINER_PUBLISH);

      ByteSource byteSource = Files.asByteSource(tempFile);
      Payload payload = Payloads.newByteSourcePayload(byteSource);

      objectApi.put(FILENAME + SUFFIX, payload);
   } finally {
      tempFile.delete();
   }
}
 
源代码7 项目: jclouds-examples   文件: UploadObjects.java
/**
 * Upload an object from a File using the Swift API.
 */
private void uploadObjectFromFile() throws IOException {
   System.out.format("Upload Object From File%n");

   String filename = "uploadObjectFromFile";
   String suffix = ".txt";

   File tempFile = File.createTempFile(filename, suffix);

   try {
      Files.write("uploadObjectFromFile", tempFile, Charsets.UTF_8);

      ByteSource byteSource = Files.asByteSource(tempFile);
      Payload payload = Payloads.newByteSourcePayload(byteSource);

      cloudFiles.getObjectApi(REGION, CONTAINER)
         .put(filename + suffix, payload);

      System.out.format("  %s%s%n", filename, suffix);
   } finally {
      tempFile.delete();
   }
}
 
源代码8 项目: etcd-java   文件: EtcdClusterConfig.java
public static EtcdClusterConfig fromProperties(ByteSource source) throws IOException {
    Properties props = new Properties();
    try (InputStream in = source.openStream()) {
        props.load(in);
    }
    String epString = props.getProperty("endpoints");
    if (epString == null) {
        throw new IOException("etcd config must contain endpoints property");
    }
    EtcdClusterConfig config = new EtcdClusterConfig();
    config.endpoints = Sets.newHashSet(epString.split(","));
    config.user = bs(props.getProperty("username"));
    config.password = bs(props.getProperty("password"));
    config.composeDeployment = props.getProperty("compose_deployment");
    config.rootPrefix = bs(props.getProperty("root_prefix")); // a.k.a namespace
    String tlsMode = props.getProperty("tls_mode");
    if (tlsMode != null) {
        config.tlsMode = TlsMode.valueOf(tlsMode);
    }
    String certPath = props.getProperty("certificate_file");
    if (certPath != null) {
        File certFile = new File(certPath);
        if (!certFile.exists()) {
            throw new IOException("cant find certificate file: " + certPath);
        }
        config.certificate = Files.asByteSource(certFile);
    }
    config.overrideAuthority = props.getProperty("override_authority");
    return config;
}
 
源代码9 项目: openapi-generator   文件: CodeGenMojo.java
/**
 * Calculate openapi specification file hash. If specification is hosted on remote resource it is downloaded first
 *
 * @param inputSpecFile - Openapi specification input file to calculate it's hash.
 *                        Does not taken into account if input spec is hosted on remote resource
 * @return openapi specification file hash
 * @throws IOException
 */
private String calculateInputSpecHash(File inputSpecFile) throws IOException {

    URL inputSpecRemoteUrl = inputSpecRemoteUrl();

    File inputSpecTempFile = inputSpecFile;

    if (inputSpecRemoteUrl != null) {
        inputSpecTempFile = File.createTempFile("openapi-spec", ".tmp");

        URLConnection conn = inputSpecRemoteUrl.openConnection();
        if (isNotEmpty(auth)) {
            List<AuthorizationValue> authList = AuthParser.parse(auth);
            for (AuthorizationValue a : authList) {
                conn.setRequestProperty(a.getKeyName(), a.getValue());
            }
        }
        try (ReadableByteChannel readableByteChannel = Channels.newChannel(conn.getInputStream())) {
            FileChannel fileChannel;
            try (FileOutputStream fileOutputStream = new FileOutputStream(inputSpecTempFile)) {
                fileChannel = fileOutputStream.getChannel();
                fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
            }
        }
    }

    ByteSource inputSpecByteSource =
            inputSpecTempFile.exists()
                    ? Files.asByteSource(inputSpecTempFile)
                    : CharSource.wrap(ClasspathHelper.loadFileFromClasspath(inputSpecTempFile.toString().replaceAll("\\\\","/")))
                    .asByteSource(StandardCharsets.UTF_8);

    return inputSpecByteSource.hash(Hashing.sha256()).toString();
}
 
源代码10 项目: nomulus   文件: LoggingParameters.java
void configureLogging() throws IOException {
  ByteSource baseConfig = (configFile != null)
      ? Files.asByteSource(configFile.toFile())
      : DEFAULT_LOG_CONFIG;
  if (logLevel != null) {
    configLines.add(".level = " + logLevel);
  }
  // Add an extra leading newline in case base properties file does not end in a newline.
  String customProperties = "\n" + Joiner.on('\n').join(configLines);
  ByteSource logConfig =
      ByteSource.concat(baseConfig, ByteSource.wrap(customProperties.getBytes(UTF_8)));
  try (InputStream input = logConfig.openStream()) {
    LogManager.getLogManager().readConfiguration(input);
  }
}
 
源代码11 项目: jclouds-examples   文件: UploadDirectoryToCDN.java
public BlobDetail call() throws Exception {
   ByteSource byteSource = Files.asByteSource(toBeUploadedBlobDetail.getLocalFile());

   Blob blob = blobStore.blobBuilder(toBeUploadedBlobDetail.getRemoteBlobName())
         .payload(Payloads.newByteSourcePayload(byteSource))
         .contentType("") // allows Cloud Files to determine the content type
         .build();
   String eTag = blobStore.putBlob(container, blob);
   BlobDetail uploadedBlobDetail = new BlobDetail(
         toBeUploadedBlobDetail.getRemoteBlobName(), toBeUploadedBlobDetail.getLocalFile(), eTag);

   return uploadedBlobDetail;
}
 
源代码12 项目: tutorials   文件: GuavaIOUnitTest.java
@Test
public void whenReadUsingByteSource_thenRead() throws IOException {
    final String expectedValue = "Hello world";
    final File file = new File("src/test/resources/test1.in");

    final ByteSource source = Files.asByteSource(file);

    final byte[] result = source.read();
    assertEquals(expectedValue, new String(result));
}
 
源代码13 项目: purplejs   文件: FileResource.java
@Override
public ByteSource getBytes()
{
    return Files.asByteSource( this.file );
}
 
源代码14 项目: chaos-http-proxy   文件: Main.java
public static void main(String[] args) throws Exception {
    Options options = new Options();
    CmdLineParser parser = new CmdLineParser(options);
    try {
        parser.parseArgument(args);
    } catch (CmdLineException cle) {
        usage(parser);
    }

    if (options.version) {
        System.err.println(
                Main.class.getPackage().getImplementationVersion());
        System.exit(0);
    }

    ByteSource byteSource;
    if (options.propertiesFile == null) {
        byteSource = Resources.asByteSource(Resources.getResource(
                    "chaos-http-proxy.conf"));
    } else {
        byteSource = Files.asByteSource(options.propertiesFile);
    }

    ChaosConfig config;
    try (InputStream is = byteSource.openStream()) {
        config = ChaosConfig.loadFromPropertyStream(is);
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        System.exit(1);
        return;
    }

    URI proxyEndpoint = new URI("http", null, options.address,
            options.port, null, null, null);
    ChaosHttpProxy proxy = new ChaosHttpProxy(proxyEndpoint, config);
    try {
        proxy.start();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
 
源代码15 项目: buck   文件: PathByteSource.java
public static ByteSource asByteSource(Path path) {
  return Files.asByteSource(path.toFile());
}
 
源代码16 项目: Strata   文件: ArrayByteSourceTest.java
@Test
public void test_from_GuavaFileByteSource() {
  ByteSource source = Files.asByteSource(new File("pom.xml"));
  ArrayByteSource test = ArrayByteSource.from(source);
  assertThat(test.getFileName()).hasValue("pom.xml");
}
 
源代码17 项目: Strata   文件: XmlFileTest.java
@Test
public void test_of_ByteSource_ioException() {
  ByteSource source = Files.asByteSource(new File("/oh-dear-no-such-file"));
  assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> XmlFile.of(source));
}
 
源代码18 项目: Strata   文件: XmlFileTest.java
@Test
public void test_of_ByteSource_parsedReferences_ioException() {
  ByteSource source = Files.asByteSource(new File("/oh-dear-no-such-file"));
  assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> XmlFile.of(source, "key"));
}
 
源代码19 项目: Strata   文件: XmlFileTest.java
@Test
public void test_parseElements_ByteSource_Fn_ioException() {
  ByteSource source = Files.asByteSource(new File("/oh-dear-no-such-file"));
  assertThatExceptionOfType(UncheckedIOException.class)
      .isThrownBy(() -> XmlFile.parseElements(source, name -> Integer.MAX_VALUE));
}
 
源代码20 项目: jclouds-examples   文件: BlobUploader.java
@Override
public void run() {
   /**
    * Instantiate the ThreadLocal variables when this thread runs for the first time.
    * Instantiating this in the constructor will not work (different thread).
    */
   if (blobStore.get() == null) {
      // It is usually a good idea to include the currentThread when logging parallel tasks.
      System.out.println("Creating connection for thread " + Thread.currentThread());
      /**
       * In some cases, especially when running very large jobs with many parallel threads, some connections will
       * break. In that case, we need to be able to obtain a new connection (and socket) to the service, which is
       * why this is factored out.
       */
      resetBlobstore(username, password, provider, region);
   }

   if (container.get() == null) {
      container.set(UUID.randomUUID().toString());
      Location location = getOnlyElement(blobStore.get().listAssignableLocations());
      blobStore.get().createContainerInLocation(location, container.get());

      System.out.println("Created container " + container.get() +
            " for thread " + Thread.currentThread() +
            " in " + location.toString());
   }

   // The md5 as returned by the service, and as calculated locally.
   String md5Local;
   String md5Remote;
   Blob blob;

   try {
      md5Local = BaseEncoding.base16().encode(Files.hash(file, Hashing.md5()).asBytes()).toLowerCase();
   } catch (java.io.IOException e) {
      e.printStackTrace();
      /**
       * The file is no longer available on the local FS.
       * In some application cases, you might also want to retry this instead of finishing the unit of work.
       */
      return;
   }

   ByteSourcePayload bsp = new ByteSourcePayload(Files.asByteSource(file));

   /**
    * Uploading a file over a network is an inherently fragile operation. Over thousands of files, especially in
    * highly parallel jobs that tax upload bandwidth, a small percent of uploads are guaranteed to fail.
    */
   do {
      System.out.println("Uploading " + file.getName() + " ; " + FileUtils.sizeOf(file));
      blob = blobStore.get().blobBuilder(file.getName())
               .payload(bsp)
               .build();
      md5Remote = blobStore.get().putBlob(container.get(), blob).toLowerCase();
      if (md5Local.equals(md5Remote)) {
         long total = BlobUploaderMain.bytesUploaded.addAndGet(FileUtils.sizeOf(file));
         System.out.println("Uploaded MB: " + (int)total / FileUtils.ONE_MB + "MB ; " + (int)((float)BlobUploaderMain.bytesUploaded.get() / BlobUploaderMain.totalBytes) * 100 + "%");
         bsp.release();
         return;
      } else {
         System.out.printf("md5 mismatch %s vs %s, retrying %s", md5Local, md5Remote, file.getName());
      }
   } while(true);
}