com.amazonaws.util.IOUtils类的使用及代码示例

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

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

IOUtils介绍

[英]Utilities for IO operations.
[中]IO操作的实用程序。

代码示例

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

  1. /**
  2. * Reads a system resource fully into a String
  3. *
  4. * @param location
  5. * Relative or absolute location of system resource.
  6. * @return String contents of resource file
  7. * @throws RuntimeException
  8. * if any error occurs
  9. */
  10. protected String getResourceAsString(String location) {
  11. try {
  12. InputStream resourceStream = getClass().getResourceAsStream(location);
  13. String resourceAsString = IOUtils.toString(resourceStream);
  14. resourceStream.close();
  15. return resourceAsString;
  16. } catch (Exception e) {
  17. throw new RuntimeException(e);
  18. }
  19. }

代码示例来源:origin: brianfrankcooper/YCSB

  1. /**
  2. * Download an object from S3.
  3. *
  4. * @param bucket
  5. * The name of the bucket
  6. * @param key
  7. * The file key of the object to upload/update.
  8. * @param result
  9. * The Hash map where data from the object are written
  10. *
  11. */
  12. protected Status readFromStorage(String bucket, String key,
  13. Map<String, ByteIterator> result, SSECustomerKey ssecLocal) {
  14. try {
  15. S3Object object = getS3ObjectAndMetadata(bucket, key, ssecLocal);
  16. InputStream objectData = object.getObjectContent(); //consuming the stream
  17. // writing the stream to bytes and to results
  18. result.put(key, new ByteArrayByteIterator(IOUtils.toByteArray(objectData)));
  19. objectData.close();
  20. object.close();
  21. } catch (Exception e){
  22. System.err.println("Not possible to get the object "+key);
  23. e.printStackTrace();
  24. return Status.ERROR;
  25. }
  26. return Status.OK;
  27. }

代码示例来源: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. 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. 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. @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. /**
  2. * It's important to override writeTo. Some versions of Apache HTTP
  3. * client use writeTo for {@link org.apache.http.entity.BufferedHttpEntity}
  4. * and the default implementation just delegates to the wrapped entity
  5. * which completely bypasses our CRC32 calculating input stream. The
  6. * {@link org.apache.http.entity.BufferedHttpEntity} is used for the
  7. * request timeout and client execution timeout features.
  8. *
  9. * @see <a href="https://github.com/aws/aws-sdk-java/issues/526">Issue #526</a>
  10. *
  11. * @param outstream OutputStream to write contents to
  12. */
  13. @Override
  14. public void writeTo(OutputStream outstream) throws IOException {
  15. try {
  16. IOUtils.copy(this.getContent(), outstream);
  17. } finally {
  18. this.getContent().close();
  19. }
  20. }
  21. };

