com.google.common.io.FileWriteMode Java Examples

The following examples show how to use com.google.common.io.FileWriteMode. 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: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuleHasCustomRequirements() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(
          String.format(
              "{%s: %s, %s: %s}", OTHER_RULE_ACTION_TAGS, SIZE_SMALL, ACTION_TAGS, SIZE_LARGE));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(WorkerSize.LARGE, workerRequirements.getWorkerSize());
}
 
Example #2
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuleHasCustomRequirementsButOnlyWithoutAuxiliaryTag() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(
          String.format(
              "{%s: %s, %s: %s}",
              OTHER_RULE_ACTION_TAGS, SIZE_SMALL, ACTION_TAGS_NO_AUX_TAG, SIZE_LARGE));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(WorkerSize.LARGE, workerRequirements.getWorkerSize());
}
 
Example #3
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuleHasInvalidTypeShouldProvideDefault() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(
          String.format(
              "{%s: %s, %s: %s}", OTHER_RULE_ACTION_TAGS, SIZE_SMALL, ACTION_TAGS, SIZE_UNKNOWN));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, NO_AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(
      FileBasedWorkerRequirementsProvider.RETRY_ON_OOM_DEFAULT, workerRequirements);
}
 
Example #4
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuleHasCustomRequirementsInOldFormat() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(
          String.format(
              "{\"%s\": %s, \"%s\": %s}", OTHER_SHORT_NAME, SIZE_SMALL, SHORT_NAME, SIZE_LARGE));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, NO_AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(WorkerSize.LARGE, workerRequirements.getWorkerSize());
}
 
Example #5
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void testRuleHasInvalidTypeShouldProvideDefaultInOldFormat() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(
          String.format(
              "{\"%s\": %s, \"%s\": %s}",
              OTHER_SHORT_NAME, SIZE_SMALL, SHORT_NAME, SIZE_UNKNOWN));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, NO_AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(
      FileBasedWorkerRequirementsProvider.RETRY_ON_OOM_DEFAULT, workerRequirements);
}
 
Example #6
Source File: BatchFileAppender.java    From qmq with Apache License 2.0 5 votes vote down vote up
BatchFileAppender(final File file, final int capacity) {
    this.sink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
    this.pendingWrite = new ArrayBlockingQueue<>(capacity);
    this.batchWriteThread = new Thread(new Runnable() {
        @Override
        public void run() {
            batchWriteLoop();
        }
    });
    this.stop = false;
    this.batchWriteThread.start();
}
 
Example #7
Source File: FileUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 以UTF-8格式向文件写入字符串,需指定是否覆盖:true为不覆盖,false为覆盖
 * @author Frodez
 * @date 2019-03-09
 */
@SneakyThrows
public static void writeString(String content, File file, boolean isAppend) {
	if (isAppend) {
		Files.asCharSink(file, DefCharset.UTF_8_CHARSET, FileWriteMode.APPEND).write(content);
	} else {
		Files.asCharSink(file, DefCharset.UTF_8_CHARSET).write(content);
	}
}
 
Example #8
Source File: FileUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 以UTF-8格式向文件写入字符串,需指定Charset,需指定是否覆盖:true为不覆盖,false为覆盖
 * @author Frodez
 * @date 2019-03-09
 */
@SneakyThrows
public static void writeString(String content, File file, Charset charset, boolean isAppend) {
	if (isAppend) {
		Files.asCharSink(file, charset, FileWriteMode.APPEND).write(content);
	} else {
		Files.asCharSink(file, charset).write(content);
	}
}
 
Example #9
Source File: FileUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 向文件写入byte数组,需指定是否覆盖:true为不覆盖,false为覆盖
 * @author Frodez
 * @date 2019-03-09
 */
@SneakyThrows
public static void writeBytes(byte[] content, File file, boolean isAppend) {
	if (isAppend) {
		Files.asByteSink(file, FileWriteMode.APPEND).write(content);
	} else {
		Files.asByteSink(file).write(content);
	}
}
 
Example #10
Source File: FileUtil.java    From BlogManagePlatform with Apache License 2.0 5 votes vote down vote up
/**
 * 将输入流输入数据转入文件中,需指定是否覆盖:true为不覆盖,false为覆盖
 * @author Frodez
 * @date 2019-03-09
 */
