Java Code Examples for com.amazonaws.util.IOUtils#toByteArray()

The following examples show how to use com.amazonaws.util.IOUtils#toByteArray() . 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: SecretModel.java    From strongbox with Apache License 2.0 6 votes vote down vote up
private byte[] fromStdin() {
    try {

        InputStream inputStream = System.in;
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
        if (!inputReader.ready()) {
            // Interactive
            char[] secretValue = System.console().readPassword("Enter secret value:");

            if (secretValue == null) {
                throw new IllegalArgumentException("A secret value must be specified");
            }
            return SecretValueConverter.asBytes(secretValue);
        } else {
            // Piped in
            return IOUtils.toByteArray(inputStream);
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to read secret value from stdin", e);
    }
}
 
Example 2
Source File: ExecutionDAOFacadeTest.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Test
public void tesGetWorkflowById() throws Exception {
    when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(new Workflow());
    Workflow workflow = executionDAOFacade.getWorkflowById("workflowId", true);
    assertNotNull(workflow);
    verify(indexDAO, never()).get(any(), any());

    when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(null);
    InputStream stream = ExecutionDAOFacadeTest.class.getResourceAsStream("/test.json");
    byte[] bytes = IOUtils.toByteArray(stream);
    String jsonString = new String(bytes);
    when(indexDAO.get(any(), any())).thenReturn(jsonString);
    workflow = executionDAOFacade.getWorkflowById("workflowId", true);
    assertNotNull(workflow);
    verify(indexDAO, times(1)).get(any(), any());
}
 
Example 3
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
public String upload(InputStream is, String fileKey, String fileName, String suffix, Boolean isPublic) throws Exception {
    validateFile(is, suffix);
    if (isPublic == null) {
        isPublic = Boolean.TRUE;
    }
    if ((is != null) && (fileKey != null)) {
        try {
            byte[] bytes = IOUtils.toByteArray(is);
            s3Client.putObject(
                    new PutObjectRequest(
                    		s3BucketConfig.getName(),
                            fileKey,
                            new ByteArrayInputStream(bytes),
                            S3ObjectMetadata.getObjectMetadata(bytes)
                    ).withCannedAcl(isPublic ? CannedAccessControlList.PublicRead : CannedAccessControlList.AuthenticatedRead)
            );
            return fileName + '.' + suffix;
        } catch (AmazonServiceException | IOException e) {
            throw new BusinessException(Validations.INVALID_S3_BUCKET_CREDENTIALS.getCode());
        } finally {
            is.close();
        }
    } else {
        throw new BusinessException(Validations.INVALID_PARAMETERS.getCode());
    }
}
 
Example 4
Source File: Serializer.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
public String decompress(final String s) throws IOException {
    final Map<String, Object> map = deserialize(s, MAP_TYPE_REFERENCE);

    if (!map.containsKey(COMPRESSED)) {
        return s;
    }

    final byte[] bytes = Base64.decodeBase64((String) map.get(COMPRESSED));
    try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);) {
        return new String(IOUtils.toByteArray(gzipInputStream), StandardCharsets.UTF_8);
    }
}
 
Example 5
Source File: AwsS3FileStorageService.java    From pizzeria with MIT License 5 votes vote down vote up
@Override
protected void saveFileContent(InputStream inputStream, String name, String contentType) throws IOException {
    byte[] bytes = IOUtils.toByteArray(inputStream);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentType(contentType);
    objectMetadata.setContentLength(bytes.length);
    getAmazonS3().putObject(bucketName, name, new ByteArrayInputStream(bytes), objectMetadata);
}
 
Example 6
Source File: RepositoryS3.java    From github-bucket with ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example 7
Source File: AwsUploadRepository.java    From konker-platform with Apache License 2.0 5 votes vote down vote up
public String upload(InputStream is, String fileName, String suffix, Boolean isPublic) throws Exception {
    validateFile(is, suffix);
    if (isPublic == null) {
        isPublic = Boolean.TRUE;
    }
    if ((is != null) && (fileName != null)) {

        try {
            byte[] bytes = IOUtils.toByteArray(is);
            s3Client.putObject(
                    new PutObjectRequest(
                    		cdnConfig.getName(),
                            fileName + '.' + suffix,
                            new ByteArrayInputStream(bytes),
                            S3ObjectMetadata.getObjectMetadata(bytes)
                    ).withCannedAcl(isPublic ? CannedAccessControlList.PublicRead : CannedAccessControlList.AuthenticatedRead)
            );
            return fileName + '.' + suffix;
        } catch (AmazonServiceException | IOException e) {
            throw new BusinessException(Validations.INVALID_S3_BUCKET_CREDENTIALS.getCode());
        } finally {
            is.close();
        }
    } else {
        throw new BusinessException(Validations.INVALID_PARAMETERS.getCode());
    }
}
 
Example 8
Source File: RepositoryS3.java    From github-bucket with ISC License 5 votes vote down vote up
private boolean walk(Iterator<S3ObjectSummary> iter, ObjectId file, String path) throws IOException {
    byte[] content;
    byte[] newHash;
    LOG.debug("Start processing file: {}", path);
    try (DigestInputStream is = new DigestInputStream(repository.open(file).openStream(), DigestUtils.getMd5Digest())) {
        // Get content
        content = IOUtils.toByteArray(is);
        // Get hash
        newHash = is.getMessageDigest().digest();
    }
    if (isUploadFile(iter, path, Hex.encodeHexString(newHash))) {
        LOG.info("Uploading file: {}", path);
        ObjectMetadata bucketMetadata = new ObjectMetadata();
        bucketMetadata.setContentMD5(Base64.encodeAsString(newHash));
        bucketMetadata.setContentLength(content.length);
        // Give Tika a few hints for the content detection
        Metadata tikaMetadata = new Metadata();
        tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
        // Fire!
        try (InputStream bis = TikaInputStream.get(content, tikaMetadata)) {
            bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
            s3.putObject(bucket.getName(), path, bis, bucketMetadata);
            return true;
        }
    }
    LOG.info("Skipping file (same checksum): {}", path);
    return false;
}
 
Example 9
Source File: FileService.java    From halyard with Apache License 2.0 5 votes vote down vote up
private byte[] readFromLocalFilesystem(String path) throws IOException {
  Path absolutePath = absolutePath(path);
  if (absolutePath == null) {
    throw new IOException(
        "Provided path: \"" + path + "\" cannot be resolved to a local absolute path.");
  }
  return IOUtils.toByteArray(new FileInputStream(absolutePath.toString()));
}
 
Example 10
Source File: EmailNotificationService.java    From athenz with Apache License 2.0 5 votes vote down vote up
byte[] readBinaryFromFile(String fileName) {

        byte[] fileByteArray = null;
        URL resource = getClass().getClassLoader().getResource(fileName);
        if (resource != null) {
            try (InputStream fileStream = resource.openStream()) {
                //convert to byte array
                fileByteArray = IOUtils.toByteArray(fileStream);

            } catch (IOException ex) {
                LOGGER.error("Could not read file: {}. Error message: {}", fileName, ex.getMessage());
            }
        }
        return fileByteArray;
    }
 
Example 11
Source File: TestVectorRunner.java    From aws-encryption-sdk-java with Apache License 2.0 4 votes vote down vote up
private static byte[] readBytesFromJar(JarFile jar, String fileName) throws IOException {
    try (InputStream is = readFromJar(jar, fileName)) {
        return IOUtils.toByteArray(is);
    }
}