com.google.common.io.Resources.copy()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中com.google.common.io.Resources.copy方法的一些代码示例,展示了Resources.copy的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Resources.copy方法的具体详情如下:
包路径:com.google.common.io.Resources
类名称:Resources
方法名:copy

Resources.copy介绍

[英]Copies all bytes from a URL to an output stream.
[中]将URL中的所有字节复制到输出流。

代码示例

代码示例来源:origin: google/guava

public void testCopyToOutputStream() throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 URL resource = getClass().getResource("testdata/i18n.txt");
 Resources.copy(resource, out);
 assertEquals(I18N, out.toString("UTF-8"));
}

代码示例来源:origin: apache/incubator-gobblin

/**
  * Get ivy settings file from classpath
  */
 public static File getIvySettingsFile() throws IOException {
  URL settingsUrl = Thread.currentThread().getContextClassLoader().getResource(IVY_SETTINGS_FILE_NAME);
  if (settingsUrl == null) {
   throw new IOException("Failed to find " + IVY_SETTINGS_FILE_NAME + " from class path");
  }

  // Check if settingsUrl is file on classpath
  File ivySettingsFile = new File(settingsUrl.getFile());
  if (ivySettingsFile.exists()) {
   // can access settingsUrl as a file
   return ivySettingsFile;
  }

  // Create temporary Ivy settings file.
  ivySettingsFile = File.createTempFile("ivy.settings", ".xml");
  ivySettingsFile.deleteOnExit();

  try (OutputStream os = new BufferedOutputStream(new FileOutputStream(ivySettingsFile))) {
   Resources.copy(settingsUrl, os);
  }

  return ivySettingsFile;
 }
}

代码示例来源:origin: google/guava

private static void writeSelfReferencingJarFile(File jarFile, String... entries)
  throws IOException {
 Manifest manifest = new Manifest();
 // Without version, the manifest is silently ignored. Ugh!
 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
 manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName());
 Closer closer = Closer.create();
 try {
  FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
  JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut));
  for (String entry : entries) {
   jarOut.putNextEntry(new ZipEntry(entry));
   Resources.copy(ClassPathTest.class.getResource(entry), jarOut);
   jarOut.closeEntry();
  }
 } catch (Throwable e) {
  throw closer.rethrow(e);
 } finally {
  closer.close();
 }
}

代码示例来源:origin: org.locationtech.geogig/geogig-core

private void copyHookFile(String folder, String file) throws IOException {
  URL url = Resources.getResource(Hookables.class, file);
  OutputStream os = new FileOutputStream(new File(folder, file).getAbsolutePath());
  Resources.copy(url, os);
  os.close();
}

代码示例来源:origin: apache/sentry

public static void copyToDir(File dest, String... resources)
  throws FileNotFoundException, IOException {
 for(String resource : resources) {
  LOGGER.debug("Copying " + resource + " to " + dest);
  try (OutputStream output = Files.newOutputStream(new File(dest, resource).toPath())) {
   Resources.copy(Resources.getResource(resource), output);
  }
 }
}

代码示例来源:origin: apache/incubator-sentry

public static void copyToDir(File dest, String... resources)
  throws FileNotFoundException, IOException {
 for(String resource : resources) {
  LOGGER.debug("Copying " + resource + " to " + dest);
  Resources.copy(Resources.getResource(resource), new FileOutputStream(new File(dest, resource)));
 }
}

代码示例来源:origin: org.doctester/doctester-core

private void copyResourceIfItExists(String resource, String targetFileForResource) {
  try {
    URL url = this.getClass().getClassLoader().getResource(
        resource);
    if (url == null) {
      logger.info("Did not find resource {}.", resource);
      return;
    }
    File targetFile = new File(targetFileForResource);
    Files.createParentDirs(targetFile);
    Resources.copy(url, new FileOutputStream(targetFile));
  } catch (IOException ex) {
    logger.error("Something went wrong copying resource {}", resource, ex);
  }
}

代码示例来源:origin: org.eclipse.neoscada.ide/org.eclipse.scada.configuration.world.lib

