本文整理汇总了Java中com.google.common.io.Closeables类的典型用法代码示例。如果您正苦于以下问题:Java Closeables类的具体用法?Java Closeables怎么用?Java Closeables使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Closeables类属于com.google.common.io包,在下文中一共展示了Closeables类的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: closeSource
import com.google.common.io.Closeables; //导入依赖的package包/类
private void closeSource(final StreamSource source)
{
if( source != null )
{
try
{
// one of these should be non-null
Closeables.close(source.getReader(), true);
Closeables.close(source.getInputStream(), true);
}
catch( Throwable th )
{
// Ignore
}
}
}
示例4: Combiner
import com.google.common.io.Closeables; //导入依赖的package包/类
public Combiner(ManifestResolver resolver, Writer output) throws IOException, XmlPullParserException
{
this.output = output;
this.resolver = resolver;
Reader reader = null;
try
{
reader = resolver.getStream();
parser = new MXParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
parser.setInput(reader);
parseXml();
}
finally
{
Closeables.close(reader, true); // Quietly
}
}
示例5: createItemFromFile
import com.google.common.io.Closeables; //导入依赖的package包/类
private ObjectNode createItemFromFile(String jsonFile, String token, String stagingUuid, int responseCode,
boolean returnError) throws Exception
{
final StringWriter sw = new StringWriter();
final InputStream input = ItemApiEditTest.class.getResourceAsStream(jsonFile);
try
{
CharStreams.copy(new InputStreamReader(input), sw);
}
finally
{
Closeables.close(input, true);
}
String newItemJson = sw.toString();
return createItemFromString(newItemJson, token, stagingUuid, responseCode, returnError);
}
示例6: getStringValue
import com.google.common.io.Closeables; //导入依赖的package包/类
private static String getStringValue(@NonNull IAbstractFile file, @NonNull String xPath)
throws StreamException, XPathExpressionException {
XPath xpath = AndroidXPathFactory.newXPath();
InputStream is = null;
try {
is = file.getContents();
return xpath.evaluate(xPath, new InputSource(is));
} finally {
try {
Closeables.close(is, true /* swallowIOException */);
} catch (IOException e) {
// cannot happen
}
}
}
示例7: createNewStore
import com.google.common.io.Closeables; //导入依赖的package包/类
public static boolean createNewStore(String storeType, File storeFile, char[] storePassword, DN dn) {
if (storeType == null) {
storeType = "jks";
}
try {
KeyStore ks = KeyStore.getInstance(storeType);
ks.load(null, null);
Pair<PrivateKey, X509Certificate> generated = generateKeyAndCertificate("RSA", "SHA1withRSA", dn.validityYears, encodeDN(dn));
ks.setKeyEntry(dn.alias, generated.getFirst(), dn.password, new Certificate[]{generated.getSecond()});
FileOutputStream fos = new FileOutputStream(storeFile);
boolean threw = true;
try {
ks.store(fos, storePassword);
threw = false;
} finally {
Closeables.close(fos, threw);
}
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | OperatorCreationException e) {
return false;
}
return true;
}
示例8: addNewKey
import com.google.common.io.Closeables; //导入依赖的package包/类
public static boolean addNewKey(KeyStore ks, File storeFile, char[] storePassword, DN dn) {
try {
Pair<PrivateKey, X509Certificate> generated = generateKeyAndCertificate("RSA", "SHA1withRSA", dn.validityYears, encodeDN(dn));
ks.setKeyEntry(dn.alias, generated.getFirst(), dn.password, new Certificate[]{generated.getSecond()});
FileOutputStream fos = new FileOutputStream(storeFile);
boolean threw = true;
try {
ks.store(fos, storePassword);
threw = false;
} finally {
Closeables.close(fos, threw);
}
} catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | OperatorCreationException e) {
return false;
}
return true;
}
示例9: 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);
}
}
示例10: getMovies
import com.google.common.io.Closeables; //导入依赖的package包/类
public static Map<Long, String> getMovies(String base) {
Map<Long, String> movies = new HashMap<>();
try {
File file = new File(base + "hot_movies.csv");
FileLineIterator iterator = new FileLineIterator(file, false);
String line = iterator.next();
while (!line.isEmpty()) {
String[] m = line.split(",");
movies.put(Long.parseLong(m[0]), m[2]);
line = iterator.next();
}
Closeables.close(iterator, true);
} catch (Exception ex) {
}
return movies;
}
示例11: write
import com.google.common.io.Closeables; //导入依赖的package包/类
@Override
public OutputStream write(String entry) throws IOException {
saveInputFile();
File file = changedEntries.get(entry);
if (file == null) {
file = new File(changedFiles, entry);
createParentDirs(file);
changedEntries.put(entry, file);
}
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
Closeables.close(outputStreams.put(entry, out), true);
if (removedEntries.remove(entry)) {
deleteIfExists(new File(removedFiles, entry).toPath());
}
return out;
}
示例12: saveTo
import com.google.common.io.Closeables; //导入依赖的package包/类
@Override
public void saveTo(File target) throws IOException {
for (OutputStream out : outputStreams.values()) {
Closeables.close(out, false);
}
outputStreams.clear();
try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)))) {
if (zipFile != null) {
for (ZipEntry entry : Collections.list(zipFile.entries())) {
if (!changedEntries.containsKey(entry.getName()) && !removedEntries.contains(entry.getName())) {
out.putNextEntry(entry);
Utils.copy(zipFile.getInputStream(entry), out);
}
}
}
for (Map.Entry<String, File> e : changedEntries.entrySet()) {
out.putNextEntry(new ZipEntry(e.getKey()));
Utils.copy(new BufferedInputStream(new FileInputStream(e.getValue())), out);
}
}
}
示例13: 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);
}
}
示例14: 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);
}
}
示例15: getTranslationKeys
import com.google.common.io.Closeables; //导入依赖的package包/类
/**
* Loads the keys from the specified translation file into a set.
* @param file translation file.
* @return a Set object which holds the loaded keys.
*/
private Set<String> getTranslationKeys(File file) {
Set<String> keys = new HashSet<String>();
InputStream inStream = null;
try {
inStream = new FileInputStream(file);
final Properties translations = new Properties();
translations.load(inStream);
keys = translations.stringPropertyNames();
}
catch (final IOException ex) {
logIoException(ex, file);
}
finally {
Closeables.closeQuietly(inStream);
}
return keys;
}