当前位置: 首页>>代码示例>>Java>>正文


Java Closeables.closeQuietly方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:yrain,项目名称:smart-cache,代码行数:19,代码来源:Utils.java

示例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;
}
 
开发者ID:twosigma,项目名称:beaker-notebook-archive,代码行数:25,代码来源:SimpleClassLoader.java

示例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);
  }
}
 
开发者ID:alfasoftware,项目名称:morf,代码行数:23,代码来源:XmlDataSetProducer.java

示例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));
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:21,代码来源:CheckstyleAntTaskTest.java

示例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);
  }
}
 
开发者ID:alfsch,项目名称:workspacemechanic,代码行数:17,代码来源:KeyBindingsManualFormatter.java

示例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);
    }
}
 
开发者ID:egetman,项目名称:ibm-bpm-rest-client,代码行数:21,代码来源:ProcessClientTest.java

示例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));
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:19,代码来源:RegexpHeaderCheckTest.java

示例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);
  }
}
 
开发者ID:shillner,项目名称:unleash-maven-plugin,代码行数:18,代码来源:ScmPomVersionsMergeClientTest.java

示例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);
    }
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:17,代码来源:ExtractAnnotationsDriverTest.java

示例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);
    }
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:25,代码来源:UniquePropertiesCheck.java

示例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);
    }
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:21,代码来源:AbstractHeaderCheck.java

示例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);
    }
}
 
开发者ID:egetman,项目名称:ibm-bpm-rest-client,代码行数:21,代码来源:ExposedClientTest.java

示例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));
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:23,代码来源:MainTest.java

示例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();
    }
}
 
开发者ID:rnveach,项目名称:checkstyle-backport-jre6,代码行数:29,代码来源:PropertyCacheFile.java

示例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);
    }
}
 
开发者ID:yrain,项目名称:smart-cache,代码行数:14,代码来源:Utils.java


注:本文中的com.google.common.io.Closeables.closeQuietly方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。