private void createLogback ( final Element comp, final EquinoxAppService eas, final File resourceBase ) throws Exception
{
  final Element file = createElement ( comp, "File" ); //$NON-NLS-1$
  final String serviceName = makeServiceName ( eas );
  file.setAttribute ( "Id", "logback.xml_" + serviceName ); //$NON-NLS-1$ //$NON-NLS-2$
  file.setAttribute ( "Source", String.format ( "resources\\apps\\%s\\logback.xml", eas.getName () ) ); //$NON-NLS-1$ //$NON-NLS-2$
  final File logback = new File ( resourceBase, "logback.xml" ); //$NON-NLS-1$
  try ( FileOutputStream out = new FileOutputStream ( logback ) )
  {
    Resources.copy ( Resources.getResource ( MsiHandler.class, "templates/msi/app.logback.xml" ), out ); //$NON-NLS-1$
  }
}

代码示例来源:origin: com.walmartlabs.concord.server/concord-server

@Override
public void fetch(String repoUrl, String branch, String commitId, Secret secret, Path dst) {
  URL resUrl = Resources.getResource(normalizeUrl(repoUrl));
  try {
    if (!Files.exists(dst)) {
      Files.createDirectories(dst);
    }
    String fileName = repoUrl.substring(repoUrl.lastIndexOf('/') + 1);
    Path dstFile = dst.resolve(fileName);
    try (OutputStream out = Files.newOutputStream(dstFile, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
      Resources.copy(resUrl, out);
    }
  } catch (IOException e) {
    throw new RepositoryException("Error while fetching a repository", e);
  }
}

代码示例来源:origin: de.dentrassi.eclipse.neoscada.ide/org.eclipse.scada.configuration.world.lib

private void createLogback ( final Element comp, final EquinoxAppService eas, final File resourceBase ) throws Exception
{
  final Element file = createElement ( comp, "File" ); //$NON-NLS-1$
  final String serviceName = makeServiceName ( eas );
  file.setAttribute ( "Id", "logback.xml_" + serviceName ); //$NON-NLS-1$ //$NON-NLS-2$
  file.setAttribute ( "Source", String.format ( "resources\\apps\\%s\\logback.xml", eas.getName () ) ); //$NON-NLS-1$ //$NON-NLS-2$
  final File logback = new File ( resourceBase, "logback.xml" ); //$NON-NLS-1$
  try ( FileOutputStream out = new FileOutputStream ( logback ) )
  {
    Resources.copy ( Resources.getResource ( MsiHandler.class, "templates/msi/app.logback.xml" ), out ); //$NON-NLS-1$
  }
}

代码示例来源:origin: org.asciidoctor/asciidoclet

public boolean copy() {
  if (!docletOptions.destDir().isPresent()) {
    // standard doclet must have checked this by the time we are called
    errorReporter.printError("Destination directory not specified, cannot copy stylesheet");
    return false;
  }
  String stylesheet = selectStylesheet(System.getProperty("java.version"));
  File destDir = docletOptions.destDir().get();
  try {
    Resources.copy(Resources.getResource(stylesheet), new FileOutputStream(new File(destDir, OUTPUT_STYLESHEET)));
    Resources.copy(Resources.getResource(CODERAY_STYLESHEET), new FileOutputStream(new File(destDir, CODERAY_STYLESHEET)));
    return true;
  } catch (IOException e) {
    errorReporter.printError(e.getLocalizedMessage());
    return false;
  }
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-iphone-driver

protected static String getIphoneSimPath() {
 String filename = "ios-sim";
 File parentDir = TemporaryFilesystem.getDefaultTmpFS().createTempDir("webdriver", "libs");
 try {
  File destination = new File(parentDir, filename);
  FileOutputStream outputStream = new FileOutputStream(destination);
  try {
   URL resource = Resources.getResource(IPhoneSimulatorBinary.class.getPackage().getName().replace('.', '/') + '/' + filename);
   Resources.copy(resource, outputStream);
   FileHandler.makeExecutable(destination);
   return destination.getAbsolutePath();
  } finally {
   outputStream.close();
  }
 } catch (IOException e) {
  throw new WebDriverException(e);
 }
}

代码示例来源:origin: org.kill-bill.billing/killbill-osgi

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
  final URL url = findResourceURL(req);
  if (url != null) {
    Resources.copy(url, resp.getOutputStream());
    resp.setStatus(200);
    return;
  }
  // If we can't find it, the container might
  final RequestDispatcher rd = getServletContext().getNamedDispatcher("default");
  final HttpServletRequest wrapped = new HttpServletRequestWrapper(req) {
    public String getServletPath() { return ""; }
  };
  rd.forward(wrapped, resp);
}

代码示例来源:origin: org.seleniumhq.selenium/selenium-firefox-driver

public void writeTo(File extensionsDir) throws IOException {
  if (!FileHandler.isZipped(loadFrom)) {
   throw new WebDriverException("Will only install zipped extensions for now");
  }

  File holdingPen = new File(extensionsDir, "webdriver-staging");
  FileHandler.createDir(holdingPen);

  File extractedXpi = new File(holdingPen, loadFrom);
  File parentDir = extractedXpi.getParentFile();
  if (!parentDir.exists()) {
   parentDir.mkdirs();
  }

  URL resourceUrl = Resources.getResource(loadResourcesUsing, loadFrom);

  try (OutputStream stream = new FileOutputStream(extractedXpi)){
   Resources.copy(resourceUrl, stream);
  }
  new FileExtension(extractedXpi).writeTo(extensionsDir);
 }
}

