Package com.google.common.io

Examples of com.google.common.io.Closer


  /**
   * Create a temporary ANT file for executing JUnit4 ANT task.
   */
  private File createTemporaryAntFile(Document doc) throws IOException {
    Closer closer = Closer.create();
    try {
      File antFile = File.createTempFile("junit4-ant-", ".xml", dir);
      OutputStream os = closer.register(new FileOutputStream(antFile));
      XMLWriter xmlWriter = new XMLWriter(os, OutputFormat.createPrettyPrint());
      xmlWriter.write(doc);
      return antFile;
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here


  public PrivateKey getPrivateKey(String alias) {
    return privateKey;
  }

  private static KeyStore loadKeyStore(InputSupplier<InputStream> inputStreamSupplier) throws IOException {
    Closer closer = Closer.create();
    try {
      InputStream inputStream = closer.register(inputStreamSupplier.getInput());
      KeyStore keyStore = KeyStore.getInstance("JKS");
      keyStore.load(inputStream, KEYSTORE_PASSWORD);
      return keyStore;
    } catch (Throwable e) {
      throw closer.rethrow(e);
    } finally {
      closer.close();
    }
  }
View Full Code Here

            if (sink == null) {
                throw new IOException("Cannot persist unbound object: " + object);
            }
        }

        Closer closer = Closer.create();
        try {
            OutputStream os = closer.register(sink.openBufferedStream());
            mapper.writeValue(os, object);
        } finally {
            closer.close();
        }
    }
View Full Code Here

     * @param <V> the type of class
     * @return an object
     */
    public static <V> V read(ByteSource source, Class<V> cls, boolean returnNull) {
        V object;
        Closer closer = Closer.create();

        try {
            object = mapper.readValue(closer.register(source.openBufferedStream()), cls);
        } catch (IOException e) {
            if (!(e instanceof FileNotFoundException)) {
                log.log(Level.INFO, "Failed to load" + cls.getCanonicalName(), e);
            }

            if (returnNull) {
                return null;
            }

            try {
                object = cls.newInstance();
            } catch (InstantiationException e1) {
                throw new RuntimeException(
                        "Failed to construct object with no-arg constructor", e1);
            } catch (IllegalAccessException e1) {
                throw new RuntimeException(
                        "Failed to construct object with no-arg constructor", e1);
            }
        } finally {
            try {
                closer.close();
            } catch (IOException e) {
            }
        }

        return object;
View Full Code Here

            throw new InterruptedException();
        }
    }

    public static Properties loadProperties(Class<?> clazz, String name, String extraProperty) throws IOException {
        Closer closer = Closer.create();
        Properties prop = new Properties();
        try {
            InputStream in = closer.register(clazz.getResourceAsStream(name));
            prop.load(in);
            String extraPath = System.getProperty(extraProperty);
            if (extraPath != null) {
                log.info("Loading extra properties for " +
                        clazz.getCanonicalName() + ":" + name + " from " + extraPath + "...");
                in = closer.register(new BufferedInputStream(closer.register(new FileInputStream(extraPath))));
                prop.load(in);
            }
        } finally {
            closer.close();
        }

        return prop;
    }
View Full Code Here

        this.destination = destination;
    }

    @Override
    public void run() {
        Closer closer = Closer.create();

        try {
            InputStream is = closer.register(source.openBufferedStream());
            ZipInputStream zis = closer.register(new ZipInputStream(is));
            ZipEntry entry;

            destination.getParentFile().mkdirs();

            while ((entry = zis.getNextEntry()) != null) {
                if (matches(entry)) {
                    File file = new File(getDestination(), entry.getName());
                    writeEntry(zis, file);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                closer.close();
            } catch (IOException e) {
            }
        }
    }
View Full Code Here

  public OPFData peek()
    throws InvalidVersionException,
    IOException
  {
    Closer closer = Closer.create();
    try
    {
      InputStream in = resourceProvider.getInputStream(path);
      if (in == null)
        throw new IOException("Couldn't find resource " + path);
      in = closer.register(resourceProvider.getInputStream(path));
      return peek(in);
    } catch (Throwable e)
    {
      throw closer.rethrow(e, InvalidVersionException.class);
    } finally
    {
      closer.close();
    }
  }
View Full Code Here

    return table;
  }

  private boolean loadWorkspaceSettings() {
    boolean success;
    Closer closer = Closer.create();
    try {
      try {
        final FileInputStream loadSource = closer.register(new FileInputStream(WORKSPACE_XML_FILENAME));
        MainWindow.this.toolWindowManager.getPersistenceDelegate().merge(loadSource, MergePolicy.RESET);
        LOGGER.info("Successfull loaded workspace settings.");
        success = true;
      } catch (Throwable ex) {
        success = false;
        closer.rethrow(ex);
      } finally {
        closer.close();
      }
    } catch (IOException ex) {
      LOGGER.error("Exeption thrown while loading workspace settings.", ex);
      success = false;
    }
View Full Code Here

    return success;
  }

  private boolean saveWorskapceSettings() {
    boolean success;
    Closer closer = Closer.create();
    try {
      try {
        final FileOutputStream saveDestination = closer.register(new FileOutputStream(WORKSPACE_XML_FILENAME));
        MainWindow.this.toolWindowManager.getPersistenceDelegate().save(saveDestination);
        LOGGER.info("Successfull saved workspace settings.");
        success = true;
      } catch (Throwable ex) {
        closer.rethrow(ex);
        success = false;
      } finally {
        closer.close();
      }
    } catch (IOException ex) {
      LOGGER.error("Exeption thrown while saving workspace settings.", ex);
      success = false;
    }
View Full Code Here

  public void run() {
    try {
      LOG.info("Exec " + mCommand + " output to " + mFilePath);
      Process p = java.lang.Runtime.getRuntime().exec(mCommand);
      String line;
      Closer closer = Closer.create();
      try {
        BufferedReader bri =
            closer.register(new BufferedReader(new InputStreamReader(p.getInputStream())));
        BufferedReader bre =
            closer.register(new BufferedReader(new InputStreamReader(p.getErrorStream())));
        File file = new File(mFilePath);
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = closer.register(new BufferedWriter(fw));
        while ((line = bri.readLine()) != null) {
          bw.write(line + "\n");
        }
        while ((line = bre.readLine()) != null) {
          bw.write(line + "\n");
        }
        bw.flush();
      } finally {
        closer.close();
      }

      p.waitFor();
      LOG.info("Exec " + mCommand + " output to " + mFilePath + " done.");
    } catch (IOException e) {
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.