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

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

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

IOUtils.toByteArray介绍

[英]Reads and returns the rest of the given input stream as a byte array. Caller is responsible for closing the given input stream.
[中]以字节数组的形式读取并返回给定输入流的其余部分。调用者负责关闭给定的输入流。

代码示例

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

  1. /**
  2. * Reads and returns the rest of the given input stream as a string.
  3. * Caller is responsible for closing the given input stream.
  4. */
  5. public static String toString(InputStream is) throws IOException {
  6. return new String(toByteArray(is), StringUtils.UTF8);
  7. }

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

  1. /**
  2. * Static factory method to create a JsonContent object from the contents of the HttpResponse
  3. * provided
  4. */
  5. public static JsonContent createJsonContent(HttpResponse httpResponse,
  6. JsonFactory jsonFactory) {
  7. byte[] rawJsonContent = null;
  8. try {
  9. if (httpResponse.getContent() != null) {
  10. rawJsonContent = IOUtils.toByteArray(httpResponse.getContent());
  11. }
  12. } catch (Exception e) {
  13. LOG.debug("Unable to read HTTP response content", e);
  14. }
  15. return new JsonContent(rawJsonContent, new ObjectMapper(jsonFactory)
  16. .configure(JsonParser.Feature.ALLOW_COMMENTS, true));
  17. }

代码示例来源: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. * 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/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 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. 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: Netflix/conductor

  1. @Test
  2. public void tesGetWorkflowById() throws Exception {
  3. when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(new Workflow());
  4. Workflow workflow = executionDAOFacade.getWorkflowById("workflowId", true);
  5. assertNotNull(workflow);
  6. verify(indexDAO, never()).get(any(), any());
  7. when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(null);
  8. InputStream stream = ExecutionDAOFacadeTest.class.getResourceAsStream("/test.json");
  9. byte[] bytes = IOUtils.toByteArray(stream);
  10. String jsonString = new String(bytes);
  11. when(indexDAO.get(any(), any())).thenReturn(jsonString);
  12. workflow = executionDAOFacade.getWorkflowById("workflowId", true);
  13. assertNotNull(workflow);
  14. verify(indexDAO, times(1)).get(any(), any());
  15. }

代码示例来源: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. public InvokeEndpointResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. InvokeEndpointResult invokeEndpointResult = new InvokeEndpointResult();
  3. if (context.isStartOfDocument()) {
  4. if (context.getHeader("Content-Type") != null) {
  5. context.setCurrentHeader("Content-Type");
  6. invokeEndpointResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context));
  7. }
  8. if (context.getHeader("x-Amzn-Invoked-Production-Variant") != null) {
  9. context.setCurrentHeader("x-Amzn-Invoked-Production-Variant");
  10. invokeEndpointResult.setInvokedProductionVariant(context.getUnmarshaller(String.class).unmarshall(context));
  11. }
  12. if (context.getHeader("X-Amzn-SageMaker-Custom-Attributes") != null) {
  13. context.setCurrentHeader("X-Amzn-SageMaker-Custom-Attributes");
  14. invokeEndpointResult.setCustomAttributes(context.getUnmarshaller(String.class).unmarshall(context));
  15. }
  16. }
  17. java.io.InputStream is = context.getHttpResponse().getContent();
  18. if (is != null) {
  19. try {
  20. invokeEndpointResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  21. } finally {
  22. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  23. }
  24. }
  25. return invokeEndpointResult;
  26. }

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

  1. public InvokeResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  2. InvokeResult invokeResult = new InvokeResult();
  3. if (context.isStartOfDocument()) {
  4. if (context.getHeader("X-Amz-Function-Error") != null) {
  5. context.setCurrentHeader("X-Amz-Function-Error");
  6. invokeResult.setFunctionError(context.getUnmarshaller(String.class).unmarshall(context));
  7. }
  8. if (context.getHeader("X-Amz-Log-Result") != null) {
  9. context.setCurrentHeader("X-Amz-Log-Result");
  10. invokeResult.setLogResult(context.getUnmarshaller(String.class).unmarshall(context));
  11. }
  12. if (context.getHeader("X-Amz-Executed-Version") != null) {
  13. context.setCurrentHeader("X-Amz-Executed-Version");
  14. invokeResult.setExecutedVersion(context.getUnmarshaller(String.class).unmarshall(context));
  15. }
  16. }
  17. invokeResult.setStatusCode(context.getHttpResponse().getStatusCode());
  18. java.io.InputStream is = context.getHttpResponse().getContent();
  19. if (is != null) {
  20. try {
  21. invokeResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is)));
  22. } finally {
  23. com.amazonaws.util.IOUtils.closeQuietly(is, null);
  24. }
  25. }
  26. return invokeResult;
  27. }

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

  1. /**
  2. * Reads and returns the rest of the given input stream as a string, closing
  3. * the input stream afterwards.
  4. * @param is the input stream.
  5. * @return the rest of the given input stream.
  6. */
  7. public static String toString(InputStream is) throws IOException {
  8. return new String(toByteArray(is), StringUtils.UTF8);
  9. }

代码示例来源:origin: com.amazonaws/aws-java-sdk-core

  1. /**
  2. * Reads and returns the rest of the given input stream as a string.
  3. * Caller is responsible for closing the given input stream.
  4. */
  5. public static String toString(InputStream is) throws IOException {
  6. return new String(toByteArray(is), StringUtils.UTF8);
  7. }

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

  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. updateThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils
  6. .toByteArray(is)));
  7. }
  8. return updateThingShadowResult;
  9. }

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

  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. deleteThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils
  6. .toByteArray(is)));
  7. }
  8. return deleteThingShadowResult;
  9. }

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

  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. getThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils
  6. .toByteArray(is)));
  7. }
  8. return getThingShadowResult;
  9. }

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

  1. @Test
  2. public void test() throws Exception {
  3. PutEventsRequest putEventsRequest = new PutEventsRequest();
  4. List<Event> events = new ArrayList<Event>();
  5. events.add(createEvent());
  6. events.add(createEvent());
  7. putEventsRequest.setEvents(events);
  8. PutEventsRequestMarshaller marshaller = new PutEventsRequestMarshaller();
  9. Request<PutEventsRequest> request = marshaller.marshall(putEventsRequest);
  10. assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding"));
  11. byte[] content = IOUtils.toByteArray(request.getContent());
  12. System.out.println(content.length);
  13. assertEquals("content length", request.getHeaders().get("Content-Length"),
  14. String.valueOf(content.length));
  15. GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content));
  16. String str = IOUtils.toString(gis);
  17. assertTrue("data is compressed", content.length < str.length());
  18. }

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

  1. @Override
  2. public InvokeResult unmarshall(JsonUnmarshallerContext context) throws Exception {
  3. InvokeResult invokeResult = new InvokeResult();
  4. if (context.getHeader("X-Amz-Function-Error") != null)
  5. invokeResult.setFunctionError(context.getHeader("X-Amz-Function-Error"));
  6. if (context.getHeader("X-Amz-Log-Result") != null)
  7. invokeResult.setLogResult(context.getHeader("X-Amz-Log-Result"));
  8. invokeResult.setStatusCode(context.getHttpResponse().getStatusCode());
  9. ByteBuffer payload = EMPTY_BYTEBUFFER;
  10. InputStream content = context.getHttpResponse().getContent();
  11. if (content != null) {
  12. payload = ByteBuffer.wrap(IOUtils.toByteArray(content));
  13. }
  14. invokeResult.setPayload(payload);
  15. return invokeResult;
  16. }

相关文章