本文整理汇总了Java中com.google.common.io.Closer.rethrow方法的典型用法代码示例。如果您正苦于以下问题:Java Closer.rethrow方法的具体用法?Java Closer.rethrow怎么用?Java Closer.rethrow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.io.Closer
的用法示例。
在下文中一共展示了Closer.rethrow方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: zipDirectory
import com.google.common.io.Closer; //导入方法依赖的package包/类
/**
* Zips an entire directory specified by the path.
*
* @param sourceDirectory the directory to read from. This directory and all
* subdirectories will be added to the zip-file. The path within the zip
* file is relative to the directory given as parameter, not absolute.
* @param zipFile the zip-file to write to.
* @throws IOException the zipping failed, e.g. because the input was not
* readable.
*/
static void zipDirectory(
File sourceDirectory,
File zipFile) throws IOException {
checkNotNull(sourceDirectory);
checkNotNull(zipFile);
checkArgument(
sourceDirectory.isDirectory(),
"%s is not a valid directory",
sourceDirectory.getAbsolutePath());
checkArgument(
!zipFile.exists(),
"%s does already exist, files are not being overwritten",
zipFile.getAbsolutePath());
Closer closer = Closer.create();
try {
OutputStream outputStream = closer.register(new BufferedOutputStream(
new FileOutputStream(zipFile)));
zipDirectory(sourceDirectory, outputStream);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例2: writeSelfReferencingJarFile
import com.google.common.io.Closer; //导入方法依赖的package包/类
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();
}
}
示例3: readServiceFile
import com.google.common.io.Closer; //导入方法依赖的package包/类
/**
* Reads the set of service classes from a service file.
*
* @param input not {@code null}. Closed after use.
* @return a not {@code null Set} of service class names.
* @throws IOException
*/
static Set<String> readServiceFile(InputStream input) throws IOException {
HashSet<String> serviceClasses = new HashSet<String>();
Closer closer = Closer.create();
try {
// TODO(gak): use CharStreams
BufferedReader r = closer.register(new BufferedReader(new InputStreamReader(input, Charsets.UTF_8)));
String line;
while ((line = r.readLine()) != null) {
int commentStart = line.indexOf('#');
if (commentStart >= 0) {
line = line.substring(0, commentStart);
}
line = line.trim();
if (!line.isEmpty()) {
serviceClasses.add(line);
}
}
return serviceClasses;
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例4: addLocalResources
import com.google.common.io.Closer; //导入方法依赖的package包/类
/** Returns a URL to a local copy of the given resource, or null. There is
* no filename conflict resolution. */
protected String addLocalResources(URL url) throws IOException {
// Attempt to make local copy
File resourceDir = computeResourceDir();
if (resourceDir != null) {
String base = url.getFile();
base = base.substring(base.lastIndexOf('/') + 1);
mNameToFile.put(base, new File(url.toExternalForm()));
File target = new File(resourceDir, base);
Closer closer = Closer.create();
try {
FileOutputStream output = closer.register(new FileOutputStream(target));
InputStream input = closer.register(url.openStream());
ByteStreams.copy(input, output);
} catch (Throwable e) {
closer.rethrow(e);
} finally {
closer.close();
}
return resourceDir.getName() + '/' + encodeUrl(base);
}
return null;
}
示例5: createRealJarArchive
import com.google.common.io.Closer; //导入方法依赖的package包/类
/**
* We need real Jar contents as this test will actually run Gradle that will peek inside the archive.
*/
@NotNull
private static byte[] createRealJarArchive() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Closer closer = Closer.create();
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream jar = closer.register(new JarOutputStream(buffer, manifest));
try {
jar.putNextEntry(new JarEntry("/dummy.txt"));
jar.write(TOP_LEVEL_BUILD_GRADLE.getBytes());
closer.close();
return buffer.toByteArray();
}
catch (IOException e) {
closer.close();
throw closer.rethrow(e);
}
}
示例6: main
import com.google.common.io.Closer; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
Closer closer = Closer.create();
try {
File destination = new File("src/main/resources/copy.txt");
destination.deleteOnExit();
BufferedReader reader = new BufferedReader(new FileReader("src/main/resources/sampleTextFileOne.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter(destination));
closer.register(reader);
closer.register(writer);
String line;
while((line = reader.readLine())!=null){
writer.write(line);
}
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例7: main
import com.google.common.io.Closer; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("Usage: " + StateStoreCleaner.class.getSimpleName() + " <configuration file>");
System.exit(1);
}
Closer closer = Closer.create();
try {
Properties properties = new Properties();
properties.load(closer.register(new FileInputStream(args[0])));
closer.register(new StateStoreCleaner(properties)).run();
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例8: commitDataset
import com.google.common.io.Closer; //导入方法依赖的package包/类
/**
* Commit the output data of a dataset.
*/
@SuppressWarnings("unchecked")
private void commitDataset(JobState.DatasetState datasetState) throws IOException {
Closer closer = Closer.create();
try {
Class<? extends DataPublisher> dataPublisherClass = (Class<? extends DataPublisher>) Class.forName(
datasetState.getProp(ConfigurationKeys.DATA_PUBLISHER_TYPE, ConfigurationKeys.DEFAULT_DATA_PUBLISHER_TYPE));
DataPublisher publisher = closer.register(DataPublisher.getInstance(dataPublisherClass, datasetState));
publisher.publish(datasetState.getTaskStates());
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
// Set the dataset state to COMMITTED upon successful commit
datasetState.setState(JobState.RunningState.COMMITTED);
}
示例9: testGetWorkUnitsAndExtractor
import com.google.common.io.Closer; //导入方法依赖的package包/类
@Test
public void testGetWorkUnitsAndExtractor() throws IOException, DataRecordException {
HadoopFileInputSource<String, Text, LongWritable, Text> fileInputSource = new TestHadoopFileInputSource();
List<WorkUnit> workUnitList = fileInputSource.getWorkunits(this.sourceState);
Assert.assertEquals(workUnitList.size(), 1);
WorkUnitState workUnitState = new WorkUnitState(workUnitList.get(0));
Closer closer = Closer.create();
try {
HadoopFileInputExtractor<String, Text, LongWritable, Text> extractor =
(HadoopFileInputExtractor<String, Text, LongWritable, Text>) fileInputSource.getExtractor(
workUnitState);
Text text = extractor.readRecord(null);
Assert.assertEquals(text.toString(), TEXT);
Assert.assertNull(extractor.readRecord(null));
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例10: getSecurityTokens
import com.google.common.io.Closer; //导入方法依赖的package包/类
private ByteBuffer getSecurityTokens() throws IOException {
Credentials credentials = UserGroupInformation.getCurrentUser().getCredentials();
Closer closer = Closer.create();
try {
DataOutputBuffer dataOutputBuffer = closer.register(new DataOutputBuffer());
credentials.writeTokenStorageToStream(dataOutputBuffer);
// Remove the AM->RM token so that containers cannot access it
Iterator<Token<?>> tokenIterator = credentials.getAllTokens().iterator();
while (tokenIterator.hasNext()) {
Token<?> token = tokenIterator.next();
if (token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) {
tokenIterator.remove();
}
}
return ByteBuffer.wrap(dataOutputBuffer.getData(), 0, dataOutputBuffer.getLength());
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
示例11: testGenerateDumpScript
import com.google.common.io.Closer; //导入方法依赖的package包/类
@Test
public void testGenerateDumpScript() throws IOException {
Path dumpScript = new Path(TEST_DIR, SCRIPT_NAME);
HeapDumpForTaskUtils.generateDumpScript(dumpScript, this.fs, "test.hprof", "chmod 777 ");
Assert.assertEquals(true, this.fs.exists(dumpScript));
Assert.assertEquals(true, this.fs.exists(new Path(dumpScript.getParent(), "dumps")));
Closer closer = Closer.create();
try {
BufferedReader scriptReader =
closer.register(new BufferedReader(new InputStreamReader(this.fs.open(dumpScript))));
Assert.assertEquals("#!/bin/sh", scriptReader.readLine());
Assert.assertEquals("if [ -n \"$HADOOP_PREFIX\" ]; then", scriptReader.readLine());
Assert.assertEquals(" ${HADOOP_PREFIX}/bin/hadoop dfs -put test.hprof dumpScript/dumps/${PWD//\\//_}.hprof",
scriptReader.readLine());
Assert.assertEquals("else", scriptReader.readLine());
Assert.assertEquals(" ${HADOOP_HOME}/bin/hadoop dfs -put test.hprof dumpScript/dumps/${PWD//\\//_}.hprof",
scriptReader.readLine());
Assert.assertEquals("fi", scriptReader.readLine());
} catch (Throwable t) {
closer.rethrow(t);
} finally {
closer.close();
}
}
示例12: saveProperties
import com.google.common.io.Closer; //导入方法依赖的package包/类
@Override
public void saveProperties(
@NonNull File file,
@NonNull Properties props,
@NonNull String comments) throws IOException {
Closer closer = Closer.create();
try {
OutputStream fos = closer.register(newFileOutputStream(file));
props.store(fos, comments);
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
示例13: generate
import com.google.common.io.Closer; //导入方法依赖的package包/类
/**
* Generates the BuildConfig class.
*/
public void generate() throws IOException {
File pkgFolder = getFolderPath();
if (!pkgFolder.isDirectory()) {
if (!pkgFolder.mkdirs()) {
throw new RuntimeException("Failed to create " + pkgFolder.getAbsolutePath());
}
}
File buildConfigJava = new File(pkgFolder, BUILD_CONFIG_NAME);
Closer closer = Closer.create();
try {
FileOutputStream fos = closer.register(new FileOutputStream(buildConfigJava));
OutputStreamWriter out = closer.register(new OutputStreamWriter(fos, Charsets.UTF_8));
JavaWriter writer = closer.register(new JavaWriter(out));
writer.emitJavadoc("Automatically generated file. DO NOT MODIFY")
.emitPackage(mBuildConfigPackageName)
.beginType("BuildConfig", "class", PUBLIC_FINAL);
for (ClassField field : mFields) {
emitClassField(writer, field);
}
for (Object item : mItems) {
if (item instanceof ClassField) {
emitClassField(writer, (ClassField) item);
} else if (item instanceof String) {
writer.emitSingleLineComment((String) item);
}
}
writer.endType();
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
示例14: zip
import com.google.common.io.Closer; //导入方法依赖的package包/类
public static void zip(FileObject file, OutputStream out) throws IOException {
Closer closer = Closer.create();
try {
closer.register(out);
InputStream in = file.getContent().getInputStream();
closer.register(in);
ByteStreams.copy(in, out);
} catch (IOException ioe) {
throw closer.rethrow(ioe);
} finally {
closer.close();
}
}
示例15: parseSchema
import com.google.common.io.Closer; //导入方法依赖的package包/类
private Schema parseSchema(String schemaFile) throws IOException {
Closer closer = Closer.create();
try {
InputStream in = closer.register(getClass().getResourceAsStream(schemaFile));
return new Schema.Parser().parse(in);
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}