當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。