本文整理汇总了Java中com.google.common.io.Closeables.closeQuietly方法的典型用法代码示例。如果您正苦于以下问题:Java Closeables.closeQuietly方法的具体用法?Java Closeables.closeQuietly怎么用?Java Closeables.closeQuietly使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.io.Closeables
的用法示例。
在下文中一共展示了Closeables.closeQuietly方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readFromResource
import com.google.common.io.Closeables; //导入方法依赖的package包/类
public static String readFromResource(String resource) throws IOException {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
in = Utils.class.getResourceAsStream(resource);
}
if (in == null) {
return null;
}
String text = Utils.read(in);
return text;
} finally {
Closeables.closeQuietly(in);
}
}
示例2: findClassInJarFile
import com.google.common.io.Closeables; //导入方法依赖的package包/类
protected Class<?> findClassInJarFile(String qualifiedClassName) throws ClassNotFoundException {
URI classUri = URIUtil.buildUri(StandardLocation.CLASS_OUTPUT, qualifiedClassName);
String internalClassName = classUri.getPath().substring(1);
JarFile jarFile = null;
try {
for (int i = 0; i < jarFiles.size(); i++) {
jarFile = jarFiles.get(i);
JarEntry jarEntry = jarFile.getJarEntry(internalClassName);
if (jarEntry != null) {
InputStream inputStream = jarFile.getInputStream(jarEntry);
try {
byte[] byteCode = new byte[(int) jarEntry.getSize()];
ByteStreams.read(inputStream, byteCode, 0, byteCode.length);
return defineClass(qualifiedClassName, byteCode, 0, byteCode.length);
} finally {
Closeables.closeQuietly(inputStream);
}
}
}
} catch (IOException e) {
throw new IllegalStateException(String.format("Failed to lookup class %s in jar file %s", qualifiedClassName, jarFile), e);
}
return null;
}
示例3: getTable
import com.google.common.io.Closeables; //导入方法依赖的package包/类
/**
* @see org.alfasoftware.morf.metadata.Schema#getTable(java.lang.String)
*/
@Override
public Table getTable(String name) {
// Read the meta data for the specified table
InputStream inputStream = xmlStreamProvider.openInputStreamForTable(name);
try {
XMLStreamReader xmlStreamReader = openPullParser(inputStream);
XmlPullProcessor.readTag(xmlStreamReader, XmlDataSetNode.TABLE_NODE);
String version = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.VERSION_ATTRIBUTE);
if (StringUtils.isNotEmpty(version)) {
return new PullProcessorTableMetaData(xmlStreamReader, Integer.parseInt(version));
} else {
return new PullProcessorTableMetaData(xmlStreamReader, 1);
}
} finally {
// abandon any remaining content
Closeables.closeQuietly(inputStream);
}
}
示例4: testSetPropertiesFile
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@Test
public final void testSetPropertiesFile() throws IOException {
//check if input stream finally closed
mockStatic(Closeables.class);
doNothing().when(Closeables.class);
Closeables.closeQuietly(any(InputStream.class));
TestRootModuleChecker.reset();
final CheckstyleAntTask antTask = getCheckstyleAntTask(CUSTOM_ROOT_CONFIG_FILE);
antTask.setFile(new File(getPath(VIOLATED_INPUT)));
antTask.setProperties(new File(getPath(
"InputCheckstyleAntTaskCheckstyleAntTest.properties")));
antTask.execute();
assertEquals("Property is not set",
"ignore", TestRootModuleChecker.getProperty());
verifyStatic(times(1));
Closeables.closeQuietly(any(InputStream.class));
}
示例5: dumpBindingsToFile
import com.google.common.io.Closeables; //导入方法依赖的package包/类
private void dumpBindingsToFile(
BindingType bindingType,
Map<KbaChangeSetQualifier,
KbaChangeSet> kbaChangeSet,
IPath outputLocation,
String description) throws FileNotFoundException, IOException {
String output = getBindingsPrintout(bindingType, kbaChangeSet, description);
File file = outputLocation.toFile();
PrintStream stream = null;
try {
stream = new PrintStream(new FileOutputStream(file));
stream.print(output);
} finally {
Closeables.closeQuietly(stream);
}
}
示例6: initializeClient
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@SuppressWarnings("Duplicates")
private void initializeClient() {
final URL url = Resources.getResource("server.properties");
final ByteSource byteSource = Resources.asByteSource(url);
final Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = byteSource.openBufferedStream();
properties.load(inputStream);
final String serverUrl = properties.getProperty("secure.url");
final String user = properties.getProperty("default.user");
final String password = properties.getProperty("default.password");
bpmClient = BpmClientFactory.createClient(serverUrl, user, password);
} catch (IOException e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(inputStream);
}
}
示例7: testSetHeaderSimple
import com.google.common.io.Closeables; //导入方法依赖的package包/类
/**
* Test of setHeader method, of class RegexpHeaderCheck.
*/
@Test
public void testSetHeaderSimple() {
//check if reader finally closed
mockStatic(Closeables.class);
doNothing().when(Closeables.class);
Closeables.closeQuietly(any(Reader.class));
final RegexpHeaderCheck instance = new RegexpHeaderCheck();
// check valid header passes
final String header = "abc.*";
instance.setHeader(header);
verifyStatic(times(2));
Closeables.closeQuietly(any(Reader.class));
}
示例8: testMerge
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@Test
@UseDataProvider("merge")
public void testMerge(InputStream local, InputStream remote, InputStream base, InputStream expected)
throws IOException {
MergeClient mergeClient = new ScmPomVersionsMergeClient();
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
mergeClient.merge(local, remote, base, os);
String result = os.toString();
Assert.assertEquals(new String(ByteStreams.toByteArray(expected)), result);
} finally {
Closeables.closeQuietly(local);
Closeables.closeQuietly(remote);
Closeables.closeQuietly(base);
Closeables.close(os, true);
}
}
示例9: checkPackageXml
import com.google.common.io.Closeables; //导入方法依赖的package包/类
private static void checkPackageXml(String pkg, File output, String expected)
throws IOException {
assertNotNull(output);
assertTrue(output.exists());
URL url = new URL("jar:" + fileToUrlString(output) + "!/" + pkg.replace('.','/') +
"/annotations.xml");
InputStream stream = url.openStream();
try {
byte[] bytes = ByteStreams.toByteArray(stream);
assertNotNull(bytes);
String xml = new String(bytes, Charsets.UTF_8);
assertEquals(expected, xml);
} finally {
Closeables.closeQuietly(stream);
}
}
示例10: processFiltered
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@Override
protected void processFiltered(File file, FileText fileText) {
final UniqueProperties properties = new UniqueProperties();
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
properties.load(fileInputStream);
}
catch (IOException ex) {
log(0, MSG_IO_EXCEPTION_KEY, file.getPath(),
ex.getLocalizedMessage());
}
finally {
Closeables.closeQuietly(fileInputStream);
}
for (Entry<String> duplication : properties
.getDuplicatedKeys().entrySet()) {
final String keyName = duplication.getElement();
final int lineNumber = getLineNumber(fileText, keyName);
// Number of occurrences is number of duplications + 1
log(lineNumber, MSG_KEY, keyName, duplication.getCount() + 1);
}
}
示例11: loadHeaderFile
import com.google.common.io.Closeables; //导入方法依赖的package包/类
/**
* Load the header from a file.
* @throws CheckstyleException if the file cannot be loaded
*/
private void loadHeaderFile() throws CheckstyleException {
checkHeaderNotInitialized();
Reader headerReader = null;
try {
headerReader = new InputStreamReader(new BufferedInputStream(
headerFile.toURL().openStream()), charset);
loadHeader(headerReader);
}
catch (final IOException ex) {
throw new CheckstyleException(
"unable to load header file " + headerFile, ex);
}
finally {
Closeables.closeQuietly(headerReader);
}
}
示例12: initializeClient
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@BeforeClass
public void initializeClient() {
final URL url = Resources.getResource("server.properties");
final ByteSource byteSource = Resources.asByteSource(url);
final Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = byteSource.openBufferedStream();
properties.load(inputStream);
final String serverUrl = properties.getProperty("default.url");
final String user = properties.getProperty("default.user");
final String password = properties.getProperty("default.password");
bpmClient = BpmClientFactory.createClient(serverUrl, user, password);
} catch (IOException e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(inputStream);
}
}
示例13: testExistingTargetFilePlainOutputProperties
import com.google.common.io.Closeables; //导入方法依赖的package包/类
@Test
public void testExistingTargetFilePlainOutputProperties() throws Exception {
mockStatic(Closeables.class);
doNothing().when(Closeables.class);
Closeables.closeQuietly(any(InputStream.class));
//exit.expectSystemExitWithStatus(0);
exit.checkAssertionAfterwards(new Assertion() {
@Override
public void checkAssertion() {
assertEquals("Unexpected output log", auditStartMessage.getMessage() + EOL
+ auditFinishMessage.getMessage() + EOL, systemOut.getLog());
assertEquals("Unexpected system error log", "", systemErr.getLog());
}
});
Main.main("-c", getPath("InputMainConfig-classname-prop.xml"),
"-p", getPath("InputMainMycheckstyle.properties"),
getPath("InputMain.java"));
verifyStatic(times(1));
Closeables.closeQuietly(any(InputStream.class));
}
示例14: load
import com.google.common.io.Closeables; //导入方法依赖的package包/类
/**
* Load cached values from file.
* @throws IOException when there is a problems with file read
*/
public void load() throws IOException {
// get the current config so if the file isn't found
// the first time the hash will be added to output file
configHash = getHashCodeBasedOnObjectContent(config);
if (new File(fileName).exists()) {
FileInputStream inStream = null;
try {
inStream = new FileInputStream(fileName);
details.load(inStream);
final String cachedConfigHash = details.getProperty(CONFIG_HASH_KEY);
if (!configHash.equals(cachedConfigHash)) {
// Detected configuration change - clear cache
reset();
}
}
finally {
Closeables.closeQuietly(inStream);
}
}
else {
// put the hash in the file if the file is going to be created
reset();
}
}
示例15: readByteArrayFromResource
import com.google.common.io.Closeables; //导入方法依赖的package包/类
public static byte[] readByteArrayFromResource(String resource) throws IOException {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
return null;
}
return readByteArray(in);
} finally {
Closeables.closeQuietly(in);
}
}