ADLSOutputStream.java org::apache::iceberg::azure::adlsv2::ADLSOutputStream org::apache::iceberg::azure::adlsv2 /* *LicensedtotheApacheSoftwareFoundation(ASF)underone *ormorecontributorlicenseagreements.SeetheNOTICEfile *distributedwiththisworkforadditionalinformation *regardingcopyrightownership.TheASFlicensesthisfile *toyouundertheApacheLicense,Version2.0(the *"License");youmaynotusethisfileexceptincompliance *withtheLicense.YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting, *softwaredistributedundertheLicenseisdistributedonan *"ASIS"BASIS,WITHOUTWARRANTIESORCONDITIONSOFANY *KIND,eitherexpressorimplied.SeetheLicenseforthe *specificlanguagegoverningpermissionsandlimitations *undertheLicense. */ packageorg.apache.iceberg.azure.adlsv2; importcom.azure.storage.common.ParallelTransferOptions; importcom.azure.storage.file.datalake.DataLakeFileClient; importcom.azure.storage.file.datalake.options.DataLakeFileOutputStreamOptions; importjava.io.BufferedOutputStream; importjava.io.IOException; importjava.io.OutputStream; importjava.util.Arrays; importorg.apache.iceberg.azure.AzureProperties; importorg.apache.iceberg.io.FileIOMetricsContext; importorg.apache.iceberg.io.PositionOutputStream; importorg.apache.iceberg.metrics.Counter; importorg.apache.iceberg.metrics.MetricsContext; importorg.apache.iceberg.metrics.MetricsContext.Unit; importorg.apache.iceberg.relocated.com.google.common.base.Joiner; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; classADLSOutputStreamextendsPositionOutputStream{ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(ADLSOutputStream.class); privatefinalStackTraceElement[]createStack; privatefinalDataLakeFileClientfileClient; privatefinalAzurePropertiesazureProperties; privateOutputStreamstream; privatefinalCounterwriteBytes; privatefinalCounterwriteOperations; privatelongpos; privatebooleanclosed; ADLSOutputStream( DataLakeFileClientfileClient,AzurePropertiesazureProperties,MetricsContextmetrics) throwsIOException{ this.fileClient=fileClient; this.azureProperties=azureProperties; this.createStack=Thread.currentThread().getStackTrace(); this.writeBytes=metrics.counter(FileIOMetricsContext.WRITE_BYTES,Unit.BYTES); this.writeOperations=metrics.counter(FileIOMetricsContext.WRITE_OPERATIONS); openStream(); } @Override publiclonggetPos(){ returnpos; } @Override publicvoidflush()throwsIOException{ stream.flush(); } @Override publicvoidwrite(intb)throwsIOException{ stream.write(b); pos+=1; writeBytes.increment(); writeOperations.increment(); } @Override publicvoidwrite(byte[]b,intoff,intlen)throwsIOException{ stream.write(b,off,len); pos+=len; writeBytes.increment(len); writeOperations.increment(); } privatevoidopenStream(){ DataLakeFileOutputStreamOptionsoptions=newDataLakeFileOutputStreamOptions(); ParallelTransferOptionstransferOptions=newParallelTransferOptions(); azureProperties.adlsWriteBlockSize().ifPresent(transferOptions::setBlockSizeLong); this.stream=newBufferedOutputStream(fileClient.getOutputStream(options)); } @Override publicvoidclose()throwsIOException{ if(closed){ return; } super.close(); this.closed=true; if(stream!=null){ stream.close(); } } @SuppressWarnings("checkstyle:NoFinalizer") @Override protectedvoidfinalize()throwsThrowable{ super.finalize(); if(!closed){ close();//releasingresourcesismoreimportantthanprintingthewarning Stringtrace=Joiner.on("\n\t").join(Arrays.copyOfRange(createStack,1,createStack.length)); LOG.warn("Unclosedoutputstreamcreatedby:\n\t{}",trace); } } }