software.amazon.awssdk.utils.IoUtils.closeQuietly()方法的使用及代码示例

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

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

IoUtils.closeQuietly介绍

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

代码示例

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

  1. /**
  2. * Closes the given Closeable quietly.
  3. * @param maybeCloseable the given closeable
  4. * @param log logger used to log any failure should the close fail
  5. */
  6. public static void closeIfCloseable(Object maybeCloseable, Logger log) {
  7. if (maybeCloseable instanceof AutoCloseable) {
  8. IoUtils.closeQuietly((AutoCloseable) maybeCloseable, log);
  9. }
  10. }

代码示例来源:origin: software.amazon.awssdk/utils

  1. /**
  2. * Closes the given Closeable quietly.
  3. * @param maybeCloseable the given closeable
  4. * @param log logger used to log any failure should the close fail
  5. */
  6. public static void closeIfCloseable(Object maybeCloseable, Logger log) {
  7. if (maybeCloseable instanceof AutoCloseable) {
  8. IoUtils.closeQuietly((AutoCloseable) maybeCloseable, log);
  9. }
  10. }

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

  1. public static void closeQuietly(Closeable closeable) {
  2. IoUtils.closeQuietly(closeable, null);
  3. }
  4. }

代码示例来源:origin: software.amazon.awssdk/codegen-lite

  1. public static void closeQuietly(Closeable closeable) {
  2. IoUtils.closeQuietly(closeable, null);
  3. }
  4. }

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

  1. public static void closeQuietly(Closeable closeable) {
  2. IoUtils.closeQuietly(closeable, null);
  3. }

代码示例来源:origin: software.amazon.awssdk/codegen

  1. public static void closeQuietly(Closeable closeable) {
  2. IoUtils.closeQuietly(closeable, null);
  3. }

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

  1. @Override
  2. public void close() {
  3. IoUtils.closeIfCloseable(parentCredentialsProvider, null);
  4. IoUtils.closeQuietly(credentialsProvider, null);
  5. IoUtils.closeQuietly(stsClient, null);
  6. }
  7. }

代码示例来源:origin: software.amazon.awssdk/sts

  1. @Override
  2. public void close() {
  3. IoUtils.closeIfCloseable(parentCredentialsProvider, null);
  4. IoUtils.closeQuietly(credentialsProvider, null);
  5. IoUtils.closeQuietly(stsClient, null);
  6. }
  7. }

代码示例来源:origin: software.amazon.awssdk/sdk-core

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

代码示例来源:origin: software.amazon.awssdk/sdk-core

  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 (in instanceof Releasable) {
  6. // This allows any underlying stream that has the close operation
  7. // disabled to be truly released
  8. Releasable r = (Releasable) in;
  9. r.release();
  10. }
  11. }
  12. }

代码示例来源:origin: software.amazon.awssdk/sdk-core

  1. /**
  2. * Close the input stream if required.
  3. */
  4. private void closeInputStreamIfNeeded(SdkHttpFullResponse httpResponse,
  5. boolean didRequestFail) {
  6. // Always close on failed requests. Close on successful requests unless it needs connection left open
  7. if (didRequestFail || !successResponseHandler.needsConnectionLeftOpen()) {
  8. Optional.ofNullable(httpResponse)
  9. .flatMap(SdkHttpFullResponse::content) // If no content, no need to close
  10. .ifPresent(s -> IoUtils.closeQuietly(s, log));
  11. }
  12. }
  13. }

代码示例来源:origin: software.amazon.awssdk/sdk-core

  1. private Mimetype() {
  2. Optional.ofNullable(CLASS_LOADER).map(loader -> loader.getResourceAsStream(MIME_TYPE_PATH)).ifPresent(
  3. stream -> {
  4. try {
  5. loadAndReplaceMimetypes(stream);
  6. } catch (IOException e) {
  7. LOG.debug("Failed to load mime types from file in the classpath: mime.types", e);
  8. } finally {
  9. IoUtils.closeQuietly(stream, null);
  10. }
  11. }
  12. );
  13. }

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

  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: software.amazon.awssdk/test-utils

  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: software.amazon.awssdk/sdk-core

  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. static void release(Closeable is, Logger log) {
  18. closeQuietly(is, log);
  19. if (is instanceof Releasable) {
  20. Releasable r = (Releasable) is;
  21. r.release();
  22. }
  23. }
  24. }

代码示例来源:origin: software.amazon.awssdk/test-utils

  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 actual
  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. byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected);
  17. byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual);
  18. return Arrays.equals(expectedDigest, actualDigest);
  19. } catch (NoSuchAlgorithmException nse) {
  20. throw new RuntimeException(nse.getMessage(), nse);
  21. } finally {
  22. IoUtils.closeQuietly(expected, null);
  23. IoUtils.closeQuietly(actual, null);
  24. }
  25. }

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

  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 actual
  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. byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected);
  17. byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual);
  18. return Arrays.equals(expectedDigest, actualDigest);
  19. } catch (NoSuchAlgorithmException nse) {
  20. throw new RuntimeException(nse.getMessage(), nse);
  21. } finally {
  22. IoUtils.closeQuietly(expected, null);
  23. IoUtils.closeQuietly(actual, null);
  24. }
  25. }

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

  1. @Override
  2. public ProfileFile build() {
  3. InputStream stream = content != null ? content :
  4. FunctionalUtils.invokeSafely(() -> Files.newInputStream(contentLocation));
  5. Validate.paramNotNull(type, "type");
  6. Validate.paramNotNull(stream, "content");
  7. try {
  8. return new ProfileFile(ProfileFileReader.parseFile(stream, type));
  9. } finally {
  10. IoUtils.closeQuietly(stream, null);
  11. }
  12. }
  13. }

代码示例来源:origin: software.amazon.awssdk/profiles

  1. @Override
  2. public ProfileFile build() {
  3. InputStream stream = content != null ? content :
  4. FunctionalUtils.invokeSafely(() -> Files.newInputStream(contentLocation));
  5. Validate.paramNotNull(type, "type");
  6. Validate.paramNotNull(stream, "content");
  7. try {
  8. return new ProfileFile(ProfileFileReader.parseFile(stream, type));
  9. } finally {
  10. IoUtils.closeQuietly(stream, null);
  11. }
  12. }
  13. }

代码示例来源:origin: software.amazon.awssdk/sdk-core

  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. }

相关文章