private void checkThatTheFileWasReceivedSuccessfully(final File file) throws IOException {
    assertTrue("Should exist: " + file, file.exists());

    final ByteSource originalFile = Files.asByteSource(image.getFile());
    final ByteSource savedFile = Files.asByteSource(file);

    assertTrue(originalFile.contentEquals(savedFile));
  }
 @Test
 public void sanitizeWorkingDirectory() throws IOException {
   workspace.runBuckBuild("//:simple#default,static").assertSuccess();
   Path lib = workspace.getPath("buck-out/gen/simple#default,static/libsimple.a");
   String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
   assertFalse(lib.toString(), contents.contains(tmp.getRootPath().toString()));
 }
  public static void main(String[] args) throws IOException {
    Closer closer = Closer.create();
    // copy a file
    File origin = new File("join_temp");
    File copy = new File("target_temp");

    try {
      BufferedReader reader = new BufferedReader(new FileReader("join_temp"));
      BufferedWriter writer = new BufferedWriter(new FileWriter("target_temp"));

      closer.register(reader);
      closer.register(writer);

      String line;

      while ((line = reader.readLine()) != null) {
        writer.write(line);
      }
    } catch (IOException e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }

    Files.copy(origin, copy);

    File moved = new File("moved");

    // moving renaming
    Files.move(copy, moved);

    // working files as string
    List<String> lines = Files.readLines(origin, Charsets.UTF_8);

    HashCode hashCode = Files.hash(origin, Hashing.md5());
    System.out.println(hashCode);

    // file write and append
    String hamlet = "To be, or not to be it is a question\n";
    File write_and_append = new File("write_and_append");

    Files.write(hamlet, write_and_append, Charsets.UTF_8);

    Files.append(hamlet, write_and_append, Charsets.UTF_8);

    //        write_and_append.deleteOnExit();

    Files.write("OverWrite the file", write_and_append, Charsets.UTF_8);

    // ByteSource ByteSink
    ByteSource fileBytes = Files.asByteSource(write_and_append);
    byte[] readBytes = fileBytes.read();
    // equals to pre line -> Files.toByteArray(write_and_append) == readBytes

    ByteSink fileByteSink = Files.asByteSink(write_and_append);
    fileByteSink.write(Files.toByteArray(write_and_append));

    BaseEncoding base64 = BaseEncoding.base64();
    System.out.println(base64.encode("123456".getBytes()));
  }
Пример #4
0
 @Test
 public void createByteSourceFromFileTest() throws Exception {
   File f1 = new File("src/main/resources/sample.pdf");
   ByteSource byteSource = Files.asByteSource(f1);
   byte[] readBytes = byteSource.read();
   assertThat(readBytes, is(Files.toByteArray(f1)));
 }
Пример #5
0
  public CaliperConfig loadOrCreate() throws InvalidConfigurationException {
    File configFile = options.caliperConfigFile();
    ImmutableMap<String, String> defaults;
    try {
      defaults =
          Util.loadProperties(
              Util.resourceSupplier(CaliperConfig.class, "global-config.properties"));
    } catch (IOException impossible) {
      throw new AssertionError(impossible);
    }

    // TODO(kevinb): deal with migration issue from old-style .caliperrc

    if (configFile.exists()) {
      try {
        ImmutableMap<String, String> user = Util.loadProperties(Files.asByteSource(configFile));
        return new CaliperConfig(mergeProperties(options.configProperties(), user, defaults));
      } catch (IOException keepGoing) {
      }
    }

    ByteSource supplier = Util.resourceSupplier(CaliperConfig.class, "default-config.properties");
    tryCopyIfNeeded(supplier, configFile);

    ImmutableMap<String, String> user;
    try {
      user = Util.loadProperties(supplier);
    } catch (IOException e) {
      throw new AssertionError(e); // class path must be messed up
    }
    return new CaliperConfig(mergeProperties(options.configProperties(), user, defaults));
  }
  @Test
  public void sanitizeSymlinkedWorkingDirectory() throws IOException {
    TemporaryFolder folder = new TemporaryFolder();
    folder.create();

    // Setup up a symlink to our working directory.
    Path symlinkedRoot = folder.getRoot().toPath().resolve("symlinked-root");
    java.nio.file.Files.createSymbolicLink(symlinkedRoot, tmp.getRootPath());

    // Run the build, setting PWD to the above symlink.  Typically, this causes compilers to use
    // the symlinked directory, even though it's not the right project root.
    Map<String, String> envCopy = Maps.newHashMap(System.getenv());
    envCopy.put("PWD", symlinkedRoot.toString());
    workspace
        .runBuckCommandWithEnvironmentAndContext(
            tmp.getRootPath(),
            Optional.<NGContext>absent(),
            Optional.<BuckEventListener>absent(),
            Optional.of(ImmutableMap.copyOf(envCopy)),
            "build",
            "//:simple#default,static")
        .assertSuccess();

    // Verify that we still sanitized this path correctly.
    Path lib = workspace.getPath("buck-out/gen/simple#default,static/libsimple.a");
    String contents = Files.asByteSource(lib.toFile()).asCharSource(Charsets.ISO_8859_1).read();
    assertFalse(lib.toString(), contents.contains(tmp.getRootPath().toString()));
    assertFalse(lib.toString(), contents.contains(symlinkedRoot.toString()));

    folder.delete();
  }