代码示例来源:origin: com.google.guava/guava-tests

public void testCopyToOutputStream() throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 URL resource = getClass().getResource("testdata/i18n.txt");
 Resources.copy(resource, out);
 assertEquals(I18N, out.toString("UTF-8"));
}

代码示例来源:origin: org.geoserver.community/gs-geogig

static void copySampleInitSript(Resource configDirectory, String scriptName)
    throws IOException {
  Resource resource = configDirectory.get(scriptName);
  if (!resource.getType().equals(Resource.Type.UNDEFINED)) {
    return;
  }
  try (OutputStream out = resource.out()) {
    Resources.copy(LogStoreInitializer.class.getResource(scriptName), out);
  }
}

代码示例来源:origin: palantir/gradle-baseline

private static void copyTestFile(String source, TemporaryFolder root, String target) {
  File targetFile = new File(root.getRoot(), target);
  targetFile.getParentFile().mkdirs();
  try (OutputStream stream = new FileOutputStream(targetFile)) {
    Resources.copy(Resources.getResource(BaselineCircleCiJavaIntegrationTests.class, source), stream);
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}

代码示例来源:origin: palantir/gradle-circle-style

public static File copyTestFile(String source, TemporaryFolder root, String target) {
  File targetFile = new File(root.getRoot(), target);
  targetFile.getParentFile().mkdirs();
  try (OutputStream stream = new FileOutputStream(targetFile)) {
    Resources.copy(WhoCalled.$.getCallingClass().getResource(source), stream);
    return targetFile;
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}

代码示例来源:origin: com.google.guava/guava-tests

private static void writeSelfReferencingJarFile(File jarFile, String... entries)
  throws IOException {
 Manifest manifest = new Manifest();
 // Without version, the manifest is silently ignored. Ugh!
 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
 manifest.getMainAttributes().put(Attributes.Name.CLASS_PATH, jarFile.getName());
 Closer closer = Closer.create();
 try {
  FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
  JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut));
  for (String entry : entries) {
   jarOut.putNextEntry(new ZipEntry(entry));
   Resources.copy(ClassPathTest.class.getResource(entry), jarOut);
   jarOut.closeEntry();
  }
 } catch (Throwable e) {
  throw closer.rethrow(e);
 } finally {
  closer.close();
 }
}

代码示例来源:origin: v-ladynev/fluent-hibernate

private static void writeJarFile(File jarFile, Class<?>... classes) throws IOException {
  Manifest manifest = new Manifest();
  manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
  Closer closer = Closer.create();
  try {
    FileOutputStream fileOut = closer.register(new FileOutputStream(jarFile));
    JarOutputStream jarOut = closer.register(new JarOutputStream(fileOut, manifest));
    for (Class<?> clazz : classes) {
      String classResource = ResourceUtils.classAsResource(clazz);
      jarOut.putNextEntry(new ZipEntry(classResource));
      Resources.copy(ScannerTestUtils.class.getResource(ResourceUtils
          .resourcePathFromRoot(classResource)), jarOut);
      jarOut.closeEntry();
    }
  } catch (Throwable e) {
    throw closer.rethrow(e);
  } finally {
    closer.close();
  }
}

相关文章