代码示例来源:origin: zalando-stups/fullstop

  1. public String downloadObject(final String bucketName, final String key) {
  2. final S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));
  3. final InputStream inputStream = object.getObjectContent();
  4. String result = null;
  5. try {
  6. result = IOUtils.toString(inputStream);
  7. } catch (final IOException e) {
  8. log.warn("Could not download file for bucket: {}, with key: {}", bucketName, key);
  9. } finally {
  10. if (inputStream != null) {
  11. try {
  12. inputStream.close();
  13. } catch (final IOException ex) {
  14. log.debug("Ignore failure in closing the Closeable", ex);
  15. }
  16. }
  17. }
  18. log.info("Downloaded file for bucket: {}, with key: {}", bucketName, key);
  19. return result;
  20. }
  21. }

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

  1. /**
  2. * Downloads the certificate from the provided URL. Asserts that the endpoint is an SNS endpoint and that
  3. * the certificate is vended over HTTPs.
  4. *
  5. * @param certUrl URL to download certificate from.
  6. * @return String contents of certificate.
  7. * @throws SdkClientException If certificate cannot be downloaded or URL is invalid.
  8. */
  9. private String downloadCert(URI certUrl) {
  10. try {
  11. signingCertUrlVerifier.verifyCertUrl(certUrl);
  12. HttpResponse response = client.execute(new HttpGet(certUrl));
  13. if (ApacheUtils.isRequestSuccessful(response)) {
  14. try {
  15. return IOUtils.toString(response.getEntity().getContent());
  16. } finally {
  17. response.getEntity().getContent().close();
  18. }
  19. } else {
  20. throw new HttpException("Could not download the certificate from SNS", response);
  21. }
  22. } catch (IOException e) {
  23. throw new SdkClientException("Unable to download SNS certificate from " + certUrl.toString(), e);
  24. }
  25. }

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

  1. /**
  2. * Creates a private key from the file given, either in RSA private key
  3. * (.pem) or pkcs8 (.der) format. Other formats will cause an exception to
  4. * be thrown.
  5. */
  6. public static PrivateKey loadPrivateKey(File privateKeyFile) throws InvalidKeySpecException, IOException {
  7. if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".pem") ) {
  8. InputStream is = new FileInputStream(privateKeyFile);
  9. try {
  10. return PEM.readPrivateKey(is);
  11. } finally {
  12. try {is.close();} catch(IOException ignore) {}
  13. }
  14. } else if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".der") ) {
  15. InputStream is = new FileInputStream(privateKeyFile);
  16. try {
  17. return RSA.privateKeyFromPKCS8(IOUtils.toByteArray(is));
  18. } finally {
  19. try {is.close();} catch(IOException ignore) {}
  20. }
  21. } else {
  22. throw new AmazonClientException("Unsupported file type for private key");
  23. }
  24. }

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

  1. final int code = response.getStatusCode();
  2. final InputStream content = response.getContent();
  3. final Reader reader = new InputStreamReader(response.getContent(),
  4. StringUtils.UTF8);
  5. final Object obj = GSON_WITH_DATE_FORMATTER.fromJson(reader, t);
  6. content.close();
  7. final String error = content == null ? "" : IOUtils.toString(content);
  8. final ApiClientException ase = new ApiClientException(error);
  9. ase.setStatusCode(response.getStatusCode());

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

  1. if (originalContent != null && originalContent.markSupported()
  2. && !(originalContent instanceof BufferedInputStream)) {
  3. originalContent.mark(readLimit);
  4. if (originalContent instanceof BufferedInputStream && originalContent.markSupported()) {
  5. if (entity != null) {
  6. try {
  7. closeQuietly(entity.getContent(), log);
  8. } catch (IOException e) {
  9. log.warn("Cannot close the response content.", e);

代码示例来源: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. 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. @Override
  2. public ObjectMetadata getObjectSecurely(GetObjectRequest getObjectRequest,
  3. File destinationFile) {
  4. assertParameterNotNull(destinationFile,
  5. "The destination file parameter must be specified when downloading an object directly to a file");
  6. S3Object s3Object = getObjectSecurely(getObjectRequest);
  7. // getObject can return null if constraints were specified but not met
  8. if (s3Object == null) return null;
  9. OutputStream outputStream = null;
  10. try {
  11. outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile));
  12. byte[] buffer = new byte[1024*10];
  13. int bytesRead;
  14. while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) {
  15. outputStream.write(buffer, 0, bytesRead);
  16. }
  17. } catch (IOException e) {
  18. throw new SdkClientException(
  19. "Unable to store object contents to disk: " + e.getMessage(), e);
  20. } finally {
  21. closeQuietly(outputStream, log);
  22. closeQuietly(s3Object.getObjectContent(), log);
  23. }
  24. /*
  25. * Unlike the standard Amazon S3 Client, the Amazon S3 Encryption Client does not do an MD5 check
  26. * here because the contents stored in S3 and the contents we just retrieved are different. In
  27. * S3, the stored contents are encrypted, and locally, the retrieved contents are decrypted.
  28. */
  29. return s3Object.getObjectMetadata();
  30. }

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

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

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

  1. return IOUtils.toString(inputStream);
  2. } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) {
  3. throw new SdkClientException("The requested metadata is not found at " + connection.getURL());
  4. } else {
  5. if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withStatusCode(statusCode).build())) {
  6. IOUtils.closeQuietly(inputStream, LOG);

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

  1. /**
  2. * Unsubscribes this endpoint from the topic.
  3. */
  4. public void unsubscribeFromTopic() {
  5. try {
  6. HttpGet request = new HttpGet(unsubscribeUrl.toURI());
  7. HttpResponse response = httpClient.execute(request);
  8. if (!ApacheUtils.isRequestSuccessful(response)) {
  9. throw new SdkClientException(String.format("Could not unsubscribe from %s: %d %s.%n%s",
  10. getTopicArn(),
  11. response.getStatusLine().getStatusCode(),
  12. response.getStatusLine().getReasonPhrase(),
  13. IOUtils.toString(response.getEntity().getContent())));
  14. }
  15. } catch (Exception e) {
  16. throw new SdkClientException(e);
  17. }
  18. }

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

  1. S3Object object = s3.getObject(getPartRequest);
  2. objectContent = object.getObjectContent();
  3. byte[] buffer = new byte[BUFFER_SIZE];
  4. int bytesRead;
  5. IOUtils.closeQuietly(objectContent, LOG);
  6. IOUtils.closeQuietly(randomAccessFile, LOG);
  7. IOUtils.closeQuietly(channel, LOG);

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

  1. final byte[] buffer = new byte[DEFAULT_BYTE_SIZE];
  2. int bytesRead;
  3. while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) {
  4. outputStream.write(buffer, 0, bytesRead);
  5. "Unable to store object contents to disk: " + e.getMessage(), e);
  6. } finally {
  7. closeQuietly(outputStream, log);
  8. closeQuietly(s3Object.getObjectContent(), log);
  9. return s3Object.getObjectMetadata();

相关文章