/** * Reads a system resource fully into a String * * @param location * Relative or absolute location of system resource. * @return String contents of resource file * @throws RuntimeException * if any error occurs */ protected String getResourceAsString(String location) { try { InputStream resourceStream = getClass().getResourceAsStream(location); String resourceAsString = IOUtils.toString(resourceStream); resourceStream.close(); return resourceAsString; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Download an object from S3. * * @param bucket * The name of the bucket * @param key * The file key of the object to upload/update. * @param result * The Hash map where data from the object are written * */ protected Status readFromStorage(String bucket, String key, Map<String, ByteIterator> result, SSECustomerKey ssecLocal) { try { S3Object object = getS3ObjectAndMetadata(bucket, key, ssecLocal); InputStream objectData = object.getObjectContent(); //consuming the stream // writing the stream to bytes and to results result.put(key, new ByteArrayByteIterator(IOUtils.toByteArray(objectData))); objectData.close(); object.close(); } catch (Exception e){ System.err.println("Not possible to get the object "+key); e.printStackTrace(); return Status.ERROR; } return Status.OK; }
/** * 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); } } }
public GetIntrospectionSchemaResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetIntrospectionSchemaResult getIntrospectionSchemaResult = new GetIntrospectionSchemaResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getIntrospectionSchemaResult.setSchema(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getIntrospectionSchemaResult; }
private JmesPathExpression getAstFromArgument(String argument, Map<String, JmesPathExpression> argumentToAstMap) throws IOException { if (argument != null && !argumentToAstMap.containsKey(argument)) { final Process p = executeToAstProcess(argument); if(p.exitValue()!= 0) { throw new RuntimeException(IOUtils.toString(p.getErrorStream())); } JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream())); JmesPathExpression ast = fromAstJsonToAstJava(jsonNode); argumentToAstMap.put(argument, ast); IOUtils.closeQuietly(p.getInputStream(), null); return ast; } else if (argument != null) { return argumentToAstMap.get(argument); } return null; }
@Override public String getObjectAsString(String bucketName, String key) throws AmazonServiceException, SdkClientException { rejectNull(bucketName, "Bucket name must be provided"); rejectNull(key, "Object key must be provided"); S3Object object = getObject(bucketName, key); try { return IOUtils.toString(object.getObjectContent()); } catch (IOException e) { throw new SdkClientException("Error streaming content from S3 during download"); } finally { IOUtils.closeQuietly(object, log); } }
/** * It's important to override writeTo. Some versions of Apache HTTP * client use writeTo for {@link org.apache.http.entity.BufferedHttpEntity} * and the default implementation just delegates to the wrapped entity * which completely bypasses our CRC32 calculating input stream. The * {@link org.apache.http.entity.BufferedHttpEntity} is used for the * request timeout and client execution timeout features. * * @see <a href="https://github.com/aws/aws-sdk-java/issues/526">Issue #526</a> * * @param outstream OutputStream to write contents to */ @Override public void writeTo(OutputStream outstream) throws IOException { try { IOUtils.copy(this.getContent(), outstream); } finally { this.getContent().close(); } } };
public String downloadObject(final String bucketName, final String key) { final S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key)); final InputStream inputStream = object.getObjectContent(); String result = null; try { result = IOUtils.toString(inputStream); } catch (final IOException e) { log.warn("Could not download file for bucket: {}, with key: {}", bucketName, key); } finally { if (inputStream != null) { try { inputStream.close(); } catch (final IOException ex) { log.debug("Ignore failure in closing the Closeable", ex); } } } log.info("Downloaded file for bucket: {}, with key: {}", bucketName, key); return result; } }
/** * Downloads the certificate from the provided URL. Asserts that the endpoint is an SNS endpoint and that * the certificate is vended over HTTPs. * * @param certUrl URL to download certificate from. * @return String contents of certificate. * @throws SdkClientException If certificate cannot be downloaded or URL is invalid. */ private String downloadCert(URI certUrl) { try { signingCertUrlVerifier.verifyCertUrl(certUrl); HttpResponse response = client.execute(new HttpGet(certUrl)); if (ApacheUtils.isRequestSuccessful(response)) { try { return IOUtils.toString(response.getEntity().getContent()); } finally { response.getEntity().getContent().close(); } } else { throw new HttpException("Could not download the certificate from SNS", response); } } catch (IOException e) { throw new SdkClientException("Unable to download SNS certificate from " + certUrl.toString(), e); } }
/** * Creates a private key from the file given, either in RSA private key * (.pem) or pkcs8 (.der) format. Other formats will cause an exception to * be thrown. */ public static PrivateKey loadPrivateKey(File privateKeyFile) throws InvalidKeySpecException, IOException { if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".pem") ) { InputStream is = new FileInputStream(privateKeyFile); try { return PEM.readPrivateKey(is); } finally { try {is.close();} catch(IOException ignore) {} } } else if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".der") ) { InputStream is = new FileInputStream(privateKeyFile); try { return RSA.privateKeyFromPKCS8(IOUtils.toByteArray(is)); } finally { try {is.close();} catch(IOException ignore) {} } } else { throw new AmazonClientException("Unsupported file type for private key"); } }
final int code = response.getStatusCode(); final InputStream content = response.getContent(); final Reader reader = new InputStreamReader(response.getContent(), StringUtils.UTF8); final Object obj = GSON_WITH_DATE_FORMATTER.fromJson(reader, t); content.close(); final String error = content == null ? "" : IOUtils.toString(content); final ApiClientException ase = new ApiClientException(error); ase.setStatusCode(response.getStatusCode());
if (originalContent != null && originalContent.markSupported() && !(originalContent instanceof BufferedInputStream)) { originalContent.mark(readLimit); if (originalContent instanceof BufferedInputStream && originalContent.markSupported()) { if (entity != null) { try { closeQuietly(entity.getContent(), log); } catch (IOException e) { log.warn("Cannot close the response content.", e);
public GetExportResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetExportResult getExportResult = new GetExportResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); getExportResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("Content-Disposition") != null) { context.setCurrentHeader("Content-Disposition"); getExportResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getExportResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getExportResult; }
private Partitions loadPartitionFromStream(InputStream stream, String location) { try { return mapper.readValue(stream, Partitions.class); } catch (IOException e) { throw new SdkClientException("Error while loading partitions " + "file from " + location, e); } finally { IOUtils.closeQuietly(stream, null); } } }
@Override public ObjectMetadata getObjectSecurely(GetObjectRequest getObjectRequest, File destinationFile) { assertParameterNotNull(destinationFile, "The destination file parameter must be specified when downloading an object directly to a file"); S3Object s3Object = getObjectSecurely(getObjectRequest); // getObject can return null if constraints were specified but not met if (s3Object == null) return null; OutputStream outputStream = null; try { outputStream = new BufferedOutputStream(new FileOutputStream(destinationFile)); byte[] buffer = new byte[1024*10]; int bytesRead; while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new SdkClientException( "Unable to store object contents to disk: " + e.getMessage(), e); } finally { closeQuietly(outputStream, log); closeQuietly(s3Object.getObjectContent(), log); } /* * Unlike the standard Amazon S3 Client, the Amazon S3 Encryption Client does not do an MD5 check * here because the contents stored in S3 and the contents we just retrieved are different. In * S3, the stored contents are encrypted, and locally, the retrieved contents are decrypted. */ return s3Object.getObjectMetadata(); }
@Override public String getObjectAsString(String bucketName, String key) throws AmazonServiceException, AmazonClientException { assertParameterNotNull(bucketName, "Bucket name must be provided"); assertParameterNotNull(key, "Object key must be provided"); final S3Object object = getObject(bucketName, key); try { return IOUtils.toString(object.getObjectContent()); } catch (final IOException e) { throw new AmazonClientException("Error streaming content from S3 during download"); } }
return IOUtils.toString(inputStream); } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) { throw new SdkClientException("The requested metadata is not found at " + connection.getURL()); } else { if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withStatusCode(statusCode).build())) { IOUtils.closeQuietly(inputStream, LOG);
/** * Unsubscribes this endpoint from the topic. */ public void unsubscribeFromTopic() { try { HttpGet request = new HttpGet(unsubscribeUrl.toURI()); HttpResponse response = httpClient.execute(request); if (!ApacheUtils.isRequestSuccessful(response)) { throw new SdkClientException(String.format("Could not unsubscribe from %s: %d %s.%n%s", getTopicArn(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), IOUtils.toString(response.getEntity().getContent()))); } } catch (Exception e) { throw new SdkClientException(e); } }
S3Object object = s3.getObject(getPartRequest); objectContent = object.getObjectContent(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; IOUtils.closeQuietly(objectContent, LOG); IOUtils.closeQuietly(randomAccessFile, LOG); IOUtils.closeQuietly(channel, LOG);
final byte[] buffer = new byte[DEFAULT_BYTE_SIZE]; int bytesRead; while ((bytesRead = s3Object.getObjectContent().read(buffer)) > -1) { outputStream.write(buffer, 0, bytesRead); "Unable to store object contents to disk: " + e.getMessage(), e); } finally { closeQuietly(outputStream, log); closeQuietly(s3Object.getObjectContent(), log); return s3Object.getObjectMetadata();