com.amazonaws.util.IOUtils.closeQuietly()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(270)

本文整理了Java中com.amazonaws.util.IOUtils.closeQuietly()方法的一些代码示例,展示了IOUtils.closeQuietly()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.closeQuietly()方法的具体详情如下:
包路径:com.amazonaws.util.IOUtils
类名称:IOUtils
方法名:closeQuietly

IOUtils.closeQuietly介绍

[英]Closes the given Closeable quietly.
[中]安静地关闭给定的可关闭对象。

代码示例

代码示例来源:origin: aws/aws-sdk-java

  1. private Partitions loadPartitionFromStream(InputStream stream, String location) {
  2. try {
  3. return mapper.readValue(stream, Partitions.class);
  4. } catch (IOException e) {
  5. throw new SdkClientException("Error while loading partitions " +
  6. "file from " + location, e);
  7. } finally {
  8. IOUtils.closeQuietly(stream, null);
  9. }
  10. }
  11. }

代码示例来源:origin: aws/aws-sdk-java

  1. private void closeQuietlyForRuntimeExceptions(Closeable c, Log log) {
  2. try {
  3. closeQuietly(c, log);
  4. } catch (RuntimeException e) {
  5. if (log.isDebugEnabled()) {
  6. log.debug("Unable to close closeable", e);
  7. }
  8. }
  9. }

代码示例来源:origin: aws/aws-sdk-java

  1. @Override
  2. public final void release() {
  3. // Don't call IOUtils.release(in, null) or else could lead to infinite loop
  4. IOUtils.closeQuietly(this, null);
  5. if (out instanceof Releasable) {
  6. // This allows any underlying stream that has the close operation
  7. // disabled to be truly released
  8. Releasable r = (Releasable)out;
  9. r.release();
  10. }
  11. }
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * Reads to the end of the inputStream returning a byte array of the contents
  3. *
  4. * @param inputStream
  5. * InputStream to drain
  6. * @return Remaining data in stream as a byte array
  7. */
  8. public static byte[] drainInputStream(InputStream inputStream) {
  9. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  10. try {
  11. byte[] buffer = new byte[1024];
  12. long bytesRead = 0;
  13. while ((bytesRead = inputStream.read(buffer)) > -1) {
  14. byteArrayOutputStream.write(buffer, 0, (int) bytesRead);
  15. }
  16. return byteArrayOutputStream.toByteArray();
  17. } catch (IOException e) {
  18. throw new RuntimeException(e);
  19. } finally {
  20. IOUtils.closeQuietly(byteArrayOutputStream, null);
  21. }
  22. }
  23. }

