Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2016 the original author or authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.miovision.oss.awsbillingtools.s3.loader; import com.miovision.oss.awsbillingtools.FileType; import com.miovision.oss.awsbillingtools.parser.BillingRecordParser; import com.miovision.oss.awsbillingtools.s3.scanner.BillingRecordFileNotFoundException; import com.miovision.oss.awsbillingtools.s3.scanner.S3BillingRecordFile; import com.miovision.oss.awsbillingtools.s3.scanner.S3BillingRecordFileScanner; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.S3Object; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.stream.Stream; import java.util.zip.ZipInputStream; /** * Loads billing record objects from S3. * * @param <T> The record type. */ public class S3BillingRecordLoader<T> { private final AmazonS3 amazonS3; private final S3BillingRecordFileScanner fileScanner; private final BillingRecordParser<T> billingRecordParser; private final FileType fileType; public S3BillingRecordLoader(AmazonS3 amazonS3, S3BillingRecordFileScanner fileScanner, BillingRecordParser<T> billingRecordParser) { this.amazonS3 = amazonS3; this.fileScanner = fileScanner; this.billingRecordParser = billingRecordParser; this.fileType = FileType.forClass(billingRecordParser.getRecordTypeClass()); } public Stream<T> load(int year, int month) throws BillingRecordFileNotFoundException, IOException { final S3BillingRecordFile billingRecordFile = fileScanner.scan(fileType, year, month).findFirst() .orElseThrow(() -> new BillingRecordFileNotFoundException(fileType, year, month)); return load(billingRecordFile); } public Stream<T> load(S3BillingRecordFile billingRecordFile) throws IOException { S3Object s3Object = amazonS3.getObject(billingRecordFile.getBucketName(), billingRecordFile.getKey()); try { InputStream inputStream = s3Object.getObjectContent(); if (billingRecordFile.isZip()) { ZipInputStream zipInputStream = new ZipInputStream(inputStream); zipInputStream.getNextEntry(); inputStream = zipInputStream; } return billingRecordParser.parse(new InputStreamReader(inputStream, Charset.defaultCharset())); } catch (Exception e) { s3Object.close(); throw e; } } public boolean canLoad(S3BillingRecordFile s3BillingRecordFile) { final Class<T> recordTypeClass = billingRecordParser.getRecordTypeClass(); final FileType parserFileType = FileType.forClass(recordTypeClass); return s3BillingRecordFile.getType() == parserFileType; } }