@Override public void close() { // some cases require this close here if(toClose != null) { IOUtils.closeQuietly(toClose); toClose = null; } }
@Override public void dispose( StepMetaInterface smi, StepDataInterface sdi ) { meta = (JsonInputMeta) smi; data = (JsonInputData) sdi; if ( data.file != null ) { IOUtils.closeQuietly( data.file ); } data.inputs = null; data.reader = null; data.readerRowSet = null; data.repeatedFields = null; super.dispose( smi, sdi ); }
public static AesZipFileZipEntrySource createZipEntrySource(InputStream is) throws IOException { // generate session key SecureRandom sr = new SecureRandom(); byte[] ivBytes = new byte[16], keyBytes = new byte[16]; sr.nextBytes(ivBytes); sr.nextBytes(keyBytes); final File tmpFile = TempFile.createTempFile("protectedXlsx", ".zip"); copyToFile(is, tmpFile, keyBytes, ivBytes); IOUtils.closeQuietly(is); return fileToSource(tmpFile, keyBytes, ivBytes); }
/** * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void writeAndClose(Workbook doc, OutputStream out) throws IOException { try { doc.write(out); } finally { closeQuietly(doc); } }
/** * Like {@link #writeAndClose(POIDocument, File)}, but for writing a POI Document in place (to the same file that it was opened from). * This will attempt to close the document, even if an error occurred while writing the document. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable document to write in-place * @throws IOException thrown on errors writing to the file * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void writeAndClose(POIDocument doc) throws IOException { try { doc.write(); } finally { closeQuietly(doc); } }
/** * Write a POI Document ({@link org.apache.poi.ss.usermodel.Workbook}, {@link org.apache.poi.sl.usermodel.SlideShow}, etc) to an output stream and close the output stream. * This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable document to write to the output stream * @param out the output stream that the document is written to * @throws IOException thrown on errors writing to the stream * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void write(POIDocument doc, OutputStream out) throws IOException { try { doc.write(out); } finally { closeQuietly(out); } }
/** * Write a POI Document ({@link org.apache.poi.ss.usermodel.Workbook}, {@link org.apache.poi.sl.usermodel.SlideShow}, etc) to an output stream and close the output stream. * This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. * This will also attempt to close the document, even if an error occurred while writing the document or closing the output stream. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable and closeable document to write to the output stream, then close * @param out the output stream that the document is written to * @throws IOException thrown on errors writing to the stream * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void writeAndClose(POIDocument doc, OutputStream out) throws IOException { try { write(doc, out); } finally { closeQuietly(doc); } }
/** * Write a ({@link org.apache.poi.ss.usermodel.Workbook}) to an output stream and close the output stream. * This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable document to write to the output stream * @param out the output stream that the document is written to * @throws IOException thrown on errors writing to the stream * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void write(Workbook doc, OutputStream out) throws IOException { try { doc.write(out); } finally { closeQuietly(out); } }
/** * Like {@link #writeAndClose(POIDocument, OutputStream)}, but for writing to a File instead of an OutputStream. * This will attempt to close the document, even if an error occurred while writing the document. * * If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. * This function exists for Java 6 code. * * @param doc a writeable and closeable document to write to the output file, then close * @param out the output file that the document is written to * @throws IOException thrown on errors writing to the stream * * @deprecated since 4.0, use try-with-resources, will be removed in 4.2 */ @Deprecated @Removal(version="4.2") public static void writeAndClose(POIDocument doc, File out) throws IOException { try { doc.write(out); } finally { closeQuietly(doc); } }
static OPCPackage empty() { InputStream is = XMLSlideShow.class.getResourceAsStream("empty.pptx"); if (is == null) { throw new POIXMLException("Missing resource 'empty.pptx'"); } try { return OPCPackage.open(is); } catch (Exception e) { throw new POIXMLException(e); } finally { IOUtils.closeQuietly(is); } }
/** * Open an user provided {@link ZipEntrySource} with read-only permission. * This method can be used to stream data into POI. * Opposed to other open variants, the data is read as-is, e.g. there aren't * any zip-bomb protection put in place. * * @param zipEntry the custom source * @return A Package object * @throws InvalidFormatException if a parsing error occur. */ public static OPCPackage open(ZipEntrySource zipEntry) throws InvalidFormatException { OPCPackage pack = new ZipPackage(zipEntry, PackageAccess.READ); try { if (pack.partList == null) { pack.getParts(); } // pack.originalPackagePath = file.getAbsolutePath(); return pack; } catch (InvalidFormatException | RuntimeException e) { IOUtils.closeQuietly(pack); throw e; } }
public OldExcelExtractor(File f) throws IOException { POIFSFileSystem poifs = null; try { poifs = new POIFSFileSystem(f); open(poifs); toClose = poifs; return; } catch (OldExcelFormatException | NotOLE2FileException e) { // will be handled by workaround below } finally { if (toClose == null) { IOUtils.closeQuietly(poifs); } } @SuppressWarnings("resource") FileInputStream biffStream = new FileInputStream(f); // NOSONAR try { open(biffStream); } catch (IOException | RuntimeException e) { // ensure that the stream is properly closed here if an Exception // is thrown while opening biffStream.close(); throw e; } }
/** * Constructor. Opens a Zip based Open XML document from * an InputStream. * * @param in * Zip input stream to load. * @param access * The package access mode. * @throws IllegalArgumentException * If the specified input stream not an instance of * ZipInputStream. * @throws IOException * if input stream cannot be opened, read, or closed */ ZipPackage(InputStream in, PackageAccess access) throws IOException { super(access); ZipArchiveThresholdInputStream zis = ZipHelper.openZipStream(in); // NOSONAR try { this.zipArchive = new ZipInputStreamZipEntrySource(zis); } catch (final IOException e) { IOUtils.closeQuietly(zis); throw e; } }
/** * Open a package. * * Note - uses quite a bit more memory than {@link #open(String)}, which * doesn't need to hold the whole zip file in memory, and can take advantage * of native methods * * @param in * The InputStream to read the package from * @return A PackageBase object * * @throws InvalidFormatException */ public static OPCPackage open(InputStream in) throws InvalidFormatException, IOException { OPCPackage pack = new ZipPackage(in, PackageAccess.READ_WRITE); try { if (pack.partList == null) { pack.getParts(); } } catch (InvalidFormatException | RuntimeException e) { IOUtils.closeQuietly(pack); throw e; } return pack; }
public byte[] toByteArray() { byte[] result = new byte[LittleEndianConsts.INT_SIZE*2+_value.length]; LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(result,0); try { bos.writeInt(LittleEndianConsts.INT_SIZE + _value.length); bos.writeInt(_format); bos.write(_value); return result; } finally { IOUtils.closeQuietly(bos); } }
private BigInteger getCrlNumber(X509CRL crl) { byte[] crlNumberExtensionValue = crl.getExtensionValue(Extension.cRLNumber.getId()); if (null == crlNumberExtensionValue) { return null; } try { ASN1InputStream asn1IS1 = null, asn1IS2 = null; try { asn1IS1 = new ASN1InputStream(crlNumberExtensionValue); ASN1OctetString octetString = (ASN1OctetString)asn1IS1.readObject(); byte[] octets = octetString.getOctets(); asn1IS2 = new ASN1InputStream(octets); ASN1Integer integer = (ASN1Integer)asn1IS2.readObject(); return integer.getPositiveValue(); } finally { IOUtils.closeQuietly(asn1IS2); IOUtils.closeQuietly(asn1IS1); } } catch (IOException e) { throw new RuntimeException("I/O error: " + e.getMessage(), e); } }
@Override public boolean hasDirectoryEntry() { InputStream is = null; try { is = getObjectPart().getInputStream(); is = FileMagic.prepareToCheckMagic(is); return FileMagic.valueOf(is) == FileMagic.OLE2; } catch (IOException e) { LOG.log(POILogger.WARN, "can't determine if directory entry exists", e); return false; } finally { IOUtils.closeQuietly(is); } }
public Long getChecksum() { if (this.checksum == null) { InputStream is = null; byte[] data; try { is = getPackagePart().getInputStream(); data = IOUtils.toByteArray(is); } catch (IOException e) { throw new POIXMLException(e); } finally { IOUtils.closeQuietly(is); } this.checksum = IOUtils.calculateChecksum(data); } return this.checksum; }
@Override public void write(OutputStream stream) throws IOException { flushSheets(); EncryptedTempData tempData = new EncryptedTempData(); ZipEntrySource source = null; try { try (OutputStream os = tempData.getOutputStream()) { getXSSFWorkbook().write(os); } // provide ZipEntrySource to poi which decrypts on the fly source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream()); injectData(source, stream); } finally { tempData.dispose(); IOUtils.closeQuietly(source); } }
private static POITextExtractor createEncryptedOOXMLExtractor(POIFSFileSystem fs) throws IOException { String pass = Biff8EncryptionKey.getCurrentUserPassword(); if (pass == null) { pass = Decryptor.DEFAULT_PASSWORD; } EncryptionInfo ei = new EncryptionInfo(fs); Decryptor dec = ei.getDecryptor(); InputStream is = null; try { if (!dec.verifyPassword(pass)) { throw new EncryptedDocumentException("Invalid password specified - use Biff8EncryptionKey.setCurrentUserPassword() before calling extractor"); } is = dec.getDataStream(fs); return createExtractor(OPCPackage.open(is)); } catch (IOException e) { throw e; } catch (Exception e) { throw new EncryptedDocumentException(e); } finally { IOUtils.closeQuietly(is); // also close the NPOIFSFileSystem here as we read all the data // while decrypting fs.close(); } } }