@@ -567,8 +567,23 @@ public static final ParquetMetadata readFooter(InputFile file, MetadataFilter fi
567567
568568 public static final ParquetMetadata readFooter (InputFile file , ParquetReadOptions options , SeekableInputStream f )
569569 throws IOException {
570+ return readFooter (file , options , f , /*closeStreamOnFailure*/ false );
571+ }
572+
573+ public static final ParquetMetadata readFooter (
574+ InputFile file , ParquetReadOptions options , SeekableInputStream f , boolean closeStreamOnFailure )
575+ throws IOException {
570576 ParquetMetadataConverter converter = new ParquetMetadataConverter (options );
571- return readFooter (file , options , f , converter );
577+ try {
578+ return readFooter (file , options , f , converter );
579+ } catch (Exception e ) {
580+ // In case that readFooter throws an exception in the constructor, the new stream
581+ // should be closed. Otherwise, there's no way to close this outside.
582+ if (closeStreamOnFailure ) {
583+ f .close ();
584+ }
585+ throw e ;
586+ }
572587 }
573588
574589 private static final ParquetMetadata readFooter (
@@ -953,46 +968,7 @@ public ParquetFileReader(InputFile file, ParquetReadOptions options) throws IOEx
953968 * @throws IOException if the file can not be opened
954969 */
955970 public ParquetFileReader (InputFile file , ParquetReadOptions options , SeekableInputStream f ) throws IOException {
956- this .converter = new ParquetMetadataConverter (options );
957- this .file = file ;
958- this .f = f ;
959- this .options = options ;
960- try {
961- this .footer = readFooter (file , options , f , converter );
962- } catch (Exception e ) {
963- // In case that reading footer throws an exception in the constructor, the new stream
964- // should be closed. Otherwise, there's no way to close this outside.
965- f .close ();
966- throw e ;
967- }
968-
969- this .fileMetaData = footer .getFileMetaData ();
970- this .fileDecryptor = fileMetaData .getFileDecryptor (); // must be called before filterRowGroups!
971- if (null != fileDecryptor && fileDecryptor .plaintextFile ()) {
972- this .fileDecryptor = null ; // Plaintext file. No need in decryptor
973- }
974-
975- try {
976- this .blocks = filterRowGroups (footer .getBlocks ());
977- } catch (Exception e ) {
978- // In case that filterRowGroups throws an exception in the constructor, the new stream
979- // should be closed. Otherwise, there's no way to close this outside.
980- f .close ();
981- throw e ;
982- }
983- this .blockIndexStores = listWithNulls (this .blocks .size ());
984- this .blockRowRanges = listWithNulls (this .blocks .size ());
985- for (ColumnDescriptor col : footer .getFileMetaData ().getSchema ().getColumns ()) {
986- paths .put (ColumnPath .get (col .getPath ()), col );
987- }
988-
989- if (options .usePageChecksumVerification ()) {
990- this .crc = new CRC32 ();
991- this .crcAllocator = ReusingByteBufferAllocator .strict (options .getAllocator ());
992- } else {
993- this .crc = null ;
994- this .crcAllocator = null ;
995- }
971+ this (file , readFooter (file , options , f , /*closeStreamOnFailure*/ true ), options , f );
996972 }
997973
998974 /**
0 commit comments