Java Code Examples for com.google.common.io.Files#asByteSource()

The following examples show how to use com.google.common.io.Files#asByteSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigFileWatcher.java    From singer with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: UploadObjects.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
   }
}
 
Example 3
Source File: ConfigFileWatcher.java    From pinlater with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: CloudFilesPublish.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
   }
}
 
Example 5
Source File: MainAttributes.java    From Mixin with MIT License 6 votes vote down vote up
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;
}
 
Example 6
Source File: UploadLargeObject.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: UploadObjectsWithServiceNet.java    From jclouds-examples with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
   }
}
 
Example 8
Source File: EtcdClusterConfig.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: GuavaIOUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@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));
}
 
Example 10
Source File: UploadDirectoryToCDN.java    From jclouds-examples with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: LoggingParameters.java    From nomulus with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 12
Source File: CodeGenMojo.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 13
Source File: XmlFileTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@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));
}
 
Example 14
Source File: XmlFileTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@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"));
}
 
Example 15
Source File: XmlFileTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@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));
}
 
Example 16
Source File: BlobUploader.java    From jclouds-examples with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example 17
Source File: ArrayByteSourceTest.java    From Strata with Apache License 2.0 4 votes vote down vote up
@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");
}
 
Example 18
Source File: PathByteSource.java    From buck with Apache License 2.0 4 votes vote down vote up
public static ByteSource asByteSource(Path path) {
  return Files.asByteSource(path.toFile());
}
 
Example 19
Source File: Main.java    From chaos-http-proxy with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example 20
Source File: FileResource.java    From purplejs with Apache License 2.0 4 votes vote down vote up
@Override
public ByteSource getBytes()
{
    return Files.asByteSource( this.file );
}