예제 #1
0
 public static void persistentInFile(String fileDir, String fileName, List<String> lines) {
   File file = new File(fileDir, fileName);
   FileWriter fileWriter = null;
   try {
     fileWriter = new FileWriter(file, true);
     IOUtils.writeLines(lines, null, fileWriter);
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     IOUtils.closeQuietly(fileWriter);
   }
 }
 @SuppressWarnings("unchecked")
 private static void accessProperty(
     InputStream input,
     OutputStream output,
     String[] keys,
     PropertyOperation operation,
     boolean autoclose)
     throws IOException {
   List<String> lines = new ArrayList<String>();
   List<String> result = new ArrayList<String>();
   try {
     lines = IOUtils.readLines(input);
   } finally {
     if (autoclose) {
       IOUtils.closeQuietly(input);
     }
   }
   try {
     for (String line : lines) {
       int offset = line.indexOf("=");
       if (line.trim().length() > 0 && offset > -1) {
         String originalKey = line.substring(0, offset).trim();
         String originalValue = line.substring(++offset);
         int index = ArrayUtils.indexOf(keys, originalKey);
         if (index > -1) {
           String outputLine = operation.process(line, originalKey, originalValue);
           if (outputLine != null) {
             result.add(outputLine);
           }
         } else {
           result.add(line);
         }
       } else {
         result.add(line);
       }
     }
     if (output != null) {
       IOUtils.writeLines(result, null, output);
     }
   } finally {
     if (autoclose) {
       IOUtils.closeQuietly(output);
     }
   }
 }
예제 #3
0
  /**
   * Gathers all the inputs buffered by calls to {@link #addInput(String)} or {@link
   * #addInput(String...)} and writes them to the input directory, in preparation for running the
   * MapReduce job.
   *
   * @return a reference to this object
   * @throws IOException if something goes wrong
   */
  public TextIOJobBuilder writeInputs() throws IOException {

    if (fs.exists(outputPath)) {
      fs.delete(outputPath, true);
    }
    if (fs.exists(inputPath)) {
      fs.delete(inputPath, true);
    }
    fs.mkdirs(inputPath);

    DataOutputStream stream = fs.create(new Path(inputPath, "part-0"));

    IOUtils.writeLines(inputs, String.format("%n"), stream);

    stream.close();

    return this;
  }
예제 #4
0
  /* (non-Javadoc)
   * @see org.apache.maven.plugin.AbstractMojo#execute()
   */
  public void execute() throws MojoExecutionException, MojoFailureException {

    File setup = new File("src/main/python/setup.py");

    try {

      if (version != null) {
        version = projectVersion;
      }

      // update VERSION to latest version
      List<String> lines = IOUtils.readLines(new BufferedInputStream(new FileInputStream(setup)));
      for (String line : lines) {
        if (line.contains(VERSION)) {
          line = line.replace(VERSION, version);
        }
      }
      IOUtils.writeLines(lines, "\n", new BufferedOutputStream(new FileOutputStream(setup)));

      // execute setup script
      ProcessBuilder t = new ProcessBuilder("python", "setup.py", "bdist_egg");
      t.directory(new File("src/test/python"));
      t.redirectErrorStream(true);

      Process pr = t.start();
      int exitCode = pr.waitFor();
      BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
      String line = "";
      while ((line = buf.readLine()) != null) {
        getLog().info(line);
      }

      if (exitCode != 0) {
        throw new MojoExecutionException("python setup.py returned error code " + exitCode);
      }

    } catch (FileNotFoundException e) {
      throw new MojoExecutionException("Unable to find " + setup.getPath(), e);
    } catch (IOException e) {
      throw new MojoExecutionException("Unable to read " + setup.getPath(), e);
    } catch (InterruptedException e) {
      throw new MojoExecutionException("Unable to execute python " + setup.getPath(), e);
    }
  }
 /**
  * Adds the property.
  *
  * @param input the input
  * @param output the output
  * @param properties the properties
  * @throws IOException Signals that an I/O exception has occurred.
  */
 @SuppressWarnings("unchecked")
 public static void addProperty(
     InputStream input, OutputStream output, Map<String, String> properties) throws IOException {
   List<String> sources = new ArrayList<String>();
   try {
     sources = IOUtils.readLines(input);
   } finally {
     IOUtils.closeQuietly(input);
   }
   try {
     for (String key : properties.keySet()) {
       String value = properties.get(key);
       sources.add(key + "=" + value);
     }
     IOUtils.writeLines(sources, null, output);
   } finally {
     IOUtils.closeQuietly(output);
   }
 }
예제 #6
0
 private void writeRpmFileList(Collection<String> files, File rpmListOutputFile)
     throws IOException {
   FileOutputStream outputStream = new FileOutputStream(rpmListOutputFile);
   try {
     writeLines(files, "\n", outputStream);
     if (files.isEmpty()) {
       LOG.debug(
           "Write non existing package to rpm list file {} to avoid an empty packge list that would cause createrepo to scan the whole directory",
           rpmListOutputFile);
       write(
           ".foo/.bar.rpm/to-avoid-an-empty-rpm-list-file/that-would-cause-createrepo-to-scan-the-whole-repo.rpm",
           outputStream);
     } else {
       LOG.debug("Wrote {} rpm packages to rpm list file {} .", files.size(), rpmListOutputFile);
     }
   } finally {
     outputStream.close();
   }
 }
예제 #7
0
파일: IOUtils.java 프로젝트: snooplsm/teav
 /**
  * Writes the <code>toString()</code> value of each item in a collection to an <code>OutputStream
  * </code> line by line, using the specified character encoding and the specified line ending.
  *
  * <p>Character encoding names can be found at <a
  * href="http://www.iana.org/assignments/character-sets">IANA</a>.
  *
  * @param lines the lines to write, null entries produce blank lines
  * @param lineEnding the line separator to use, null is system default
  * @param output the <code>OutputStream</code> to write to, not null, not closed
  * @param encoding the encoding to use, null means platform default
  * @throws NullPointerException if the output is null
  * @throws IOException if an I/O error occurs
  * @since Commons IO 1.1
  */
 public static void writeLines(
     Collection lines, String lineEnding, OutputStream output, String encoding)
     throws IOException {
   if (encoding == null) {
     writeLines(lines, lineEnding, output);
   } else {
     if (lines == null) {
       return;
     }
     if (lineEnding == null) {
       lineEnding = LINE_SEPARATOR;
     }
     for (Iterator it = lines.iterator(); it.hasNext(); ) {
       Object line = it.next();
       if (line != null) {
         output.write(line.toString().getBytes(encoding));
       }
       output.write(lineEnding.getBytes(encoding));
     }
   }
 }
예제 #8
0
  public static void writeLines(Collection var0, String var1, OutputStream var2, String var3)
      throws IOException {
    if (var3 == null) {
      writeLines(var0, var1, var2);
    } else if (var0 != null) {
      if (var1 == null) {
        var1 = LINE_SEPARATOR;
      }

      Iterator var4 = var0.iterator();

      while (var4.hasNext()) {
        Object var5 = var4.next();
        if (var5 != null) {
          byte[] var6 = var5.toString().getBytes(var3);
          var2.write(var6);
        }

        byte[] var7 = var1.getBytes(var3);
        var2.write(var7);
      }
    }
  }