@SneakyThrows
public static void transfer(InputStream input, File file, boolean isAppend) {
	if (isAppend) {
		Files.asByteSink(file, FileWriteMode.APPEND).writeFrom(input);
	} else {
		Files.asByteSink(file).writeFrom(input);
	}
}
 
Example #11
Source File: SubprocessCommunicator.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Spawns a process and communicates with it.
 *
 * 1. Spawn the subprocess
 * 2. Write any input to its stream and flush it.
 * 3. Close the input stream.
 * 4. Read the stderr / stdout streams into memory if their are processors.
 * 5. Wait for the subprocess
 * 6. Process the stdout/stderr thru the callers line processors.
 * 7. return the subprocess exit code.
 *
 * Because lineprocessors are stateful, it only makes sense to allow the communicator to be called
 * once.
 *
 * @return the process exit code
 * @throws IllegalStateException when called more then once
 * @throws RuntimeException when process runs passed timeout or when it is interrupted.
 */
public int communicate() {
  checkState(communicatorCalled.compareAndSet(false, true), "Already called!");
  List<String> executorArgs = Lists.newArrayList(executorLocation, logFile.getPath());
  executorArgs.addAll(arguments);

  Command command =
      new Command(
          executorArgs.toArray(new String[0]), environment, null /* use current working dir*/);

  Stopwatch startTime = Stopwatch.createStarted();
  int returnCode = innerCommunicate(command);
  long elapsed = startTime.elapsed(TimeUnit.MILLISECONDS);
  boolean success = true;
  try {
    Files.asCharSink(logFile, Charsets.UTF_8, FileWriteMode.APPEND)
        .write(String.format("EXIT CODE: %s\n", returnCode));
    // Also encode exit status in log file name to help debugging.
    String newSuffix = "ok.txt";
    if (returnCode != 0) {
      success = false;
      newSuffix = "fail.txt";
    }
    File newName = new File(logFile.getPath().replaceAll("txt$", newSuffix));
    if (!logFile.getPath().equals(newName.getPath())) {
      if (logFile.renameTo(newName)) {
        logFile = newName;
      }
    }
  } catch (IOException ignore) {
    /* lumber on and let the caller handle the return */
  } finally {
    adbReporter.report(
        String.format("%s:%s", EXEC_NAMESPACE, commandBasename),
        Joiner.on(" ").join(arguments),
        elapsed,
        success);
  }
  return returnCode;
}
 
Example #12
Source File: JavaReaderToXUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenUsingGuava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
    final Reader initialReader = new StringReader("Some text");

    final File targetFile = new File("src/test/resources/targetFile.txt");
    com.google.common.io.Files.touch(targetFile);
    final CharSink charSink = com.google.common.io.Files.asCharSink(targetFile, Charset.defaultCharset(), FileWriteMode.APPEND);
    charSink.writeFrom(initialReader);
    initialReader.close();
}
 
Example #13
Source File: AppendToFileManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
    File file = new File(fileName);
    CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
    chs.write("Spain\r\n");

    assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
 
Example #14
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidJSONShouldReturnDefault() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND).write("invalid json");

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, NO_AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(
      FileBasedWorkerRequirementsProvider.RETRY_ON_OOM_DEFAULT, workerRequirements);
}
 
Example #15
Source File: FileBasedWorkerRequirementsProviderTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testRuleNotFoundShouldReturnDefault() throws IOException {
  File file = Paths.get(tmp.getPath(), FILENAME).toFile();
  Assert.assertTrue(file.createNewFile());
  Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND)
      .write(String.format("{%s: %s}", OTHER_RULE_ACTION_TAGS, SIZE_SMALL));

  WorkerRequirementsProvider provider =
      new FileBasedWorkerRequirementsProvider(projectFilesystem, FILENAME, true, 1000);
  WorkerRequirements workerRequirements =
      provider.resolveRequirements(buildTarget, NO_AUXILIARY_BUILD_TAG);
  Assert.assertNotNull(workerRequirements);
  Assert.assertEquals(
      FileBasedWorkerRequirementsProvider.RETRY_ON_OOM_DEFAULT, workerRequirements);
}