代码示例来源:origin: aws/aws-sdk-java

  1. private static String kotlinVersionByJar() {
  2. StringBuilder kotlinVersion = new StringBuilder("");
  3. JarInputStream kotlinJar = null;
  4. try {
  5. Class<?> kotlinUnit = Class.forName("kotlin.Unit");
  6. kotlinVersion.append("kotlin");
  7. kotlinJar = new JarInputStream(kotlinUnit.getProtectionDomain().getCodeSource().getLocation().openStream());
  8. String version = kotlinJar.getManifest().getMainAttributes().getValue("Implementation-Version");
  9. concat(kotlinVersion, version, "/");
  10. } catch (ClassNotFoundException e) {
  11. //Ignore
  12. } catch (Exception e) {
  13. if (log.isTraceEnabled()) {
  14. log.trace("Exception attempting to get Kotlin version.", e);
  15. }
  16. } finally {
  17. closeQuietly(kotlinJar, log);
  18. }
  19. return kotlinVersion.toString();
  20. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * Releases the given {@link Closeable} especially if it was an instance of
  3. * {@link Releasable}.
  4. * <p>
  5. * For example, the creation of a <code>ResettableInputStream</code> would entail
  6. * physically opening a file. If the opened file is meant to be closed only
  7. * (in a finally block) by the very same code block that created it, then it
  8. * is necessary that the release method must not be called while the
  9. * execution is made in other stack frames.
  10. *
  11. * In such case, as other stack frames may inadvertently or indirectly call
  12. * the close method of the stream, the creator of the stream would need to
  13. * explicitly disable the accidental closing via
  14. * <code>ResettableInputStream#disableClose()</code>, so that the release method
  15. * becomes the only way to truly close the opened file.
  16. */
  17. public static void release(Closeable is, Log log) {
  18. closeQuietly(is, log);
  19. if (is instanceof Releasable) {
  20. Releasable r = (Releasable) is;
  21. r.release();
  22. }
  23. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * Loads the versionInfo.properties file from the AWS Java SDK and
  3. * stores the information so that the file doesn't have to be read the
  4. * next time the data is needed.
  5. */
  6. private static void initializeVersion() {
  7. InputStream inputStream = ClassLoaderHelper.getResourceAsStream(
  8. VERSION_INFO_FILE, true, VersionInfoUtils.class);
  9. Properties versionInfoProperties = new Properties();
  10. try {
  11. if (inputStream == null)
  12. throw new Exception(VERSION_INFO_FILE + " not found on classpath");
  13. versionInfoProperties.load(inputStream);
  14. version = versionInfoProperties.getProperty("version");
  15. platform = versionInfoProperties.getProperty("platform");
  16. } catch (Exception e) {
  17. log.info("Unable to load version information for the running SDK: " + e.getMessage());
  18. version = "unknown-version";
  19. platform = "java";
  20. } finally {
  21. closeQuietly(inputStream, log);
  22. }
  23. }

代码示例来源:origin: aws/aws-sdk-java

  1. private RegionMetadata loadOverrideMetadataIfExists() {
  2. RegionMetadata metadata = loadFromSystemProperty();
  3. if (metadata == null) {
  4. InputStream override = RegionUtils.class
  5. .getResourceAsStream(OVERRIDE_ENDPOINTS_RESOURCE_PATH);
  6. if (override != null) {
  7. metadata = loadFromStream(override);
  8. IOUtils.closeQuietly(override, LOG);
  9. }
  10. }
  11. return metadata;
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. private JmesPathExpression getAstFromArgument(String argument, Map<String, JmesPathExpression> argumentToAstMap) throws
  2. IOException {
  3. if (argument != null && !argumentToAstMap.containsKey(argument)) {
  4. final Process p = executeToAstProcess(argument);
  5. if(p.exitValue()!= 0) {
  6. throw new RuntimeException(IOUtils.toString(p.getErrorStream()));
  7. }
  8. JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream()));
  9. JmesPathExpression ast = fromAstJsonToAstJava(jsonNode);
  10. argumentToAstMap.put(argument, ast);
  11. IOUtils.closeQuietly(p.getInputStream(), null);
  12. return ast;
  13. } else if (argument != null) {
  14. return argumentToAstMap.get(argument);
  15. }
  16. return null;
  17. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * Returns true if, and only if, the contents read from the specified input streams are exactly
  3. * equal. Both input streams will be closed at the end of this method.
  4. *
  5. * @param expected
  6. * The input stream containing the expected contents.
  7. * @param inputStream
  8. * The stream that will be read, compared to the expected file contents, and finally
  9. * closed.
  10. * @return True if the two input streams contain the same data.
  11. * @throws IOException
  12. * If any problems are encountered comparing the file and stream.
  13. */
  14. public static boolean doesStreamEqualStream(InputStream expected, InputStream actual) throws IOException {
  15. try {
  16. final byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected);
  17. final byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual);
  18. return Arrays.equals(expectedDigest, actualDigest);
  19. } catch (NoSuchAlgorithmException nse) {
  20. throw new AmazonClientException(nse.getMessage(), nse);
  21. } finally {
  22. IOUtils.closeQuietly(expected, null);
  23. IOUtils.closeQuietly(actual, null);
  24. }
  25. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * WARNING: Subclass that overrides this method must NOT call
  3. * super.release() or else it would lead to infinite loop.
  4. * <p>
  5. * {@inheritDoc}
  6. */
  7. @Override
  8. public void release() {
  9. // Don't call IOUtils.release(in, null) or else could lead to infinite loop
  10. IOUtils.closeQuietly(this, null);
  11. InputStream in = getWrappedInputStream();
  12. if (in instanceof Releasable) {
  13. // This allows any underlying stream that has the close operation
  14. // disabled to be truly released
  15. Releasable r = (Releasable)in;
  16. r.release();
  17. }
  18. }
  19. }

代码示例来源:origin: aws/aws-sdk-java

  1. @Override
  2. public String getObjectAsString(String bucketName, String key)
  3. throws AmazonServiceException, SdkClientException {
  4. rejectNull(bucketName, "Bucket name must be provided");
  5. rejectNull(key, "Object key must be provided");
  6. S3Object object = getObject(bucketName, key);
  7. try {
  8. return IOUtils.toString(object.getObjectContent());
  9. } catch (IOException e) {
  10. throw new SdkClientException("Error streaming content from S3 during download");
  11. } finally {
  12. IOUtils.closeQuietly(object, log);
  13. }
  14. }