Пример #7
0
 @Test
 public void copyToByteSinkTest() throws Exception {
   File dest = new File("src/test/resources/sampleCompany.pdf");
   dest.deleteOnExit();
   File source = new File("src/main/resources/sample.pdf");
   ByteSource byteSource = Files.asByteSource(source);
   ByteSink byteSink = Files.asByteSink(dest);
   byteSource.copyTo(byteSink);
   assertThat(Files.toByteArray(dest), is(Files.toByteArray(source)));
 }
Пример #8
0
 @RequestMapping(params = "download", method = RequestMethod.GET)
 @ResponseBody
 public void download(
     HttpServletRequest request,
     HttpServletResponse response,
     @ModelAttribute("extension") Extension extension)
     throws IOException {
   File file = new File(extension.getFilePath());
   ByteSource source = Files.asByteSource(file);
   initDownload(request, response, source, file.getName());
 }
Пример #9
0
  @Override
  public int run(String[] args) throws Exception {
    TarArgs tarArgs = new TarArgs();
    new JCommander(tarArgs, args);
    tarArgs.validate();

    if (tarArgs.isExtract()) {
      TarExtractor extractor = new TarExtractor(getConf(), new Path(tarArgs.getArgs().get(0)));
      extractor.extract(Files.asByteSource(new File(tarArgs.getFilename())));
    } else {
      throw new UnsupportedOperationException();
    }
    return 0;
  }
  /**
   * 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);
  }
Пример #11
0
 @Override
 public void run() {
   for (Map.Entry<String, ConfigFileInfo> entry : watchedFileMap.entrySet()) {
     String filePath = entry.getKey();
     ConfigFileInfo configFileInfo = entry.getValue();
     try {
       File file = new File(filePath);
       long lastModified = file.lastModified();
       Preconditions.checkArgument(lastModified > 0L);
       if (lastModified != configFileInfo.lastModifiedTimestampMillis) {
         configFileInfo.lastModifiedTimestampMillis = lastModified;
         ByteSource byteSource = Files.asByteSource(file);
         HashCode newContentHash = byteSource.hash(HASH_FUNCTION);
         if (!newContentHash.equals(configFileInfo.contentHash)) {
           configFileInfo.contentHash = newContentHash;
           LOG.info("File {} was modified at {}, notifying watchers.", filePath, lastModified);
           byte[] newContents = byteSource.read();
           for (Function<byte[], Void> watchers : configFileInfo.changeWatchers) {
             try {
               watchers.apply(newContents);
             } catch (Exception e) {
               LOG.error(
                   "Exception in watcher callback for {}, ignoring. New file contents were: {}",
                   filePath,
                   new String(newContents, Charsets.UTF_8),
                   e);
             }
           }
         } else {
           LOG.info(
               "File {} was modified at {} but content hash is unchanged.",
               filePath,
               lastModified);
         }
       } else {
         LOG.debug("File {} not modified since {}", filePath, lastModified);
       }
     } catch (Exception e) {
       // We catch and log exceptions related to the update of any specific file, but
       // move on so others aren't affected. Issues can happen for example if the watcher
       // races with an external file replace operation; in that case, the next run should
       // pick up the update.
       // TODO: Consider adding a metric to track this so we can alert on failures.
       LOG.error("Config update check failed for {}", filePath, e);
     }
   }
 }
Пример #12
0
  /**
   * 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);
  }
Пример #13
0
  private static Map<String, String> content_for(
      Set<String> relevant_files, Map<String, Vertabrae> file_names) {

    Map<String, String> result = new HashMap<>();
    for (String file : relevant_files) {
      String index = file.substring(file.length() - 1);

      Vertabrae vertabrae = file_names.get(file);
      ByteSource bs = Files.asByteSource(vertabrae.path().toFile());
      String html;
      try {
        html = md.process(bs.openBufferedStream());
        result.put(index, html);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return result;
  }
Пример #14
0
  private static void generate_markup(
      Path abs_path_from, Path abs_path_to, Map<String, Vertabrae> file_names) throws IOException {
    Function<Vertabrae, String> title = Vertabrae::title;

    Map<String, String> toc = Maps.transformValues(file_names, title);

    Set<String> files = file_names.keySet();

    Set<String> filtered_files =
        files.stream().filter(file -> !file.contains("_")).collect(Collectors.toSet());

    for (String key : filtered_files) {
      Vertabrae vertabrae = file_names.get(key);

      ByteSource bs = Files.asByteSource(vertabrae.path().toFile());
      String html = md.process(bs.openBufferedStream());

      Set<String> relevant_files =
          files.stream().filter(file -> file.contains(key + "_")).collect(Collectors.toSet());

      String template = pick_template(abs_path_from, vertabrae.file_name());

      VelocityContext velocity_context = new VelocityContext();
      velocity_context.put("name", vertabrae.title());
      velocity_context.put("content", html);
      velocity_context.put("pages", toc);

      Map<String, String> content_files = content_for(relevant_files, file_names);

      for (String index : content_files.keySet()) {
        velocity_context.put("content_" + index, content_files.get(index));
      }

      System.out.println("[SPINE] Template: " + template);

      String page = merge(template, velocity_context);

      File to_file = new File(abs_path_to + File.separator + vertabrae.file_name() + ".html");

      Files.write(page, to_file, Charsets.UTF_8);
    }
  }
Пример #15
0
 /*
  * (non-Javadoc)
  *
  * @see comeon.commons.Commons#upload(comeon.model.Picture,
  * in.yuvi.http.fluent.ProgressListener)
  */
 @Override
 public void upload(final Picture picture, final ProgressListener listener)
     throws NotLoggedInException, FailedLoginException, FailedUploadException, IOException {
   synchronized (this) {
     if (!this.api.isLoggedIn) {
       LOGGER.debug("Not logged in");
       this.login();
     }
   }
   final InputStream stream = Files.asByteSource(picture.getFile()).openBufferedStream();
   try {
     LOGGER.debug("Uploading");
     final ApiResult result =
         this.api.upload(
             picture.getFile().getName(),
             stream,
             picture.getFile().length(),
             picture.getRenderedTemplate(),
             MessageFormat.format(
                 UI.BUNDLE.getString("upload.comment"), UI.BUNDLE.getString("comeon")),
             true,
             listener);
     final ApiResult error = result.getNode("/api/error");
     if (error.getDocument() != null) {
       final String code = error.getString("@code");
       final String info = error.getString("@info");
       throw new FailedUploadException(code, info);
     }
     final List<ApiResult> warnings = result.getNodes("/api/warning");
     if (warnings != null && !warnings.isEmpty()) {
       for (final ApiResult warning : warnings) {
         final String code = warning.getString("@code");
         final String info = warning.getString("@info");
         LOGGER.warn("Upload warning. MediaWiki says: {}: {}", code, info);
       }
     }
   } catch (final IOException e) {
     throw new FailedUploadException(e);
   }
 }
Пример #16
0
 protected Payload doSlice(File content, long offset, long length) {
   return doSlice(Files.asByteSource(content), offset, length);
 }
Пример #17
0
 public ByteSource getFileSource(final String name) {
   return Files.asByteSource(new File(getString(name)));
 }