Java Code Examples for software.amazon.awssdk.utils.IoUtils#closeQuietly()
The following examples show how to use
software.amazon.awssdk.utils.IoUtils#closeQuietly() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InputStreamUtils.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
/** * Reads to the end of the inputStream returning a byte array of the contents * * @param inputStream * InputStream to drain * @return Remaining data in stream as a byte array */ public static byte[] drainInputStream(InputStream inputStream) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; long bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) > -1) { byteArrayOutputStream.write(buffer, 0, (int) bytesRead); } return byteArrayOutputStream.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } finally { IoUtils.closeQuietly(byteArrayOutputStream, null); } }
Example 2
Source File: JsonPolicyWriter.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
/** * Converts the specified AWS policy object to a JSON string, suitable for * passing to an AWS service. * * @param policy * The AWS policy object to convert to a JSON string. * * @return The JSON string representation of the specified policy object. * * @throws IllegalArgumentException * If the specified policy is null or invalid and cannot be * serialized to a JSON string. */ public String writePolicyToString(Policy policy) { if (!isNotNull(policy)) { throw new IllegalArgumentException("Policy cannot be null"); } try { return jsonStringOf(policy); } catch (Exception e) { String message = "Unable to serialize policy to JSON string: " + e.getMessage(); throw new IllegalArgumentException(message, e); } finally { IoUtils.closeQuietly(writer, log); } }
Example 3
Source File: SdkAsserts.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * Returns true if, and only if, the contents read from the specified input streams are exactly * equal. Both input streams will be closed at the end of this method. * * @param expected * The input stream containing the expected contents. * @param actual * The stream that will be read, compared to the expected file contents, and finally * closed. * @return True if the two input streams contain the same data. * @throws IOException * If any problems are encountered comparing the file and stream. */ public static boolean doesStreamEqualStream(InputStream expected, InputStream actual) throws IOException { try { byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected); byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual); return Arrays.equals(expectedDigest, actualDigest); } catch (NoSuchAlgorithmException nse) { throw new RuntimeException(nse.getMessage(), nse); } finally { IoUtils.closeQuietly(expected, null); IoUtils.closeQuietly(actual, null); } }
Example 4
Source File: IonErrorCodeParser.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public String parseErrorCode(SdkHttpFullResponse response, JsonContent jsonContents) { IonReader reader = ionSystem.newReader(jsonContents.getRawContent()); try { IonType type = reader.next(); if (type != IonType.STRUCT) { throw SdkClientException.builder() .message(String.format("Can only get error codes from structs (saw %s), request id %s", type, getRequestId(response))) .build(); } boolean errorCodeSeen = false; String errorCode = null; String[] annotations = reader.getTypeAnnotations(); for (String annotation : annotations) { if (annotation.startsWith(TYPE_PREFIX)) { if (errorCodeSeen) { throw SdkClientException.builder() .message(String.format("Multiple error code annotations found for request id %s", getRequestId(response))) .build(); } else { errorCodeSeen = true; errorCode = annotation.substring(TYPE_PREFIX.length()); } } } return errorCode; } finally { IoUtils.closeQuietly(reader, log); } }
Example 5
Source File: ProfileFile.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public ProfileFile build() { InputStream stream = content != null ? content : FunctionalUtils.invokeSafely(() -> Files.newInputStream(contentLocation)); Validate.paramNotNull(type, "type"); Validate.paramNotNull(stream, "content"); try { return new ProfileFile(ProfileFileReader.parseFile(stream, type)); } finally { IoUtils.closeQuietly(stream, null); } }
Example 6
Source File: SdkFilterInputStream.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public void release() { // Don't call IOUtils.release(in, null) or else could lead to infinite loop IoUtils.closeQuietly(this, null); if (in instanceof Releasable) { // This allows any underlying stream that has the close operation // disabled to be truly released Releasable r = (Releasable) in; r.release(); } }
Example 7
Source File: SdkInputStream.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
/** * WARNING: Subclass that overrides this method must NOT call * super.release() or else it would lead to infinite loop. * <p> * {@inheritDoc} */ @Override public void release() { // Don't call IOUtils.release(in, null) or else could lead to infinite loop IoUtils.closeQuietly(this, null); InputStream in = getWrappedInputStream(); if (in instanceof Releasable) { // This allows any underlying stream that has the close operation // disabled to be truly released Releasable r = (Releasable) in; r.release(); } }
Example 8
Source File: SdkDigestInputStream.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Override public final void release() { // Don't call IOUtils.release(in, null) or else could lead to infinite loop IoUtils.closeQuietly(this, null); if (in instanceof Releasable) { // This allows any underlying stream that has the close operation // disabled to be truly released Releasable r = (Releasable) in; r.release(); } }
Example 9
Source File: RequestBodyTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void streamConstructorHasCorrectContentType() { StringInputStream inputStream = new StringInputStream("hello world"); RequestBody requestBody = RequestBody.fromInputStream(inputStream, 11); assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM); IoUtils.closeQuietly(inputStream, null); }
Example 10
Source File: RequestBodyTest.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
@Test public void nonMarkSupportedInputStreamContentType() throws IOException { File file = folder.newFile(); try (FileWriter writer = new FileWriter(file)) { writer.write("hello world"); } InputStream inputStream = Files.newInputStream(file.toPath()); RequestBody requestBody = RequestBody.fromInputStream(inputStream, 11); assertThat(requestBody.contentType()).isEqualTo(Mimetype.MIMETYPE_OCTET_STREAM); assertThat(requestBody.contentStreamProvider().newStream()).isNotNull(); IoUtils.closeQuietly(inputStream, null); }
Example 11
Source File: Utils.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
public static void closeQuietly(Closeable closeable) { IoUtils.closeQuietly(closeable, null); }
Example 12
Source File: StsProfileCredentialsProviderFactory.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Override public void close() { IoUtils.closeIfCloseable(parentCredentialsProvider, null); IoUtils.closeQuietly(credentialsProvider, null); IoUtils.closeQuietly(stsClient, null); }
Example 13
Source File: StsWebIdentityCredentialsProviderFactory.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
@Override public void close() { IoUtils.closeQuietly(credentialsProvider, null); IoUtils.closeQuietly(stsClient, null); }
Example 14
Source File: Utils.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
public static void closeQuietly(Closeable closeable) { IoUtils.closeQuietly(closeable, null); }
Example 15
Source File: HttpResourcesUtils.java From aws-sdk-java-v2 with Apache License 2.0 | 4 votes |
/** * Connects to the given endpoint to read the resource * and returns the text contents. * * @param endpointProvider The endpoint provider. * @param method The HTTP request method to use. * @return The text payload returned from the container metadata endpoint * service for the specified resource path. * @throws IOException If any problems were encountered while connecting to the * service for the requested resource path. * @throws SdkClientException If the requested service is not found. */ public String readResource(ResourcesEndpointProvider endpointProvider, String method) throws IOException { int retriesAttempted = 0; InputStream inputStream = null; while (true) { try { HttpURLConnection connection = connectionUtils.connectToEndpoint(endpointProvider.endpoint(), endpointProvider.headers(), method); int statusCode = connection.getResponseCode(); if (statusCode == HttpURLConnection.HTTP_OK) { inputStream = connection.getInputStream(); return IoUtils.toUtf8String(inputStream); } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) { // This is to preserve existing behavior of EC2 Instance metadata service. throw SdkClientException.builder() .message("The requested metadata is not found at " + connection.getURL()) .build(); } else { if (!endpointProvider.retryPolicy().shouldRetry(retriesAttempted++, ResourcesEndpointRetryParameters.builder() .withStatusCode(statusCode) .build())) { inputStream = connection.getErrorStream(); handleErrorResponse(inputStream, statusCode, connection.getResponseMessage()); } } } catch (IOException ioException) { if (!endpointProvider.retryPolicy().shouldRetry(retriesAttempted++, ResourcesEndpointRetryParameters.builder() .withException(ioException) .build())) { throw ioException; } log.debug("An IOException occurred when connecting to endpoint: {} \n Retrying to connect again", endpointProvider.endpoint()); } finally { IoUtils.closeQuietly(inputStream, log); } } }