代码示例来源:origin: aws/aws-sdk-java

  1. public GetIntrospectionSchemaResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. GetIntrospectionSchemaResult getIntrospectionSchemaResult = new GetIntrospectionSchemaResult();
  3. java.io.InputStream is = context.getHttpResponse().getContent();
  4. if (is != null) {
  5. try {
  6. getIntrospectionSchemaResult.setSchema(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  7. } finally {
  8. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  9. }
  10. }
  11. return getIntrospectionSchemaResult;
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. public UpdateThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. UpdateThingShadowResult updateThingShadowResult = new UpdateThingShadowResult();
  3. java.io.InputStream is = context.getHttpResponse().getContent();
  4. if (is != null) {
  5. try {
  6. updateThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  7. } finally {
  8. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  9. }
  10. }
  11. return updateThingShadowResult;
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. public DeleteThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. DeleteThingShadowResult deleteThingShadowResult = new DeleteThingShadowResult();
  3. java.io.InputStream is = context.getHttpResponse().getContent();
  4. if (is != null) {
  5. try {
  6. deleteThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  7. } finally {
  8. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  9. }
  10. }
  11. return deleteThingShadowResult;
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. public GetThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. GetThingShadowResult getThingShadowResult = new GetThingShadowResult();
  3. java.io.InputStream is = context.getHttpResponse().getContent();
  4. if (is != null) {
  5. try {
  6. getThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  7. } finally {
  8. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  9. }
  10. }
  11. return getThingShadowResult;
  12. }

代码示例来源:origin: aws/aws-sdk-java

  1. /**
  2. * Used for performance testing purposes only.
  3. */
  4. private void putLocalObject(final UploadObjectRequest reqIn,
  5. OutputStream os) throws IOException {
  6. UploadObjectRequest req = reqIn.clone();
  7. final File fileOrig = req.getFile();
  8. final InputStream isOrig = req.getInputStream();
  9. if (isOrig == null) {
  10. if (fileOrig == null)
  11. throw new IllegalArgumentException("Either a file lor input stream must be specified");
  12. req.setInputStream(new FileInputStream(fileOrig));
  13. req.setFile(null);
  14. }
  15. try {
  16. IOUtils.copy(req.getInputStream(), os);
  17. } finally {
  18. cleanupDataSource(req, fileOrig, isOrig,
  19. req.getInputStream(), log);
  20. IOUtils.closeQuietly(os, log);
  21. }
  22. return;
  23. }

代码示例来源:origin: aws/aws-sdk-java

  1. public GetSdkResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. GetSdkResult getSdkResult = new GetSdkResult();
  3. if (context.isStartOfDocument()) {
  4. if (context.getHeader("Content-Type") != null) {
  5. context.setCurrentHeader("Content-Type");
  6. getSdkResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context));
  7. }
  8. if (context.getHeader("Content-Disposition") != null) {
  9. context.setCurrentHeader("Content-Disposition");
  10. getSdkResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context));
  11. }
  12. }
  13. java.io.InputStream is = context.getHttpResponse().getContent();
  14. if (is != null) {
  15. try {
  16. getSdkResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  17. } finally {
  18. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  19. }
  20. }
  21. return getSdkResult;
  22. }

代码示例来源:origin: aws/aws-sdk-java

  1. public GetExportResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. GetExportResult getExportResult = new GetExportResult();
  3. if (context.isStartOfDocument()) {
  4. if (context.getHeader("Content-Type") != null) {
  5. context.setCurrentHeader("Content-Type");
  6. getExportResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context));
  7. }
  8. if (context.getHeader("Content-Disposition") != null) {
  9. context.setCurrentHeader("Content-Disposition");
  10. getExportResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context));
  11. }
  12. }
  13. java.io.InputStream is = context.getHttpResponse().getContent();
  14. if (is != null) {
  15. try {
  16. getExportResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  17. } finally {
  18. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  19. }
  20. }
  21. return getExportResult;
  22. }

代码示例来源:origin: aws/aws-sdk-java

  1. @Override
  2. public final void putLocalObjectSecurely(final UploadObjectRequest reqIn,
  3. String uploadId, OutputStream os) throws IOException {
  4. UploadObjectRequest req = reqIn.clone();
  5. final File fileOrig = req.getFile();
  6. final InputStream isOrig = req.getInputStream();
  7. final T uploadContext = multipartUploadContexts.get(uploadId);
  8. ContentCryptoMaterial cekMaterial = uploadContext.getContentCryptoMaterial();
  9. req = wrapWithCipher(req, cekMaterial);
  10. try {
  11. IOUtils.copy(req.getInputStream(), os);
  12. // so it won't crap out with a false negative at the end; (Not
  13. // really relevant here)
  14. uploadContext.setHasFinalPartBeenSeen(true);
  15. } finally {
  16. cleanupDataSource(req, fileOrig, isOrig,
  17. req.getInputStream(), log);
  18. IOUtils.closeQuietly(os, log);
  19. }
  20. return;
  21. }

相关文章